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
This commit is contained in:
Marian Klymov 2018-06-02 23:52:56 +03:00 committed by Daniel Stenberg
parent 38203f1585
commit c45360d463
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
61 changed files with 213 additions and 273 deletions

View file

@ -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))

View file

@ -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);

View file

@ -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",

View file

@ -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;

View file

@ -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++)

View file

@ -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);

View file

@ -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