Skip to content

Commit

Permalink
cppcheck: fix warnings
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Nekto89 authored and bagder committed Jun 11, 2018
1 parent 38203f1 commit c45360d
Show file tree
Hide file tree
Showing 61 changed files with 213 additions and 273 deletions.
3 changes: 1 addition & 2 deletions lib/base64.c
Expand Up @@ -49,13 +49,12 @@ static size_t decodeQuantum(unsigned char *dest, const char *src)
unsigned long i, x = 0;

for(i = 0, s = src; i < 4; i++, s++) {
unsigned long v = 0;

if(*s == '=') {
x = (x << 6);
padding++;
}
else {
unsigned long v = 0;
p = base64;

while(*p && (*p != *s)) {
Expand Down
6 changes: 1 addition & 5 deletions lib/connect.c
Expand Up @@ -1237,8 +1237,6 @@ static int conn_is_conn(struct connectdata *conn, void *param)
curl_socket_t Curl_getconnectinfo(struct Curl_easy *data,
struct connectdata **connp)
{
curl_socket_t sockfd;

DEBUGASSERT(data);

/* this works for an easy handle:
Expand All @@ -1264,12 +1262,10 @@ curl_socket_t Curl_getconnectinfo(struct Curl_easy *data,
if(connp)
/* only store this if the caller cares for it */
*connp = c;
sockfd = c->sock[FIRSTSOCKET];
return c->sock[FIRSTSOCKET];
}
else
return CURL_SOCKET_BAD;

return sockfd;
}

/*
Expand Down
8 changes: 3 additions & 5 deletions lib/content_encoding.c
Expand Up @@ -163,7 +163,6 @@ static CURLcode inflate_stream(struct connectdata *conn,
z_stream *z = &zp->z; /* zlib state structure */
uInt nread = z->avail_in;
Bytef *orig_in = z->next_in;
int status; /* zlib status */
bool done = FALSE;
CURLcode result = CURLE_OK; /* Curl_client_write status */
char *decomp; /* Put the decompressed data here. */
Expand All @@ -184,6 +183,7 @@ static CURLcode inflate_stream(struct connectdata *conn,
/* because the buffer size is fixed, iteratively decompress and transfer to
the client via downstream_write function. */
while(!done) {
int status; /* zlib status */
done = TRUE;

/* (re)set buffer for decompressed output for every iteration */
Expand Down Expand Up @@ -761,7 +761,6 @@ char *Curl_all_content_encodings(void)
const content_encoding * const *cep;
const content_encoding *ce;
char *ace;
char *p;

for(cep = encodings; *cep; cep++) {
ce = *cep;
Expand All @@ -774,7 +773,7 @@ char *Curl_all_content_encodings(void)

ace = malloc(len);
if(ace) {
p = ace;
char *p = ace;
for(cep = encodings; *cep; cep++) {
ce = *cep;
if(!strcasecompare(ce->name, CONTENT_ENCODING_DEFAULT)) {
Expand Down Expand Up @@ -921,10 +920,9 @@ void Curl_unencode_cleanup(struct connectdata *conn)
static const content_encoding *find_encoding(const char *name, size_t len)
{
const content_encoding * const *cep;
const content_encoding *ce;

for(cep = encodings; *cep; cep++) {
ce = *cep;
const content_encoding *ce = *cep;
if((strncasecompare(name, ce->name, len) && !ce->name[len]) ||
(ce->alias && strncasecompare(name, ce->alias, len) && !ce->alias[len]))
return ce;
Expand Down
7 changes: 3 additions & 4 deletions lib/cookie.c
Expand Up @@ -376,13 +376,13 @@ static void strstore(char **str, const char *newstr)
*/
static void remove_expired(struct CookieInfo *cookies)
{
struct Cookie *co, *nx, *pv;
struct Cookie *co, *nx;
curl_off_t now = (curl_off_t)time(NULL);
unsigned int i;

for(i = 0; i < COOKIE_HASH_SIZE; i++) {
struct Cookie *pv = NULL;
co = cookies->cookies[i];
pv = NULL;
while(co) {
nx = co->next;
if(co->expires && co->expires < now) {
Expand Down Expand Up @@ -1385,9 +1385,8 @@ void Curl_cookie_clearsess(struct CookieInfo *cookies)
****************************************************************************/
void Curl_cookie_cleanup(struct CookieInfo *c)
{
unsigned int i;

if(c) {
unsigned int i;
free(c->filename);
for(i = 0; i < COOKIE_HASH_SIZE; i++)
Curl_cookie_freelist(c->cookies[i]);
Expand Down
3 changes: 1 addition & 2 deletions lib/curl_sasl.c
Expand Up @@ -146,7 +146,6 @@ CURLcode Curl_sasl_parse_url_auth_option(struct SASL *sasl,
const char *value, size_t len)
{
CURLcode result = CURLE_OK;
unsigned int mechbit;
size_t mechlen;

if(!len)
Expand All @@ -160,7 +159,7 @@ CURLcode Curl_sasl_parse_url_auth_option(struct SASL *sasl,
if(!strncmp(value, "*", len))
sasl->prefmech = SASL_AUTH_DEFAULT;
else {
mechbit = Curl_sasl_decode_mech(value, len, &mechlen);
unsigned int mechbit = Curl_sasl_decode_mech(value, len, &mechlen);
if(mechbit && mechlen == len)
sasl->prefmech |= mechbit;
else
Expand Down
6 changes: 3 additions & 3 deletions lib/dict.c
Expand Up @@ -95,17 +95,17 @@ static char *unescape_word(struct Curl_easy *data, const char *inputbuff)
{
char *newp = NULL;
char *dictp;
char *ptr;
size_t len;
char ch;
int olen = 0;

CURLcode result = Curl_urldecode(data, inputbuff, 0, &newp, &len, FALSE);
if(!newp || result)
return NULL;

dictp = malloc(((size_t)len)*2 + 1); /* add one for terminating zero */
if(dictp) {
char *ptr;
char ch;
int olen = 0;
/* According to RFC2229 section 2.2, these letters need to be escaped with
\[letter] */
for(ptr = newp;
Expand Down
6 changes: 2 additions & 4 deletions lib/escape.c
Expand Up @@ -82,7 +82,6 @@ char *curl_easy_escape(struct Curl_easy *data, const char *string,
size_t alloc;
char *ns;
char *testing_ptr = NULL;
unsigned char in; /* we need to treat the characters unsigned */
size_t newlen;
size_t strindex = 0;
size_t length;
Expand All @@ -100,7 +99,7 @@ char *curl_easy_escape(struct Curl_easy *data, const char *string,

length = alloc-1;
while(length--) {
in = *string;
unsigned char in = *string; /* we need to treat the characters unsigned */

if(Curl_isunreserved(in))
/* just copy this */
Expand Down Expand Up @@ -150,7 +149,6 @@ CURLcode Curl_urldecode(struct Curl_easy *data,
{
size_t alloc = (length?length:strlen(string)) + 1;
char *ns = malloc(alloc);
unsigned char in;
size_t strindex = 0;
unsigned long hex;
CURLcode result;
Expand All @@ -159,7 +157,7 @@ CURLcode Curl_urldecode(struct Curl_easy *data,
return CURLE_OUT_OF_MEMORY;

while(--alloc > 0) {
in = *string;
unsigned char in = *string;
if(('%' == in) && (alloc > 2) &&
ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
/* this is two hexadecimal digits following a '%' */
Expand Down
6 changes: 3 additions & 3 deletions lib/file.c
Expand Up @@ -256,8 +256,6 @@ static CURLcode file_upload(struct connectdata *conn)
CURLcode result = CURLE_OK;
struct Curl_easy *data = conn->data;
char *buf = data->state.buffer;
size_t nread;
size_t nwrite;
curl_off_t bytecount = 0;
struct_stat file_stat;
const char *buf2;
Expand Down Expand Up @@ -306,6 +304,8 @@ static CURLcode file_upload(struct connectdata *conn)
}

while(!result) {
size_t nread;
size_t nwrite;
int readcount;
result = Curl_fillreadbuffer(conn, (int)data->set.buffer_size, &readcount);
if(result)
Expand Down Expand Up @@ -378,7 +378,6 @@ static CURLcode file_do(struct connectdata *conn, bool *done)
curl_off_t expected_size = 0;
bool size_known;
bool fstated = FALSE;
ssize_t nread;
struct Curl_easy *data = conn->data;
char *buf = data->state.buffer;
curl_off_t bytecount = 0;
Expand Down Expand Up @@ -502,6 +501,7 @@ static CURLcode file_do(struct connectdata *conn, bool *done)
Curl_pgrsTime(data, TIMER_STARTTRANSFER);

while(!result) {
ssize_t nread;
/* Don't fill a whole buffer if we want less than all data */
size_t bytestoread;

Expand Down
3 changes: 1 addition & 2 deletions lib/formdata.c
Expand Up @@ -812,7 +812,6 @@ CURLcode Curl_getformdata(struct Curl_easy *data,
{
CURLcode result = CURLE_OK;
curl_mime *form = NULL;
curl_mime *multipart;
curl_mimepart *part;
struct curl_httppost *file;

Expand All @@ -831,7 +830,7 @@ CURLcode Curl_getformdata(struct Curl_easy *data,
/* Process each top part. */
for(; !result && post; post = post->next) {
/* If we have more than a file here, create a mime subpart and fill it. */
multipart = form;
curl_mime *multipart = form;
if(post->more) {
part = curl_mime_addpart(form);
if(!part)
Expand Down
13 changes: 6 additions & 7 deletions lib/ftp.c
Expand Up @@ -239,8 +239,8 @@ static void close_secondarysocket(struct connectdata *conn)

static void freedirs(struct ftp_conn *ftpc)
{
int i;
if(ftpc->dirs) {
int i;
for(i = 0; i < ftpc->dirdepth; i++) {
free(ftpc->dirs[i]);
ftpc->dirs[i] = NULL;
Expand Down Expand Up @@ -637,8 +637,6 @@ CURLcode Curl_GetFTPResponse(ssize_t *nreadp, /* return number of bytes read */
* line in a response or continue reading. */

curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
time_t timeout; /* timeout in milliseconds */
time_t interval_ms;
struct Curl_easy *data = conn->data;
CURLcode result = CURLE_OK;
struct ftp_conn *ftpc = &conn->proto.ftpc;
Expand All @@ -657,7 +655,8 @@ CURLcode Curl_GetFTPResponse(ssize_t *nreadp, /* return number of bytes read */

while(!*ftpcode && !result) {
/* check and reset timeout value every lap */
timeout = Curl_pp_state_timeout(pp);
time_t timeout = Curl_pp_state_timeout(pp); /* timeout in milliseconds */
time_t interval_ms;

if(timeout <= 0) {
failf(data, "FTP response timeout");
Expand Down Expand Up @@ -1589,7 +1588,6 @@ static CURLcode ftp_state_ul_setup(struct connectdata *conn,
struct FTP *ftp = conn->data->req.protop;
struct Curl_easy *data = conn->data;
struct ftp_conn *ftpc = &conn->proto.ftpc;
int seekerr = CURL_SEEKFUNC_OK;

if((data->state.resume_from && !sizechecked) ||
((data->state.resume_from > 0) && sizechecked)) {
Expand All @@ -1605,6 +1603,7 @@ static CURLcode ftp_state_ul_setup(struct connectdata *conn,
/* 3. pass file-size number of bytes in the source file */
/* 4. lower the infilesize counter */
/* => transfer as usual */
int seekerr = CURL_SEEKFUNC_OK;

if(data->state.resume_from < 0) {
/* Got no given size to start from, figure it out */
Expand Down Expand Up @@ -2782,7 +2781,6 @@ static CURLcode ftp_statemach_act(struct connectdata *conn)
char *ptr = &data->state.buffer[4]; /* start on the first letter */
const size_t buf_size = data->set.buffer_size;
char *dir;
char *store;
bool entry_extracted = FALSE;

dir = malloc(nread + 1);
Expand All @@ -2805,6 +2803,7 @@ static CURLcode ftp_statemach_act(struct connectdata *conn)

if('\"' == *ptr) {
/* it started good */
char *store;
ptr++;
for(store = dir; *ptr;) {
if('\"' == *ptr) {
Expand Down Expand Up @@ -4384,7 +4383,6 @@ static CURLcode ftp_setup_connection(struct connectdata *conn)
{
struct Curl_easy *data = conn->data;
char *type;
char command;
struct FTP *ftp;

conn->data->req.protop = ftp = malloc(sizeof(struct FTP));
Expand All @@ -4402,6 +4400,7 @@ static CURLcode ftp_setup_connection(struct connectdata *conn)
type = strstr(conn->host.rawalloc, ";type=");

if(type) {
char command;
*type = 0; /* it was in the middle of the hostname */
command = Curl_raw_toupper(type[6]);
conn->bits.type_set = TRUE;
Expand Down
11 changes: 4 additions & 7 deletions lib/hash.c
Expand Up @@ -60,8 +60,6 @@ Curl_hash_init(struct curl_hash *h,
comp_function comparator,
curl_hash_dtor dtor)
{
int i;

if(!slots || !hfunc || !comparator ||!dtor) {
return 1; /* failure */
}
Expand All @@ -74,6 +72,7 @@ Curl_hash_init(struct curl_hash *h,

h->table = malloc(slots * sizeof(struct curl_llist));
if(h->table) {
int i;
for(i = 0; i < slots; ++i)
Curl_llist_init(&h->table[i], (curl_llist_dtor) hash_element_dtor);
return 0; /* fine */
Expand Down Expand Up @@ -140,11 +139,10 @@ Curl_hash_add(struct curl_hash *h, void *key, size_t key_len, void *p)
int Curl_hash_delete(struct curl_hash *h, void *key, size_t key_len)
{
struct curl_llist_element *le;
struct curl_hash_element *he;
struct curl_llist *l = FETCH_LIST(h, key, key_len);

for(le = l->head; le; le = le->next) {
he = le->ptr;
struct curl_hash_element *he = le->ptr;
if(h->comp_func(he->key, he->key_len, key, key_len)) {
Curl_llist_remove(l, le, (void *) h);
--h->size;
Expand All @@ -162,13 +160,12 @@ void *
Curl_hash_pick(struct curl_hash *h, void *key, size_t key_len)
{
struct curl_llist_element *le;
struct curl_hash_element *he;
struct curl_llist *l;

if(h) {
l = FETCH_LIST(h, key, key_len);
for(le = l->head; le; le = le->next) {
he = le->ptr;
struct curl_hash_element *he = le->ptr;
if(h->comp_func(he->key, he->key_len, key, key_len)) {
return he->ptr;
}
Expand Down Expand Up @@ -291,7 +288,6 @@ void Curl_hash_start_iterate(struct curl_hash *hash,
struct curl_hash_element *
Curl_hash_next_element(struct curl_hash_iterator *iter)
{
int i;
struct curl_hash *h = iter->hash;

/* Get the next element in the current list, if any */
Expand All @@ -300,6 +296,7 @@ Curl_hash_next_element(struct curl_hash_iterator *iter)

/* If we have reached the end of the list, find the next one */
if(!iter->current_element) {
int i;
for(i = iter->slot_index; i < h->slots; i++) {
if(h->table[i].head) {
iter->current_element = h->table[i].head;
Expand Down

0 comments on commit c45360d

Please sign in to comment.