diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 07531c0..2e0b519 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -38,6 +38,7 @@ if(CMAKE_C_COMPILER_ID MATCHES "MSVC") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4711") else(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wpedantic") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall") diff --git a/src/arg_cmd.c b/src/arg_cmd.c index 1141b57..39664cc 100644 --- a/src/arg_cmd.c +++ b/src/arg_cmd.c @@ -110,7 +110,7 @@ static unsigned int hash_key(const void* key) { unsigned int hash = 5381; while ((c = *str++) != 0) - hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ + hash = ((hash << 5) + hash) + (unsigned int)c; /* hash * 33 + c */ return hash; } diff --git a/src/arg_date.c b/src/arg_date.c index bef2b70..1977479 100644 --- a/src/arg_date.c +++ b/src/arg_date.c @@ -132,7 +132,7 @@ arg_daten(const char* shortopts, const char* longopts, const char* format, const format = "%x"; nbytes = sizeof(struct arg_date) /* storage for struct arg_date */ - + maxcount * sizeof(struct tm); /* storage for tmval[maxcount] array */ + + (size_t)maxcount * sizeof(struct tm); /* storage for tmval[maxcount] array */ /* allocate storage for the arg_date struct + tmval[] array. */ /* we use calloc because we want the tmval[] array zero filled. */ diff --git a/src/arg_dbl.c b/src/arg_dbl.c index 83f352f..3a00860 100644 --- a/src/arg_dbl.c +++ b/src/arg_dbl.c @@ -124,7 +124,7 @@ struct arg_dbl* arg_dbln(const char* shortopts, const char* longopts, const char maxcount = (maxcount < mincount) ? mincount : maxcount; nbytes = sizeof(struct arg_dbl) /* storage for struct arg_dbl */ - + (maxcount + 1) * sizeof(double); /* storage for dval[maxcount] array plus one extra for padding to memory boundary */ + + (size_t)(maxcount + 1) * sizeof(double); /* storage for dval[maxcount] array plus one extra for padding to memory boundary */ result = (struct arg_dbl*)xmalloc(nbytes); diff --git a/src/arg_dstr.c b/src/arg_dstr.c index 884db66..0dbb2cd 100644 --- a/src/arg_dstr.c +++ b/src/arg_dstr.c @@ -231,12 +231,12 @@ void arg_dstr_catf(arg_dstr_t ds, const char* fmt, ...) { if ((n = (int)(2 * strlen(fmt))) < START_VSNBUFF) n = START_VSNBUFF; - buff = (char*)xmalloc(n + 2); - memset(buff, 0, n + 2); + buff = (char*)xmalloc((size_t)(n + 2)); + memset(buff, 0, (size_t)(n + 2)); for (;;) { va_start(arglist, fmt); - r = vsnprintf(buff, n + 1, fmt, arglist); + r = vsnprintf(buff, (size_t)(n + 1), fmt, arglist); va_end(arglist); slen = strlen(buff); @@ -249,8 +249,8 @@ void arg_dstr_catf(arg_dstr_t ds, const char* fmt, ...) { n += n; xfree(buff); - buff = (char*)xmalloc(n + 2); - memset(buff, 0, n + 2); + buff = (char*)xmalloc((size_t)(n + 2)); + memset(buff, 0, (size_t)(n + 2)); } arg_dstr_cat(ds, buff); @@ -296,7 +296,7 @@ static void setup_append_buf(arg_dstr_t ds, int new_space) { total_space *= 2; } newbuf = (char*)xmalloc((unsigned)total_space); - memset(newbuf, 0, total_space); + memset(newbuf, 0, (size_t)total_space); strcpy(newbuf, ds->data); if (ds->append_data != NULL) { xfree(ds->append_data); diff --git a/src/arg_end.c b/src/arg_end.c index 66ef509..a7abfdd 100644 --- a/src/arg_end.c +++ b/src/arg_end.c @@ -79,9 +79,9 @@ struct arg_end* arg_end(int maxcount) { size_t nbytes; struct arg_end* result; - nbytes = sizeof(struct arg_end) + maxcount * sizeof(int) /* storage for int error[maxcount] array*/ - + maxcount * sizeof(void*) /* storage for void* parent[maxcount] array */ - + maxcount * sizeof(char*); /* storage for char* argval[maxcount] array */ + nbytes = sizeof(struct arg_end) + (size_t)maxcount * sizeof(int) /* storage for int error[maxcount] array*/ + + (size_t)maxcount * sizeof(void*) /* storage for void* parent[maxcount] array */ + + (size_t)maxcount * sizeof(char*); /* storage for char* argval[maxcount] array */ result = (struct arg_end*)xmalloc(nbytes); diff --git a/src/arg_file.c b/src/arg_file.c index def9e9d..1db09d4 100644 --- a/src/arg_file.c +++ b/src/arg_file.c @@ -170,9 +170,9 @@ struct arg_file* arg_filen(const char* shortopts, const char* longopts, const ch maxcount = (maxcount < mincount) ? mincount : maxcount; nbytes = sizeof(struct arg_file) /* storage for struct arg_file */ - + sizeof(char*) * maxcount /* storage for filename[maxcount] array */ - + sizeof(char*) * maxcount /* storage for basename[maxcount] array */ - + sizeof(char*) * maxcount; /* storage for extension[maxcount] array */ + + sizeof(char*) * (size_t)maxcount /* storage for filename[maxcount] array */ + + sizeof(char*) * (size_t)maxcount /* storage for basename[maxcount] array */ + + sizeof(char*) * (size_t)maxcount; /* storage for extension[maxcount] array */ result = (struct arg_file*)xmalloc(nbytes); diff --git a/src/arg_getopt_long.c b/src/arg_getopt_long.c index d8ed8ea..0782713 100644 --- a/src/arg_getopt_long.c +++ b/src/arg_getopt_long.c @@ -261,7 +261,7 @@ parse_long_options(char * const *nargv, const char *options, if ((has_equal = strchr(current_argv, '=')) != NULL) { /* argument found (--option=arg) */ - current_argv_len = has_equal - current_argv; + current_argv_len = (size_t)(has_equal - current_argv); has_equal++; } else current_argv_len = strlen(current_argv); diff --git a/src/arg_hashtable.c b/src/arg_hashtable.c index 99d2d0b..b96fba4 100644 --- a/src/arg_hashtable.c +++ b/src/arg_hashtable.c @@ -132,7 +132,7 @@ arg_hashtable_t* arg_hashtable_create(unsigned int minsize, unsigned int (*hashf h->entrycount = 0; h->hashfn = hashfn; h->eqfn = eqfn; - h->loadlimit = (unsigned int)ceil(size * max_load_factor); + h->loadlimit = (unsigned int)ceil(size * (double)max_load_factor); return h; } @@ -167,7 +167,7 @@ static int arg_hashtable_expand(arg_hashtable_t* h) { xfree(h->table); h->table = newtable; h->tablelength = newsize; - h->loadlimit = (unsigned int)ceil(newsize * max_load_factor); + h->loadlimit = (unsigned int)ceil(newsize * (double)max_load_factor); return -1; } diff --git a/src/arg_int.c b/src/arg_int.c index c9caf90..9d760c9 100644 --- a/src/arg_int.c +++ b/src/arg_int.c @@ -262,7 +262,7 @@ struct arg_int* arg_intn(const char* shortopts, const char* longopts, const char maxcount = (maxcount < mincount) ? mincount : maxcount; nbytes = sizeof(struct arg_int) /* storage for struct arg_int */ - + maxcount * sizeof(int); /* storage for ival[maxcount] array */ + + (size_t)maxcount * sizeof(int); /* storage for ival[maxcount] array */ result = (struct arg_int*)xmalloc(nbytes); diff --git a/src/arg_rex.c b/src/arg_rex.c index d7d9146..65218d3 100644 --- a/src/arg_rex.c +++ b/src/arg_rex.c @@ -241,7 +241,7 @@ struct arg_rex* arg_rexn(const char* shortopts, nbytes = sizeof(struct arg_rex) /* storage for struct arg_rex */ + sizeof(struct privhdr) /* storage for private arg_rex data */ - + maxcount * sizeof(char*); /* storage for sval[maxcount] array */ + + (size_t)maxcount * sizeof(char*); /* storage for sval[maxcount] array */ /* init the arg_hdr struct */ result = (struct arg_rex*)xmalloc(nbytes); @@ -376,7 +376,7 @@ static int trex_newnode(TRex* exp, TRexNodeType type) { n.right = exp->_nsubexpr++; if (exp->_nallocated < (exp->_nsize + 1)) { exp->_nallocated *= 2; - exp->_nodes = (TRexNode*)xrealloc(exp->_nodes, exp->_nallocated * sizeof(TRexNode)); + exp->_nodes = (TRexNode*)xrealloc(exp->_nodes, (size_t)exp->_nallocated * sizeof(TRexNode)); } exp->_nodes[exp->_nsize++] = n; newid = exp->_nsize - 1; @@ -902,8 +902,8 @@ TRex* trex_compile(const TRexChar* pattern, const TRexChar** error, int flags) { TRex* exp = (TRex*)xmalloc(sizeof(TRex)); exp->_eol = exp->_bol = NULL; exp->_p = pattern; - exp->_nallocated = (int)scstrlen(pattern) * sizeof(TRexChar); - exp->_nodes = (TRexNode*)xmalloc(exp->_nallocated * sizeof(TRexNode)); + exp->_nallocated = (int)(scstrlen(pattern) * sizeof(TRexChar)); + exp->_nodes = (TRexNode*)xmalloc((size_t)exp->_nallocated * sizeof(TRexNode)); exp->_nsize = 0; exp->_matches = 0; exp->_nsubexpr = 0; @@ -931,8 +931,8 @@ TRex* trex_compile(const TRexChar* pattern, const TRexChar** error, int flags) { scprintf(_SC("\n")); } #endif - exp->_matches = (TRexMatch*)xmalloc(exp->_nsubexpr * sizeof(TRexMatch)); - memset(exp->_matches, 0, exp->_nsubexpr * sizeof(TRexMatch)); + exp->_matches = (TRexMatch*)xmalloc((size_t)exp->_nsubexpr * sizeof(TRexMatch)); + memset(exp->_matches, 0, (size_t)exp->_nsubexpr * sizeof(TRexMatch)); } else { trex_free(exp); return NULL; diff --git a/src/arg_str.c b/src/arg_str.c index 0e9903b..bf454f2 100644 --- a/src/arg_str.c +++ b/src/arg_str.c @@ -40,7 +40,7 @@ static void arg_str_resetfn(struct arg_str* parent) { int i; - + ARG_TRACE(("%s:resetfn(%p)\n", __FILE__, parent)); for (i = 0; i < parent->count; i++) { parent->sval[i] = ""; @@ -115,7 +115,7 @@ struct arg_str* arg_strn(const char* shortopts, const char* longopts, const char maxcount = (maxcount < mincount) ? mincount : maxcount; nbytes = sizeof(struct arg_str) /* storage for struct arg_str */ - + maxcount * sizeof(char*); /* storage for sval[maxcount] array */ + + (size_t)maxcount * sizeof(char*); /* storage for sval[maxcount] array */ result = (struct arg_str*)xmalloc(nbytes); diff --git a/src/arg_utils.c b/src/arg_utils.c index d98dc83..b6a5142 100644 --- a/src/arg_utils.c +++ b/src/arg_utils.c @@ -121,14 +121,14 @@ static void merge(void* data, int esize, int i, int j, int k, arg_comparefn* com mpos = 0; /* Allocate storage for the merged elements. */ - m = (char*)xmalloc(esize * ((k - i) + 1)); + m = (char*)xmalloc((size_t)(esize * ((k - i) + 1))); /* Continue while either division has elements to merge. */ while (ipos <= j || jpos <= k) { if (ipos > j) { /* The left division has no more elements to merge. */ while (jpos <= k) { - memcpy(&m[mpos * esize], &a[jpos * esize], esize); + memcpy(&m[mpos * esize], &a[jpos * esize], (size_t)esize); jpos++; mpos++; } @@ -137,7 +137,7 @@ static void merge(void* data, int esize, int i, int j, int k, arg_comparefn* com } else if (jpos > k) { /* The right division has no more elements to merge. */ while (ipos <= j) { - memcpy(&m[mpos * esize], &a[ipos * esize], esize); + memcpy(&m[mpos * esize], &a[ipos * esize], (size_t)esize); ipos++; mpos++; } @@ -147,18 +147,18 @@ static void merge(void* data, int esize, int i, int j, int k, arg_comparefn* com /* Append the next ordered element to the merged elements. */ if (comparefn(&a[ipos * esize], &a[jpos * esize]) < 0) { - memcpy(&m[mpos * esize], &a[ipos * esize], esize); + memcpy(&m[mpos * esize], &a[ipos * esize], (size_t)esize); ipos++; mpos++; } else { - memcpy(&m[mpos * esize], &a[jpos * esize], esize); + memcpy(&m[mpos * esize], &a[jpos * esize], (size_t)esize); jpos++; mpos++; } } /* Prepare to pass back the merged data. */ - memcpy(&a[i * esize], m, esize * ((k - i) + 1)); + memcpy(&a[i * esize], m, (size_t)(esize * ((k - i) + 1))); xfree(m); } diff --git a/src/argtable3.c b/src/argtable3.c index a267905..e466919 100644 --- a/src/argtable3.c +++ b/src/argtable3.c @@ -141,7 +141,7 @@ static struct longoptions* alloc_longoptions(struct arg_hdr** table) { /* allocate storage for return data structure as: */ /* (struct longoptions) + (struct options)[noptions] + char[longoptlen] */ - nbytes = sizeof(struct longoptions) + sizeof(struct option) * noptions + longoptlen; + nbytes = sizeof(struct longoptions) + sizeof(struct option) * (size_t)noptions + longoptlen; result = (struct longoptions*)xmalloc(nbytes); result->getoptval = 0; @@ -451,7 +451,7 @@ int arg_parse(int argc, char** argv, void** argtable) { return endtable->count; } - argvcopy = (char**)xmalloc(sizeof(char*) * (argc + 1)); + argvcopy = (char**)xmalloc(sizeof(char*) * (size_t)(argc + 1)); /* Fill in the local copy of argv[]. We need a local copy @@ -515,7 +515,7 @@ static void arg_cat(char** pdest, const char* src, size_t* pndest) { *dest = 0; /* update *pdest and *pndest */ - *pndest = end - dest; + *pndest = (size_t)(end - dest); *pdest = dest; }