From 6f61ca87a794a967ae9675fb172f9159c36edacd Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 22 Jan 2019 20:37:23 +0100 Subject: [PATCH] fix some weak splint warnings --- src/alchemy.c | 4 +-- src/kernel/building.c | 38 ++++++++++++++---------- src/kernel/gamedata.h | 2 +- src/kernel/messages.h | 2 +- src/util/strings.c | 68 +++++++++++++++++++++++-------------------- 5 files changed, 62 insertions(+), 52 deletions(-) diff --git a/src/alchemy.c b/src/alchemy.c index b4e7f6253..a0bcf9ea9 100644 --- a/src/alchemy.c +++ b/src/alchemy.c @@ -67,7 +67,7 @@ void new_potiontype(item_type * itype, int level) potion_type *ptype; ptype = (potion_type *)calloc(1, sizeof(potion_type)); - assert(ptype); + assert(ptype != NULL); itype->flags |= ITF_POTION; ptype->itype = itype; ptype->level = level; @@ -120,7 +120,7 @@ void herbsearch(unit * u, int max_take) if (herbsfound) { produceexp(u, SK_HERBALISM, u->number); - i_change(&u->items, whichherb, herbsfound); + (void)i_change(&u->items, whichherb, herbsfound); ADDMSG(&u->faction->msgs, msg_message("herbfound", "unit region amount herb", u, r, herbsfound, whichherb->rtype)); } diff --git a/src/kernel/building.c b/src/kernel/building.c index 09a515fc3..fc90ebe94 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -27,6 +27,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include /* kernel includes */ + #include "curse.h" #include "item.h" #include "unit.h" @@ -95,7 +96,7 @@ static int bt_changes = 1; bool bt_changed(int *cache) { - assert(cache); + assert(cache != NULL); if (*cache != bt_changes) { *cache = bt_changes; return true; @@ -108,7 +109,7 @@ static void bt_register(building_type * btype) size_t len; char data[64]; - selist_push(&buildingtypes, (void *)btype); + (void)selist_push(&buildingtypes, (void *)btype); len = cb_new_kv(btype->_name, strlen(btype->_name), &btype, sizeof(btype), data); assert(len <= sizeof(data)); cb_insert(&cb_bldgtypes, data, len); @@ -129,20 +130,25 @@ static void free_buildingtype(void *ptr) { free(btype); } +static building_type *bt_create(const char *name) { + building_type *btype = (building_type *)calloc(1, sizeof(building_type)); + if (!btype) abort(); + btype->_name = str_strdup(name); + btype->flags = BTF_DEFAULT; + btype->auraregen = 1.0; + btype->maxsize = -1; + btype->capacity = 1; + btype->maxcapacity = -1; + return btype; +} + building_type *bt_get_or_create(const char *name) { assert(name && name[0]); if (name != NULL) { building_type *btype = bt_find_i(name); if (btype == NULL) { - btype = (building_type *)calloc(1, sizeof(building_type)); - if (!btype) abort(); - btype->_name = str_strdup(name); - btype->flags = BTF_DEFAULT; - btype->auraregen = 1.0; - btype->maxsize = -1; - btype->capacity = 1; - btype->maxcapacity = -1; + btype = bt_create(name); bt_register(btype); } return btype; @@ -174,7 +180,7 @@ attrib_type at_building_generic_type = { /* TECH DEBT: simplest thing that works for E3 dwarf/halfling faction rules */ static int adjust_size(const building *b, int bsize) { - assert(b); + assert(b != NULL); if (config_get_int("rules.dwarf_castles", 0) && strcmp(b->type->_name, "castle") == 0) { unit *u = building_owner(b); @@ -190,7 +196,7 @@ static int adjust_size(const building *b, int bsize) { */ const char *buildingtype(const building_type * btype, const building * b, int bsize) { - assert(btype); + assert(btype != NULL); if (b && b->attribs) { if (is_building_type(btype, "generic")) { @@ -411,7 +417,7 @@ building *new_building(const struct building_type * btype, region * r, bname = parameters[P_GEBAEUDE]; } } - assert(bname); + assert(bname != NULL); snprintf(buffer, sizeof(buffer), "%s %s", bname, itoa36(b->no)); b->name = str_strdup(bname); return b; @@ -429,7 +435,7 @@ void remove_building(building ** blist, building * b) static const struct building_type *bt_caravan, *bt_dam, *bt_tunnel; static int btypes; - assert(bfindhash(b->no)); + assert(bfindhash(b->no) != NULL); if (bt_changed(&btypes)) { bt_caravan = bt_find("caravan"); @@ -690,7 +696,7 @@ bool in_safe_building(unit *u1, unit *u2) { } bool is_building_type(const struct building_type *btype, const char *name) { - assert(btype); + assert(btype != NULL); return name && strcmp(btype->_name, name)==0; } @@ -826,7 +832,7 @@ int cmp_wage(const struct building *b, const building * a) } int building_taxes(const building *b) { - assert(b); + assert(b != NULL); return b->type->taxes; } diff --git a/src/kernel/gamedata.h b/src/kernel/gamedata.h index 77469f9da..e14c0a551 100644 --- a/src/kernel/gamedata.h +++ b/src/kernel/gamedata.h @@ -1,4 +1,4 @@ -#pragma once +#pragma once #ifndef _GAMEDATA_H #define _GAMEDATA_H diff --git a/src/kernel/messages.h b/src/kernel/messages.h index 7fa6996e2..ea0afc870 100644 --- a/src/kernel/messages.h +++ b/src/kernel/messages.h @@ -55,7 +55,7 @@ extern "C" { struct mlist ** merge_messages(message_list *ml, message_list *append); void split_messages(message_list *ml, struct mlist **split); -#define ADDMSG(msgs, mcreate) { message * mx = mcreate; if (mx) { add_message(msgs, mx); msg_release(mx); } } +#define ADDMSG(msgs, mcreate) { message * mx = mcreate; if (mx) { (void)add_message(msgs, mx); msg_release(mx); } } void syntax_error(const struct unit *u, struct order *ord); void cmistake(const struct unit *u, struct order *ord, int mno, int mtype); diff --git a/src/util/strings.c b/src/util/strings.c index 7f6cad26c..1158d40f4 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -64,8 +64,8 @@ size_t str_strlcpy(char *dst, const char *src, size_t len) register const char *s = src; register size_t n = len; - assert(src); - assert(dst); + assert(src != NULL); + assert(dst != NULL); /* Copy as many bytes as will fit */ if (n != 0 && --n != 0) { do { @@ -82,7 +82,7 @@ size_t str_strlcpy(char *dst, const char *src, size_t len) return (s - src) + strlen(s); /* count does not include NUL */ } - return (s - src - 1); /* count does not include NUL */ + return (size_t)(s - src - 1); /* count does not include NUL */ #endif } @@ -99,7 +99,7 @@ size_t str_strlcat(char *dst, const char *src, size_t len) /* Find the end of dst and adjust bytes left but don't go past end */ while (*d != '\0' && n-- != 0) d++; - dlen = d - dst; + dlen = (size_t)(d - dst); n = len - dlen; if (n == 0) @@ -143,7 +143,7 @@ void str_replace(char *buffer, size_t size, const char *tmpl, const char *var, char *p = strstr(tmpl, var); size_t len; if (p) { - len = p - tmpl; + len = (size_t)(p - tmpl); } else { len = strlen(tmpl); @@ -170,7 +170,7 @@ void str_replace(char *buffer, size_t size, const char *tmpl, const char *var, int str_hash(const char *s) { int key = 0; - assert(s); + assert(s != NULL); while (*s) { key = key * 37 + *s++; } @@ -200,17 +200,19 @@ unsigned int wang_hash(unsigned int a) } char *str_strdup(const char *s) { - if (s == NULL) return NULL; + if (s != NULL) { #ifdef HAVE_STRDUP - return strdup(s); + return strdup(s); #elif defined(_MSC_VER) - return _strdup(s); + return _strdup(s); #else - size_t len = strlen(s); - char *dup = malloc(len+1); - memcpy(dup, s, len+1); - return dup; + size_t len = strlen(s); + char *dup = malloc(len+1); + memcpy(dup, s, len+1); + return dup; #endif + } + return NULL; } void sbs_printf(struct sbstring *sbs, const char *format, ...) @@ -218,16 +220,18 @@ void sbs_printf(struct sbstring *sbs, const char *format, ...) size_t size = sbs->size - (sbs->end - sbs->begin); if (size > 0) { + int bytes; va_list argp; va_start(argp, format); - int bytes = vsnprintf(sbs->end, size, format, argp); + bytes = vsnprintf(sbs->end, size, format, argp); if (bytes > 0) { - if ((size_t)bytes >= size) { - bytes = size - 1; + size_t len = (size_t)bytes; + if (len >= size) { + len = size - 1; /* terminate truncated output */ - sbs->end[bytes] = '\0'; + sbs->end[len] = '\0'; } - sbs->end += bytes; + sbs->end += len; } va_end(argp); } @@ -235,7 +239,7 @@ void sbs_printf(struct sbstring *sbs, const char *format, ...) void sbs_init(struct sbstring *sbs, char *buffer, size_t size) { - assert(sbs); + assert(sbs != NULL); assert(size > 0); sbs->begin = buffer; sbs->size = size; @@ -246,7 +250,7 @@ void sbs_init(struct sbstring *sbs, char *buffer, size_t size) void sbs_adopt(struct sbstring *sbs, char *buffer, size_t size) { size_t len = strlen(buffer); - assert(sbs); + assert(sbs != NULL); assert(size > len); sbs->begin = buffer; sbs->size = size; @@ -256,7 +260,7 @@ void sbs_adopt(struct sbstring *sbs, char *buffer, size_t size) void sbs_strncat(struct sbstring *sbs, const char *str, size_t size) { size_t len; - assert(sbs); + assert(sbs != NULL); len = sbs->size - (sbs->end - sbs->begin) - 1; if (len < size) { size = len; @@ -269,21 +273,21 @@ void sbs_strncat(struct sbstring *sbs, const char *str, size_t size) void sbs_strcat(struct sbstring *sbs, const char *str) { size_t len; - assert(sbs); + assert(sbs != NULL); len = sbs->size - (sbs->end - sbs->begin); - str_strlcpy(sbs->end, str, len); + (void)str_strlcpy(sbs->end, str, len); sbs->end += strlen(sbs->end); assert(sbs->begin + sbs->size >= sbs->end); } void sbs_substr(sbstring *sbs, ptrdiff_t pos, size_t len) { - if (pos > sbs->end - sbs->begin) { + if (sbs->begin + pos > sbs->end) { /* starting past end of string, do nothing */ sbs->end = sbs->begin; } if (pos >= 0) { - size_t sz = sbs->end - (sbs->begin + pos); + size_t sz = sbs->end - sbs->begin - pos; if (len > sz) len = sz; if (len - pos > 0) { memmove(sbs->begin, sbs->begin + pos, len); @@ -299,15 +303,15 @@ void sbs_substr(sbstring *sbs, ptrdiff_t pos, size_t len) size_t sbs_length(const struct sbstring *sbs) { assert(sbs->begin + sbs->size >= sbs->end); - return sbs->end - sbs->begin; + return (size_t)(sbs->end - sbs->begin); } char *str_unescape(char *str) { char *read = str, *write = str; while (*read) { char * pos = strchr(read, '\\'); - if (pos) { - size_t len = pos - read; + if (pos > read) { + size_t len = (size_t)(pos - read); memmove(write, read, len); write += len; read += (len + 1); @@ -341,15 +345,15 @@ const char *str_escape_ex(const char *str, char *buffer, size_t size, const char { size_t slen = strlen(str); const char *read = str; - char *write = buffer; + unsigned char *write = (unsigned char *)buffer; if (size < 1) { return NULL; } while (slen > 0 && size > 1 && *read) { const char *pos = strpbrk(read, chars); size_t len = size; - if (pos) { - len = pos - read; + if (pos > read) { + len = (size_t)(pos - read); } if (len < size) { unsigned char ch = *(const unsigned char *)pos; @@ -399,7 +403,7 @@ const char *str_escape_ex(const char *str, char *buffer, size_t size, const char break; default: if (size > 5) { - int n = sprintf(write, "\\%03o", ch); + int n = snprintf((char *)write, size, "\\%03o", ch); if (n > 0) { assert(n == 5); write += n;