From c45360d4633850839bb9c2d77dbf8a8285e9ad49 Mon Sep 17 00:00:00 2001 From: Marian Klymov Date: Sat, 2 Jun 2018 23:52:56 +0300 Subject: cppcheck: fix warnings - Get rid of variable that was generating false positive warning (unitialized) - Fix issues in tests - Reduce scope of several variables all over etc Closes #2631 --- tests/libtest/lib1537.c | 15 +++++---------- tests/libtest/lib1554.c | 3 +-- tests/libtest/lib1900.c | 8 ++++---- tests/libtest/lib506.c | 2 +- tests/libtest/lib512.c | 6 ++---- tests/libtest/lib556.c | 2 +- tests/libtest/lib579.c | 3 +-- tests/libtest/lib586.c | 2 +- tests/libtest/testtrace.c | 2 +- tests/server/getpart.c | 2 +- tests/server/rtspd.c | 18 +++++++----------- tests/server/sockfilt.c | 29 ++++++++++++----------------- tests/server/sws.c | 5 ++--- tests/server/testpart.c | 6 +++--- tests/server/tftpd.c | 16 +++++++--------- tests/server/util.c | 5 ++--- tests/unit/unit1300.c | 2 +- tests/unit/unit1303.c | 2 +- tests/unit/unit1307.c | 4 ++-- 19 files changed, 55 insertions(+), 77 deletions(-) (limited to 'tests') diff --git a/tests/libtest/lib1537.c b/tests/libtest/lib1537.c index b07d64fc5..9832c3a30 100644 --- a/tests/libtest/lib1537.c +++ b/tests/libtest/lib1537.c @@ -43,8 +43,7 @@ int test(char *URL) asize = (int)sizeof(a); ptr = curl_easy_escape(NULL, (char *)a, asize); printf("%s\n", ptr); - if(ptr) - curl_free(ptr); + curl_free(ptr); /* deprecated API */ ptr = curl_escape((char *)a, asize); @@ -58,8 +57,7 @@ int test(char *URL) printf("outlen == %d\n", outlen); printf("unescape == original? %s\n", memcmp(raw, a, outlen) ? "no" : "YES"); - if(raw) - curl_free(raw); + curl_free(raw); /* deprecated API */ raw = curl_unescape(ptr, (int)strlen(ptr)); @@ -71,10 +69,8 @@ int test(char *URL) printf("[old] outlen == %d\n", outlen); printf("[old] unescape == original? %s\n", memcmp(raw, a, outlen) ? "no" : "YES"); - if(raw) - curl_free(raw); - if(ptr) - curl_free(ptr); + curl_free(raw); + curl_free(ptr); /* weird input length */ ptr = curl_easy_escape(NULL, (char *)a, -1); @@ -86,8 +82,7 @@ int test(char *URL) printf("unescape -1 length: %s %d\n", ptr, outlen); test_cleanup: - if(ptr) - curl_free(ptr); + curl_free(ptr); curl_global_cleanup(); return (int)res; diff --git a/tests/libtest/lib1554.c b/tests/libtest/lib1554.c index 8842ae2f7..df12fe52e 100644 --- a/tests/libtest/lib1554.c +++ b/tests/libtest/lib1554.c @@ -43,7 +43,6 @@ static void my_unlock(CURL *handle, curl_lock_data data, void *useptr) /* test function */ int test(char *URL) { - CURL *curl; CURLcode res = CURLE_OK; CURLSH *share; int i; @@ -65,7 +64,7 @@ int test(char *URL) still reuse connections since the pool is in the shared object! */ for(i = 0; i < 3; i++) { - curl = curl_easy_init(); + CURL *curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, URL); diff --git a/tests/libtest/lib1900.c b/tests/libtest/lib1900.c index 109c57062..cf55fb332 100644 --- a/tests/libtest/lib1900.c +++ b/tests/libtest/lib1900.c @@ -63,14 +63,14 @@ static int parse_url_file(const char *filename) return 0; while(!feof(f)) { - if(fscanf(f, "%d %s\n", &filetime, buf)) { + if(fscanf(f, "%d %199s\n", &filetime, buf)) { urltime[num_handles] = filetime; urlstring[num_handles] = strdup(buf); num_handles++; continue; } - if(fscanf(f, "blacklist_site %s\n", buf)) { + if(fscanf(f, "blacklist_site %199s\n", buf)) { site_blacklist[blacklist_num_sites] = strdup(buf); blacklist_num_sites++; continue; @@ -192,11 +192,11 @@ int test(char *URL) do { msg = curl_multi_info_read(m, &msgs_left); if(msg && msg->msg == CURLMSG_DONE) { - int i, found = 0; + int i; /* Find out which handle this message is about */ for(i = 0; i < num_handles; i++) { - found = (msg->easy_handle == handles[i]); + int found = (msg->easy_handle == handles[i]); if(found) break; } diff --git a/tests/libtest/lib506.c b/tests/libtest/lib506.c index 1b522be64..5ed4f37ea 100644 --- a/tests/libtest/lib506.c +++ b/tests/libtest/lib506.c @@ -131,7 +131,6 @@ static void *fire(void *ptr) struct curl_slist *headers; struct Tdata *tdata = (struct Tdata*)ptr; CURL *curl; - int i = 0; curl = curl_easy_init(); if(!curl) { @@ -149,6 +148,7 @@ static void *fire(void *ptr) printf("PERFORM\n"); code = curl_easy_perform(curl); if(code) { + int i = 0; fprintf(stderr, "perform url '%s' repeat %d failed, curlcode %d\n", tdata->url, i, (int)code); } diff --git a/tests/libtest/lib512.c b/tests/libtest/lib512.c index 14241dd02..0c83ddd53 100644 --- a/tests/libtest/lib512.c +++ b/tests/libtest/lib512.c @@ -29,15 +29,13 @@ int test(char *URL) { CURLcode code; - CURL *curl; - CURL *curl2; int rc = 99; code = curl_global_init(CURL_GLOBAL_ALL); if(code == CURLE_OK) { - - curl = curl_easy_init(); + CURL *curl = curl_easy_init(); if(curl) { + CURL *curl2; curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); curl_easy_setopt(curl, CURLOPT_HEADER, 1L); diff --git a/tests/libtest/lib556.c b/tests/libtest/lib556.c index acb0f63d1..884f413aa 100644 --- a/tests/libtest/lib556.c +++ b/tests/libtest/lib556.c @@ -71,7 +71,6 @@ int test(char *URL) "Host: ninja\r\n\r\n"; #endif size_t iolen; - char buf[1024]; res = curl_easy_send(curl, request, strlen(request), &iolen); @@ -79,6 +78,7 @@ int test(char *URL) /* we assume that sending always work */ do { + char buf[1024]; /* busy-read like crazy */ res = curl_easy_recv(curl, buf, sizeof(buf), &iolen); diff --git a/tests/libtest/lib579.c b/tests/libtest/lib579.c index cba4b1cb4..4977a03cb 100644 --- a/tests/libtest/lib579.c +++ b/tests/libtest/lib579.c @@ -39,7 +39,6 @@ struct WriteThis { static int progress_callback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow) { - FILE *moo; static int prev_ultotal = -1; static int prev_ulnow = -1; (void)clientp; /* UNUSED */ @@ -53,7 +52,7 @@ static int progress_callback(void *clientp, double dltotal, double dlnow, if((prev_ultotal != (int)ultotal) || (prev_ulnow != (int)ulnow)) { - moo = fopen(libtest_arg2, "ab"); + FILE *moo = fopen(libtest_arg2, "ab"); if(moo) { fprintf(moo, "Progress callback called with UL %d out of %d\n", (int)ulnow, (int)ultotal); diff --git a/tests/libtest/lib586.c b/tests/libtest/lib586.c index 669f71c15..a831b83d4 100644 --- a/tests/libtest/lib586.c +++ b/tests/libtest/lib586.c @@ -99,7 +99,6 @@ static void *fire(void *ptr) CURLcode code; struct Tdata *tdata = (struct Tdata*)ptr; CURL *curl; - int i = 0; curl = curl_easy_init(); if(!curl) { @@ -116,6 +115,7 @@ static void *fire(void *ptr) printf("PERFORM\n"); code = curl_easy_perform(curl); if(code != CURLE_OK) { + int i = 0; fprintf(stderr, "perform url '%s' repeat %d failed, curlcode %d\n", tdata->url, i, (int)code); } diff --git a/tests/libtest/testtrace.c b/tests/libtest/testtrace.c index 63e022b33..3f9eedd80 100644 --- a/tests/libtest/testtrace.c +++ b/tests/libtest/testtrace.c @@ -90,7 +90,6 @@ int libtest_debug_cb(CURL *handle, curl_infotype type, struct libtest_trace_cfg *trace_cfg = userp; const char *text; struct timeval tv; - struct tm *now; char timebuf[20]; char *timestr; time_t secs; @@ -101,6 +100,7 @@ int libtest_debug_cb(CURL *handle, curl_infotype type, timestr = &timebuf[0]; if(trace_cfg->tracetime) { + struct tm *now; tv = tutil_tvnow(); if(!known_offset) { epoch_offset = time(NULL) - tv.tv_sec; diff --git a/tests/server/getpart.c b/tests/server/getpart.c index d434ba289..044705d06 100644 --- a/tests/server/getpart.c +++ b/tests/server/getpart.c @@ -116,7 +116,6 @@ CURLcode Curl_convert_clone(struct Curl_easy *data, static int readline(char **buffer, size_t *bufsize, FILE *stream) { size_t offset = 0; - size_t length; char *newptr; if(!*buffer) { @@ -127,6 +126,7 @@ static int readline(char **buffer, size_t *bufsize, FILE *stream) } for(;;) { + size_t length; int bytestoread = curlx_uztosi(*bufsize - offset); if(!fgets(*buffer + offset, bytestoread, stream)) diff --git a/tests/server/rtspd.c b/tests/server/rtspd.c index 955e9a233..4519eab60 100644 --- a/tests/server/rtspd.c +++ b/tests/server/rtspd.c @@ -340,11 +340,8 @@ static int ProcessRequest(struct httprequest *req) static char request[REQUEST_KEYWORD_SIZE]; static char doc[MAXDOCNAMELEN]; static char prot_str[5]; - char logbuf[256]; int prot_major, prot_minor; - char *end; - int error; - end = strstr(line, END_OF_HEADERS); + char *end = strstr(line, END_OF_HEADERS); logmsg("ProcessRequest() called with testno %ld and line [%s]", req->testno, line); @@ -360,6 +357,7 @@ static int ProcessRequest(struct httprequest *req) &prot_major, &prot_minor) == 5) { char *ptr; + char logbuf[256]; if(!strcmp(prot_str, "HTTP")) { req->protocol = RPROT_HTTP; @@ -426,7 +424,7 @@ static int ProcessRequest(struct httprequest *req) stream = fopen(filename, "rb"); if(!stream) { - error = errno; + int error = errno; logmsg("fopen() failed with error: %d %s", error, strerror(error)); logmsg("Error opening file: %s", filename); logmsg("Couldn't open test file %ld", req->testno); @@ -441,11 +439,10 @@ static int ProcessRequest(struct httprequest *req) int rtp_channel = 0; int rtp_size = 0; int rtp_partno = -1; - int i = 0; char *rtp_scratch = NULL; /* get the custom server control "commands" */ - error = getpart(&cmd, &cmdsize, "reply", "servercmd", stream); + int error = getpart(&cmd, &cmdsize, "reply", "servercmd", stream); fclose(stream); if(error) { logmsg("getpart() failed with error: %d", error); @@ -486,6 +483,7 @@ static int ProcessRequest(struct httprequest *req) &rtp_partno, &rtp_channel, &rtp_size)) { if(rtp_partno == req->partno) { + int i = 0; logmsg("RTP: part %d channel %d size %d", rtp_partno, rtp_channel, rtp_size); @@ -900,7 +898,6 @@ static int send_doc(curl_socket_t sock, struct httprequest *req) size_t count; const char *buffer; char *ptr = NULL; - FILE *stream; char *cmd = NULL; size_t cmdsize = 0; FILE *dump; @@ -912,8 +909,6 @@ static int send_doc(curl_socket_t sock, struct httprequest *req) static char weare[256]; - char partbuf[80]="data"; - logmsg("Send response number %ld part %ld", req->testno, req->partno); switch(req->rcmd) { @@ -987,7 +982,8 @@ static int send_doc(curl_socket_t sock, struct httprequest *req) } else { char *filename = test2file(req->testno); - + char partbuf[80]="data"; + FILE *stream; if(0 != req->partno) snprintf(partbuf, sizeof(partbuf), "data%ld", req->partno); diff --git a/tests/server/sockfilt.c b/tests/server/sockfilt.c index 844d35a4e..2fb947f15 100644 --- a/tests/server/sockfilt.c +++ b/tests/server/sockfilt.c @@ -358,11 +358,11 @@ static ssize_t write_wincon(int fd, const void *buf, size_t count) static ssize_t fullread(int filedes, void *buffer, size_t nbytes) { int error; - ssize_t rc; ssize_t nread = 0; do { - rc = read(filedes, (unsigned char *)buffer + nread, nbytes - nread); + ssize_t rc = read(filedes, + (unsigned char *)buffer + nread, nbytes - nread); if(got_exit_signal) { logmsg("signalled to die"); @@ -404,12 +404,11 @@ static ssize_t fullread(int filedes, void *buffer, size_t nbytes) static ssize_t fullwrite(int filedes, const void *buffer, size_t nbytes) { int error; - ssize_t wc; ssize_t nwrite = 0; do { - wc = write(filedes, (const unsigned char *)buffer + nwrite, - nbytes - nwrite); + ssize_t wc = write(filedes, (const unsigned char *)buffer + nwrite, + nbytes - nwrite); if(got_exit_signal) { logmsg("signalled to die"); @@ -699,8 +698,6 @@ static int select_ws(int nfds, fd_set *readfds, fd_set *writefds, WSANETWORKEVENTS wsanetevents; struct select_ws_data *data; HANDLE handle, *handles; - curl_socket_t sock; - long networkevents; WSAEVENT wsaevent; int error, fds; HANDLE waitevent = NULL; @@ -729,6 +726,7 @@ static int select_ws(int nfds, fd_set *readfds, fd_set *writefds, /* allocate internal array for the internal data */ data = calloc(nfds, sizeof(struct select_ws_data)); if(data == NULL) { + CloseHandle(waitevent); errno = ENOMEM; return -1; } @@ -736,6 +734,7 @@ static int select_ws(int nfds, fd_set *readfds, fd_set *writefds, /* allocate internal array for the internal event handles */ handles = calloc(nfds, sizeof(HANDLE)); if(handles == NULL) { + CloseHandle(waitevent); free(data); errno = ENOMEM; return -1; @@ -743,7 +742,7 @@ static int select_ws(int nfds, fd_set *readfds, fd_set *writefds, /* loop over the handles in the input descriptor sets */ for(fds = 0; fds < nfds; fds++) { - networkevents = 0; + long networkevents = 0; handles[nfd] = 0; if(FD_ISSET(fds, readfds)) @@ -812,8 +811,8 @@ static int select_ws(int nfds, fd_set *readfds, fd_set *writefds, /* loop over the internal handles returned in the descriptors */ for(idx = 0; idx < nfd; idx++) { + curl_socket_t sock = data[idx].fd; handle = handles[idx]; - sock = data[idx].fd; fds = curlx_sktosi(sock); /* check if the current internal handle was triggered */ @@ -920,9 +919,6 @@ static bool juggle(curl_socket_t *sockfdp, curl_socket_t sockfd = CURL_SOCKET_BAD; int maxfd = -99; ssize_t rc; - ssize_t nread_socket; - ssize_t bytes_written; - ssize_t buffer_len; int error = 0; /* 'buffer' is this excessively large only to be able to support things like @@ -1034,6 +1030,7 @@ static bool juggle(curl_socket_t *sockfdp, if(FD_ISSET(fileno(stdin), &fds_read)) { + ssize_t buffer_len; /* read from stdin, commands/data to be dealt with and possibly passed on to the socket @@ -1105,7 +1102,7 @@ static bool juggle(curl_socket_t *sockfdp, } else { /* send away on the socket */ - bytes_written = swrite(sockfd, buffer, buffer_len); + ssize_t bytes_written = swrite(sockfd, buffer, buffer_len); if(bytes_written != buffer_len) { logmsg("Not all data was sent. Bytes to send: %zd sent: %zd", buffer_len, bytes_written); @@ -1133,13 +1130,11 @@ static bool juggle(curl_socket_t *sockfdp, if((sockfd != CURL_SOCKET_BAD) && (FD_ISSET(sockfd, &fds_read)) ) { - - curl_socket_t newfd = CURL_SOCKET_BAD; /* newly accepted socket */ - + ssize_t nread_socket; if(*mode == PASSIVE_LISTEN) { /* there's no stream set up yet, this is an indication that there's a client connecting. */ - newfd = accept(sockfd, NULL, NULL); + curl_socket_t newfd = accept(sockfd, NULL, NULL); if(CURL_SOCKET_BAD == newfd) { error = SOCKERRNO; logmsg("accept(%d, NULL, NULL) failed with error: (%d) %s", diff --git a/tests/server/sws.c b/tests/server/sws.c index 22ef8168a..ec11224f5 100644 --- a/tests/server/sws.c +++ b/tests/server/sws.c @@ -555,7 +555,6 @@ static int ProcessRequest(struct httprequest *req) if(sscanf(req->reqbuf, "CONNECT %" MAXDOCNAMELEN_TXT "s HTTP/%d.%d", doc, &prot_major, &prot_minor) == 3) { char *portp = NULL; - unsigned long part = 0; snprintf(logbuf, sizeof(logbuf), "Received a CONNECT %s HTTP/%d.%d request", @@ -569,6 +568,7 @@ static int ProcessRequest(struct httprequest *req) if(doc[0] == '[') { char *p = &doc[1]; + unsigned long part = 0; /* scan through the hexgroups and store the value of the last group in the 'part' variable and use as test case number!! */ while(*p && (ISXDIGIT(*p) || (*p == ':') || (*p == '.'))) { @@ -954,7 +954,6 @@ static void init_httprequest(struct httprequest *req) is no data waiting, or < 0 if it should be closed */ static int get_request(curl_socket_t sock, struct httprequest *req) { - int error; int fail = 0; char *reqbuf = req->reqbuf; ssize_t got = 0; @@ -1000,7 +999,7 @@ static int get_request(curl_socket_t sock, struct httprequest *req) fail = 1; } else if(got < 0) { - error = SOCKERRNO; + int error = SOCKERRNO; if(EAGAIN == error || EWOULDBLOCK == error) { /* nothing to read at the moment */ return 0; diff --git a/tests/server/testpart.c b/tests/server/testpart.c index 79869e21c..77f148640 100644 --- a/tests/server/testpart.c +++ b/tests/server/testpart.c @@ -30,15 +30,15 @@ int main(int argc, char **argv) { - int rc; char *part; - size_t partlen, i; + size_t partlen; if(argc< 3) { printf("./testpart main sub\n"); } else { - rc = getpart(&part, &partlen, argv[1], argv[2], stdin); + int rc = getpart(&part, &partlen, argv[1], argv[2], stdin); + size_t i; if(rc) return rc; for(i = 0; i < partlen; i++) diff --git a/tests/server/tftpd.c b/tests/server/tftpd.c index a8b565197..c00731fa2 100644 --- a/tests/server/tftpd.c +++ b/tests/server/tftpd.c @@ -955,8 +955,6 @@ static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size) int first = 1, ecode; struct formats *pf; char *filename, *mode = NULL; - int error; - FILE *server; #ifdef USE_WINSOCK DWORD recvtimeout, recvtimeoutbak; #endif @@ -964,9 +962,9 @@ static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size) int toggle = 1; /* Open request dump file. */ - server = fopen(REQUEST_DUMP, "ab"); + FILE *server = fopen(REQUEST_DUMP, "ab"); if(!server) { - error = errno; + int error = errno; logmsg("fopen() failed with error: %d %s", error, strerror(error)); logmsg("Error opening file: %s", REQUEST_DUMP); return -1; @@ -1138,9 +1136,6 @@ static int validate_access(struct testcase *test, const char *filename, int mode) { char *ptr; - long testno, partno; - int error; - char partbuf[80]="data"; logmsg("trying to get file: %s mode %x", filename, mode); @@ -1161,6 +1156,9 @@ static int validate_access(struct testcase *test, ptr = strrchr(filename, '/'); if(ptr) { + char partbuf[80]="data"; + long partno; + long testno; char *file; ptr++; /* skip the slash */ @@ -1194,7 +1192,7 @@ static int validate_access(struct testcase *test, if(file) { FILE *stream = fopen(file, "rb"); if(!stream) { - error = errno; + int error = errno; logmsg("fopen() failed with error: %d %s", error, strerror(error)); logmsg("Error opening file: %s", file); logmsg("Couldn't open test file: %s", file); @@ -1202,7 +1200,7 @@ static int validate_access(struct testcase *test, } else { size_t count; - error = getpart(&test->buffer, &count, "reply", partbuf, stream); + int error = getpart(&test->buffer, &count, "reply", partbuf, stream); fclose(stream); if(error) { logmsg("getpart() failed with error: %d", error); diff --git a/tests/server/util.c b/tests/server/util.c index fdbd71f0f..df681968d 100644 --- a/tests/server/util.c +++ b/tests/server/util.c @@ -101,7 +101,6 @@ void logmsg(const char *msg, ...) va_list ap; char buffer[2048 + 1]; FILE *logfp; - int error; struct timeval tv; time_t sec; struct tm *now; @@ -135,7 +134,7 @@ void logmsg(const char *msg, ...) fclose(logfp); } else { - error = errno; + int error = errno; fprintf(stderr, "fopen() failed with error: %d %s\n", error, strerror(error)); fprintf(stderr, "Error opening file: %s\n", serverlogfile); @@ -217,7 +216,6 @@ int wait_ms(int timeout_ms) #endif struct timeval initial_tv; int pending_ms; - int error; #endif int r = 0; @@ -235,6 +233,7 @@ int wait_ms(int timeout_ms) pending_ms = timeout_ms; initial_tv = tvnow(); do { + int error; #if defined(HAVE_POLL_FINE) r = poll(NULL, 0, pending_ms); #else diff --git a/tests/unit/unit1300.c b/tests/unit/unit1300.c index c64fadef9..6030db0d2 100644 --- a/tests/unit/unit1300.c +++ b/tests/unit/unit1300.c @@ -264,7 +264,7 @@ UNITTEST_START fail_unless(llist_destination.tail != NULL, "llist_destination tail set to null after moving an element"); - fail_unless(llist_destination.tail == llist_destination.tail, + fail_unless(llist_destination.tail == llist_destination.head, "llist_destination tail doesn't equal llist_destination head"); } UNITTEST_STOP diff --git a/tests/unit/unit1303.c b/tests/unit/unit1303.c index 75a8e59c2..b065683a6 100644 --- a/tests/unit/unit1303.c +++ b/tests/unit/unit1303.c @@ -75,7 +75,6 @@ struct timetest { UNITTEST_START { struct curltime now; - time_t timeout; unsigned int i; const struct timetest run[] = { @@ -139,6 +138,7 @@ UNITTEST_START data->progress.t_startop.tv_usec = 0; for(i = 0; i < sizeof(run)/sizeof(run[0]); i++) { + time_t timeout; NOW(run[i].now_s, run[i].now_us); TIMEOUTS(run[i].timeout_ms, run[i].connecttimeout_ms); timeout = Curl_timeleft(data, &now, run[i].connecting); diff --git a/tests/unit/unit1307.c b/tests/unit/unit1307.c index 22afa4bf2..d6664ff69 100644 --- a/tests/unit/unit1307.c +++ b/tests/unit/unit1307.c @@ -272,7 +272,7 @@ enum system { UNITTEST_START { int testnum = sizeof(tests) / sizeof(struct testcase); - int i, rc; + int i; enum system machine; #ifdef HAVE_FNMATCH @@ -290,7 +290,7 @@ UNITTEST_START for(i = 0; i < testnum; i++) { int result = tests[i].result; - rc = Curl_fnmatch(NULL, tests[i].pattern, tests[i].string); + int rc = Curl_fnmatch(NULL, tests[i].pattern, tests[i].string); if(result & (LINUX_DIFFER|MAC_DIFFER)) { if((result & LINUX_DIFFER) && (machine == SYSTEM_LINUX)) result >>= LINUX_SHIFT; -- cgit v1.2.3