Skip to content

Commit

Permalink
Merge pull request #66 from andrew-aladev/master
Browse files Browse the repository at this point in the history
Added -Wconversion, fixed conversion cosmetic issues
  • Loading branch information
tomghuang authored Jun 7, 2021
2 parents 1f73484 + 054a5f1 commit e01da57
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 36 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion src/arg_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/arg_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
2 changes: 1 addition & 1 deletion src/arg_dbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
12 changes: 6 additions & 6 deletions src/arg_dstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/arg_end.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions src/arg_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/arg_getopt_long.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/arg_hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/arg_int.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
12 changes: 6 additions & 6 deletions src/arg_rex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/arg_str.c
Original file line number Diff line number Diff line change
Expand Up @@ -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] = "";
Expand Down Expand Up @@ -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);

Expand Down
12 changes: 6 additions & 6 deletions src/arg_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand All @@ -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++;
}
Expand All @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions src/argtable3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit e01da57

Please sign in to comment.