diff options
author | Daniel Stenberg <daniel@haxx.se> | 2016-04-01 10:14:06 +0200 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2016-04-01 10:46:36 +0200 |
commit | 55452ebdff47f98bf3cc383f1dfc3623fcaefefd (patch) | |
tree | d17d3be8fd9482658f07ef6b4532da9502499cf0 /tests/libtest/lib537.c | |
parent | 7218b52c49aeb1452d1fb94b394b3de2da938f17 (diff) | |
download | curl-55452ebdff47f98bf3cc383f1dfc3623fcaefefd.tar.gz |
curl/mprintf.h: remove support for _MPRINTF_REPLACE
The define is not in our name space and is therefore not protected by
our API promises.
It was only really used by libcurl internals but was mostly erased from
there already in 8aabbf5 (March 2015). This is supposedly the final
death blow to that define from everywhere.
As a side-effect, making sure _MPRINTF_REPLACE is gone and not used, I
made the lib tests in tests/libtest/ use curl_printf.h for its redefine
magic and then subsequently the use of sprintf() got banned in the tests
as well (as it is in libcurl internals) and I then replaced them all
with snprintf().
In the unlikely event that any users is actually using this define and
gets sad by this change, it is very easily copied to the user's own
code.
Diffstat (limited to 'tests/libtest/lib537.c')
-rw-r--r-- | tests/libtest/lib537.c | 123 |
1 files changed, 62 insertions, 61 deletions
diff --git a/tests/libtest/lib537.c b/tests/libtest/lib537.c index 88cbceaec..3e00776a3 100644 --- a/tests/libtest/lib537.c +++ b/tests/libtest/lib537.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al. + * Copyright (C) 1998 - 2013, 2016, Daniel Stenberg, <daniel@haxx.se>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -57,10 +57,11 @@ static char msgbuff[256]; static void store_errmsg(const char *msg, int err) { - if (!err) - sprintf(msgbuff, "%s", msg); + if(!err) + snprintf(msgbuff, sizeof(msgbuff), "%s", msg); else - sprintf(msgbuff, "%s, errno %d, %s", msg, err, strerror(err)); + snprintf(msgbuff, sizeof(msgbuff), "%s, errno %d, %s", msg, err, + strerror(err)); } static void close_file_descriptors(void) @@ -68,7 +69,7 @@ static void close_file_descriptors(void) for (num_open.rlim_cur = 0; num_open.rlim_cur < num_open.rlim_max; num_open.rlim_cur++) - if (fd[num_open.rlim_cur] > 0) + if(fd[num_open.rlim_cur] > 0) close(fd[num_open.rlim_cur]); free(fd); fd = NULL; @@ -85,7 +86,7 @@ static int fopen_works(void) } for (i = 0; i < 3; i++) { fpa[i] = fopen(DEV_NULL, "r"); - if (fpa[i] == NULL) { + if(fpa[i] == NULL) { store_errmsg("fopen() failed", ERRNO); fprintf(stderr, "%s\n", msgbuff); ret = 0; @@ -93,7 +94,7 @@ static int fopen_works(void) } } for (i = 0; i < 3; i++) { - if (fpa[i] != NULL) + if(fpa[i] != NULL) fclose(fpa[i]); } return ret; @@ -113,7 +114,7 @@ static int rlimit(int keep_open) #ifdef HAVE_LONGLONG char fmt_llu[] = "%llu"; - if (sizeof(rl.rlim_max) > sizeof(long)) + if(sizeof(rl.rlim_max) > sizeof(long)) fmt = fmt_llu; else #endif @@ -121,7 +122,7 @@ static int rlimit(int keep_open) /* get initial open file limits */ - if (getrlimit(RLIMIT_NOFILE, &rl) != 0) { + if(getrlimit(RLIMIT_NOFILE, &rl) != 0) { store_errmsg("getrlimit() failed", ERRNO); fprintf(stderr, "%s\n", msgbuff); return -1; @@ -130,19 +131,19 @@ static int rlimit(int keep_open) /* show initial open file limits */ #ifdef RLIM_INFINITY - if (rl.rlim_cur == RLIM_INFINITY) + if(rl.rlim_cur == RLIM_INFINITY) strcpy(strbuff, "INFINITY"); else #endif - sprintf(strbuff, fmt, rl.rlim_cur); + snprintf(strbuff, sizeof(strbuff), fmt, rl.rlim_cur); fprintf(stderr, "initial soft limit: %s\n", strbuff); #ifdef RLIM_INFINITY - if (rl.rlim_max == RLIM_INFINITY) + if(rl.rlim_max == RLIM_INFINITY) strcpy(strbuff, "INFINITY"); else #endif - sprintf(strbuff, fmt, rl.rlim_max); + snprintf(strbuff, sizeof(strbuff), fmt, rl.rlim_max); fprintf(stderr, "initial hard limit: %s\n", strbuff); /* @@ -154,14 +155,14 @@ static int rlimit(int keep_open) * open more than soft limit file descriptors will fail. */ - if (rl.rlim_cur != rl.rlim_max) { + if(rl.rlim_cur != rl.rlim_max) { #ifdef OPEN_MAX - if ((rl.rlim_cur > 0) && + if((rl.rlim_cur > 0) && (rl.rlim_cur < OPEN_MAX)) { fprintf(stderr, "raising soft limit up to OPEN_MAX\n"); rl.rlim_cur = OPEN_MAX; - if (setrlimit(RLIMIT_NOFILE, &rl) != 0) { + if(setrlimit(RLIMIT_NOFILE, &rl) != 0) { /* on failure don't abort just issue a warning */ store_errmsg("setrlimit() failed", ERRNO); fprintf(stderr, "%s\n", msgbuff); @@ -172,7 +173,7 @@ static int rlimit(int keep_open) fprintf(stderr, "raising soft limit up to hard limit\n"); rl.rlim_cur = rl.rlim_max; - if (setrlimit(RLIMIT_NOFILE, &rl) != 0) { + if(setrlimit(RLIMIT_NOFILE, &rl) != 0) { /* on failure don't abort just issue a warning */ store_errmsg("setrlimit() failed", ERRNO); fprintf(stderr, "%s\n", msgbuff); @@ -181,7 +182,7 @@ static int rlimit(int keep_open) /* get current open file limits */ - if (getrlimit(RLIMIT_NOFILE, &rl) != 0) { + if(getrlimit(RLIMIT_NOFILE, &rl) != 0) { store_errmsg("getrlimit() failed", ERRNO); fprintf(stderr, "%s\n", msgbuff); return -3; @@ -190,19 +191,19 @@ static int rlimit(int keep_open) /* show current open file limits */ #ifdef RLIM_INFINITY - if (rl.rlim_cur == RLIM_INFINITY) + if(rl.rlim_cur == RLIM_INFINITY) strcpy(strbuff, "INFINITY"); else #endif - sprintf(strbuff, fmt, rl.rlim_cur); + snprintf(strbuff, sizeof(strbuff), fmt, rl.rlim_cur); fprintf(stderr, "current soft limit: %s\n", strbuff); #ifdef RLIM_INFINITY - if (rl.rlim_max == RLIM_INFINITY) + if(rl.rlim_max == RLIM_INFINITY) strcpy(strbuff, "INFINITY"); else #endif - sprintf(strbuff, fmt, rl.rlim_max); + snprintf(strbuff, sizeof(strbuff), fmt, rl.rlim_max); fprintf(stderr, "current hard limit: %s\n", strbuff); } /* (rl.rlim_cur != rl.rlim_max) */ @@ -228,19 +229,19 @@ static int rlimit(int keep_open) for (nitems = i = 1; nitems <= i; i *= 2) nitems = i; - if (nitems > 0x7fff) + if(nitems > 0x7fff) nitems = 0x40000; do { num_open.rlim_max = sizeof(*memchunk) * (size_t)nitems; - sprintf(strbuff, fmt, num_open.rlim_max); + snprintf(strbuff, sizeof(strbuff), fmt, num_open.rlim_max); fprintf(stderr, "allocating memchunk %s byte array\n", strbuff); memchunk = malloc(sizeof(*memchunk) * (size_t)nitems); - if (!memchunk) { + if(!memchunk) { fprintf(stderr, "memchunk, malloc() failed\n"); nitems /= 2; } } while (nitems && !memchunk); - if (!memchunk) { + if(!memchunk) { store_errmsg("memchunk, malloc() failed", ERRNO); fprintf(stderr, "%s\n", msgbuff); return -4; @@ -256,9 +257,9 @@ static int rlimit(int keep_open) /* set the number of file descriptors we will try to open */ #ifdef RLIM_INFINITY - if ((rl.rlim_cur > 0) && (rl.rlim_cur != RLIM_INFINITY)) { + if((rl.rlim_cur > 0) && (rl.rlim_cur != RLIM_INFINITY)) { #else - if (rl.rlim_cur > 0) { + if(rl.rlim_cur > 0) { #endif /* soft limit minus SAFETY_MARGIN */ num_open.rlim_max = rl.rlim_cur - SAFETY_MARGIN; @@ -267,17 +268,17 @@ static int rlimit(int keep_open) /* a huge number of file descriptors */ for (nitems = i = 1; nitems <= i; i *= 2) nitems = i; - if (nitems > 0x7fff) + if(nitems > 0x7fff) nitems = 0x40000; num_open.rlim_max = nitems; } /* verify that we won't overflow size_t in malloc() */ - if ((size_t)(num_open.rlim_max) > ((size_t)-1) / sizeof(*fd)) { - sprintf(strbuff1, fmt, num_open.rlim_max); - sprintf(strbuff, "unable to allocate an array for %s " - "file descriptors, would overflow size_t", strbuff1); + if((size_t)(num_open.rlim_max) > ((size_t)-1) / sizeof(*fd)) { + snprintf(strbuff1, sizeof(strbuff1), fmt, num_open.rlim_max); + snprintf(strbuff, sizeof(strbuff), "unable to allocate an array for %s " + "file descriptors, would overflow size_t", strbuff1); store_errmsg(strbuff, 0); fprintf(stderr, "%s\n", msgbuff); free(memchunk); @@ -287,15 +288,15 @@ static int rlimit(int keep_open) /* allocate array for file descriptors */ do { - sprintf(strbuff, fmt, num_open.rlim_max); + snprintf(strbuff, sizeof(strbuff), fmt, num_open.rlim_max); fprintf(stderr, "allocating array for %s file descriptors\n", strbuff); fd = malloc(sizeof(*fd) * (size_t)(num_open.rlim_max)); - if (!fd) { + if(!fd) { fprintf(stderr, "fd, malloc() failed\n"); num_open.rlim_max /= 2; } } while (num_open.rlim_max && !fd); - if (!fd) { + if(!fd) { store_errmsg("fd, malloc() failed", ERRNO); fprintf(stderr, "%s\n", msgbuff); free(memchunk); @@ -311,14 +312,14 @@ static int rlimit(int keep_open) num_open.rlim_cur++) fd[num_open.rlim_cur] = -1; - sprintf(strbuff, fmt, num_open.rlim_max); + snprintf(strbuff, sizeof(strbuff), fmt, num_open.rlim_max); fprintf(stderr, "trying to open %s file descriptors\n", strbuff); /* open a dummy descriptor */ fd[0] = open(DEV_NULL, O_RDONLY); - if (fd[0] < 0) { - sprintf(strbuff, "opening of %s failed", DEV_NULL); + if(fd[0] < 0) { + snprintf(strbuff, sizeof(strbuff), "opening of %s failed", DEV_NULL); store_errmsg(strbuff, ERRNO); fprintf(stderr, "%s\n", msgbuff); free(fd); @@ -335,23 +336,23 @@ static int rlimit(int keep_open) fd[num_open.rlim_cur] = dup(fd[0]); - if (fd[num_open.rlim_cur] < 0) { + if(fd[num_open.rlim_cur] < 0) { fd[num_open.rlim_cur] = -1; - sprintf(strbuff1, fmt, num_open.rlim_cur); - sprintf(strbuff, "dup() attempt %s failed", strbuff1); + snprintf(strbuff1, sizeof(strbuff1), fmt, num_open.rlim_cur); + snprintf(strbuff, sizeof(strbuff), "dup() attempt %s failed", strbuff1); fprintf(stderr, "%s\n", strbuff); - sprintf(strbuff1, fmt, num_open.rlim_cur); - sprintf(strbuff, "fds system limit seems close to %s", strbuff1); + snprintf(strbuff1, sizeof(strbuff1), fmt, num_open.rlim_cur); + snprintf(strbuff, sizeof(strbuff), "fds system limit seems close to %s", strbuff1); fprintf(stderr, "%s\n", strbuff); num_open.rlim_max = num_open.rlim_cur - SAFETY_MARGIN; num_open.rlim_cur -= num_open.rlim_max; - sprintf(strbuff1, fmt, num_open.rlim_cur); - sprintf(strbuff, "closing %s file descriptors", strbuff1); + snprintf(strbuff1, sizeof(strbuff1), fmt, num_open.rlim_cur); + snprintf(strbuff, sizeof(strbuff), "closing %s file descriptors", strbuff1); fprintf(stderr, "%s\n", strbuff); for (num_open.rlim_cur = num_open.rlim_max; @@ -361,13 +362,13 @@ static int rlimit(int keep_open) fd[num_open.rlim_cur] = -1; } - sprintf(strbuff, fmt, num_open.rlim_max); + snprintf(strbuff, sizeof(strbuff), fmt, num_open.rlim_max); fprintf(stderr, "shrinking array for %s file descriptors\n", strbuff); /* we don't care if we can't shrink it */ tmpfd = realloc(fd, sizeof(*fd) * (size_t)(num_open.rlim_max)); - if (tmpfd) { + if(tmpfd) { fd = tmpfd; tmpfd = NULL; } @@ -378,7 +379,7 @@ static int rlimit(int keep_open) } - sprintf(strbuff, fmt, num_open.rlim_max); + snprintf(strbuff, sizeof(strbuff), fmt, num_open.rlim_max); fprintf(stderr, "%s file descriptors open\n", strbuff); #if !defined(HAVE_POLL_FINE) && \ @@ -397,8 +398,8 @@ static int rlimit(int keep_open) */ num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN; - if (num_open.rlim_max > num_open.rlim_cur) { - sprintf(strbuff, "select limit is FD_SETSIZE %d", FD_SETSIZE); + if(num_open.rlim_max > num_open.rlim_cur) { + snprintf(strbuff, sizeof(strbuff), "select limit is FD_SETSIZE %d", FD_SETSIZE); store_errmsg(strbuff, 0); fprintf(stderr, "%s\n", msgbuff); close_file_descriptors(); @@ -410,9 +411,9 @@ static int rlimit(int keep_open) for (rl.rlim_cur = 0; rl.rlim_cur < num_open.rlim_max; rl.rlim_cur++) { - if ((fd[rl.rlim_cur] > 0) && + if((fd[rl.rlim_cur] > 0) && ((unsigned int)fd[rl.rlim_cur] > num_open.rlim_cur)) { - sprintf(strbuff, "select limit is FD_SETSIZE %d", FD_SETSIZE); + snprintf(strbuff, sizeof(strbuff), "select limit is FD_SETSIZE %d", FD_SETSIZE); store_errmsg(strbuff, 0); fprintf(stderr, "%s\n", msgbuff); close_file_descriptors(); @@ -432,12 +433,12 @@ static int rlimit(int keep_open) * if it is capable of fopen()ing some additional files. */ - if (!fopen_works()) { - sprintf(strbuff1, fmt, num_open.rlim_max); - sprintf(strbuff, "stdio fopen() fails with %s fds open()", + if(!fopen_works()) { + snprintf(strbuff1, sizeof(strbuff1), fmt, num_open.rlim_max); + snprintf(strbuff, sizeof(strbuff), "stdio fopen() fails with %s fds open()", strbuff1); fprintf(stderr, "%s\n", msgbuff); - sprintf(strbuff, "stdio fopen() fails with lots of fds open()"); + snprintf(strbuff, sizeof(strbuff), "stdio fopen() fails with lots of fds open()"); store_errmsg(strbuff, 0); close_file_descriptors(); free(memchunk); @@ -451,7 +452,7 @@ static int rlimit(int keep_open) /* close file descriptors unless instructed to keep them */ - if (!keep_open) { + if(!keep_open) { close_file_descriptors(); } @@ -472,7 +473,7 @@ int test(char *URL) return 0; /* sure, run this! */ } - if (rlimit(TRUE)) { + if(rlimit(TRUE)) { /* failure */ return TEST_ERR_MAJOR_BAD; } @@ -480,13 +481,13 @@ int test(char *URL) /* run the test with the bunch of open file descriptors and close them all once the test is over */ - if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) { + if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) { fprintf(stderr, "curl_global_init() failed\n"); close_file_descriptors(); return TEST_ERR_MAJOR_BAD; } - if ((curl = curl_easy_init()) == NULL) { + if((curl = curl_easy_init()) == NULL) { fprintf(stderr, "curl_easy_init() failed\n"); close_file_descriptors(); curl_global_cleanup(); |