fix some weak splint warnings

This commit is contained in:
Enno Rehling 2019-01-22 20:37:23 +01:00
parent f054bdf8cf
commit 6f61ca87a7
5 changed files with 62 additions and 52 deletions

View file

@ -67,7 +67,7 @@ void new_potiontype(item_type * itype, int level)
potion_type *ptype; potion_type *ptype;
ptype = (potion_type *)calloc(1, sizeof(potion_type)); ptype = (potion_type *)calloc(1, sizeof(potion_type));
assert(ptype); assert(ptype != NULL);
itype->flags |= ITF_POTION; itype->flags |= ITF_POTION;
ptype->itype = itype; ptype->itype = itype;
ptype->level = level; ptype->level = level;
@ -120,7 +120,7 @@ void herbsearch(unit * u, int max_take)
if (herbsfound) { if (herbsfound) {
produceexp(u, SK_HERBALISM, u->number); 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", ADDMSG(&u->faction->msgs, msg_message("herbfound",
"unit region amount herb", u, r, herbsfound, whichherb->rtype)); "unit region amount herb", u, r, herbsfound, whichherb->rtype));
} }

View file

@ -27,6 +27,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <spells/regioncurse.h> #include <spells/regioncurse.h>
/* kernel includes */ /* kernel includes */
#include "curse.h" #include "curse.h"
#include "item.h" #include "item.h"
#include "unit.h" #include "unit.h"
@ -95,7 +96,7 @@ static int bt_changes = 1;
bool bt_changed(int *cache) bool bt_changed(int *cache)
{ {
assert(cache); assert(cache != NULL);
if (*cache != bt_changes) { if (*cache != bt_changes) {
*cache = bt_changes; *cache = bt_changes;
return true; return true;
@ -108,7 +109,7 @@ static void bt_register(building_type * btype)
size_t len; size_t len;
char data[64]; 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); len = cb_new_kv(btype->_name, strlen(btype->_name), &btype, sizeof(btype), data);
assert(len <= sizeof(data)); assert(len <= sizeof(data));
cb_insert(&cb_bldgtypes, data, len); cb_insert(&cb_bldgtypes, data, len);
@ -129,20 +130,25 @@ static void free_buildingtype(void *ptr) {
free(btype); 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) building_type *bt_get_or_create(const char *name)
{ {
assert(name && name[0]); assert(name && name[0]);
if (name != NULL) { if (name != NULL) {
building_type *btype = bt_find_i(name); building_type *btype = bt_find_i(name);
if (btype == NULL) { if (btype == NULL) {
btype = (building_type *)calloc(1, sizeof(building_type)); btype = bt_create(name);
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;
bt_register(btype); bt_register(btype);
} }
return 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 */ /* TECH DEBT: simplest thing that works for E3 dwarf/halfling faction rules */
static int adjust_size(const building *b, int bsize) { static int adjust_size(const building *b, int bsize) {
assert(b); assert(b != NULL);
if (config_get_int("rules.dwarf_castles", 0) if (config_get_int("rules.dwarf_castles", 0)
&& strcmp(b->type->_name, "castle") == 0) { && strcmp(b->type->_name, "castle") == 0) {
unit *u = building_owner(b); 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) const char *buildingtype(const building_type * btype, const building * b, int bsize)
{ {
assert(btype); assert(btype != NULL);
if (b && b->attribs) { if (b && b->attribs) {
if (is_building_type(btype, "generic")) { if (is_building_type(btype, "generic")) {
@ -411,7 +417,7 @@ building *new_building(const struct building_type * btype, region * r,
bname = parameters[P_GEBAEUDE]; bname = parameters[P_GEBAEUDE];
} }
} }
assert(bname); assert(bname != NULL);
snprintf(buffer, sizeof(buffer), "%s %s", bname, itoa36(b->no)); snprintf(buffer, sizeof(buffer), "%s %s", bname, itoa36(b->no));
b->name = str_strdup(bname); b->name = str_strdup(bname);
return b; 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 const struct building_type *bt_caravan, *bt_dam, *bt_tunnel;
static int btypes; static int btypes;
assert(bfindhash(b->no)); assert(bfindhash(b->no) != NULL);
if (bt_changed(&btypes)) { if (bt_changed(&btypes)) {
bt_caravan = bt_find("caravan"); 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) { bool is_building_type(const struct building_type *btype, const char *name) {
assert(btype); assert(btype != NULL);
return name && strcmp(btype->_name, name)==0; 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) { int building_taxes(const building *b) {
assert(b); assert(b != NULL);
return b->type->taxes; return b->type->taxes;
} }

View file

@ -1,4 +1,4 @@
#pragma once #pragma once
#ifndef _GAMEDATA_H #ifndef _GAMEDATA_H
#define _GAMEDATA_H #define _GAMEDATA_H

View file

@ -55,7 +55,7 @@ extern "C" {
struct mlist ** merge_messages(message_list *ml, message_list *append); struct mlist ** merge_messages(message_list *ml, message_list *append);
void split_messages(message_list *ml, struct mlist **split); 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 syntax_error(const struct unit *u, struct order *ord);
void cmistake(const struct unit *u, struct order *ord, int mno, int mtype); void cmistake(const struct unit *u, struct order *ord, int mno, int mtype);

View file

@ -64,8 +64,8 @@ size_t str_strlcpy(char *dst, const char *src, size_t len)
register const char *s = src; register const char *s = src;
register size_t n = len; register size_t n = len;
assert(src); assert(src != NULL);
assert(dst); assert(dst != NULL);
/* Copy as many bytes as will fit */ /* Copy as many bytes as will fit */
if (n != 0 && --n != 0) { if (n != 0 && --n != 0) {
do { 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) + 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 #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 */ /* Find the end of dst and adjust bytes left but don't go past end */
while (*d != '\0' && n-- != 0) while (*d != '\0' && n-- != 0)
d++; d++;
dlen = d - dst; dlen = (size_t)(d - dst);
n = len - dlen; n = len - dlen;
if (n == 0) 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); char *p = strstr(tmpl, var);
size_t len; size_t len;
if (p) { if (p) {
len = p - tmpl; len = (size_t)(p - tmpl);
} }
else { else {
len = strlen(tmpl); 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 str_hash(const char *s)
{ {
int key = 0; int key = 0;
assert(s); assert(s != NULL);
while (*s) { while (*s) {
key = key * 37 + *s++; key = key * 37 + *s++;
} }
@ -200,17 +200,19 @@ unsigned int wang_hash(unsigned int a)
} }
char *str_strdup(const char *s) { char *str_strdup(const char *s) {
if (s == NULL) return NULL; if (s != NULL) {
#ifdef HAVE_STRDUP #ifdef HAVE_STRDUP
return strdup(s); return strdup(s);
#elif defined(_MSC_VER) #elif defined(_MSC_VER)
return _strdup(s); return _strdup(s);
#else #else
size_t len = strlen(s); size_t len = strlen(s);
char *dup = malloc(len+1); char *dup = malloc(len+1);
memcpy(dup, s, len+1); memcpy(dup, s, len+1);
return dup; return dup;
#endif #endif
}
return NULL;
} }
void sbs_printf(struct sbstring *sbs, const char *format, ...) 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); size_t size = sbs->size - (sbs->end - sbs->begin);
if (size > 0) { if (size > 0) {
int bytes;
va_list argp; va_list argp;
va_start(argp, format); va_start(argp, format);
int bytes = vsnprintf(sbs->end, size, format, argp); bytes = vsnprintf(sbs->end, size, format, argp);
if (bytes > 0) { if (bytes > 0) {
if ((size_t)bytes >= size) { size_t len = (size_t)bytes;
bytes = size - 1; if (len >= size) {
len = size - 1;
/* terminate truncated output */ /* terminate truncated output */
sbs->end[bytes] = '\0'; sbs->end[len] = '\0';
} }
sbs->end += bytes; sbs->end += len;
} }
va_end(argp); 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) void sbs_init(struct sbstring *sbs, char *buffer, size_t size)
{ {
assert(sbs); assert(sbs != NULL);
assert(size > 0); assert(size > 0);
sbs->begin = buffer; sbs->begin = buffer;
sbs->size = size; 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) void sbs_adopt(struct sbstring *sbs, char *buffer, size_t size)
{ {
size_t len = strlen(buffer); size_t len = strlen(buffer);
assert(sbs); assert(sbs != NULL);
assert(size > len); assert(size > len);
sbs->begin = buffer; sbs->begin = buffer;
sbs->size = size; 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) void sbs_strncat(struct sbstring *sbs, const char *str, size_t size)
{ {
size_t len; size_t len;
assert(sbs); assert(sbs != NULL);
len = sbs->size - (sbs->end - sbs->begin) - 1; len = sbs->size - (sbs->end - sbs->begin) - 1;
if (len < size) { if (len < size) {
size = len; 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) void sbs_strcat(struct sbstring *sbs, const char *str)
{ {
size_t len; size_t len;
assert(sbs); assert(sbs != NULL);
len = sbs->size - (sbs->end - sbs->begin); 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); sbs->end += strlen(sbs->end);
assert(sbs->begin + sbs->size >= sbs->end); assert(sbs->begin + sbs->size >= sbs->end);
} }
void sbs_substr(sbstring *sbs, ptrdiff_t pos, size_t len) 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 */ /* starting past end of string, do nothing */
sbs->end = sbs->begin; sbs->end = sbs->begin;
} }
if (pos >= 0) { 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 > sz) len = sz;
if (len - pos > 0) { if (len - pos > 0) {
memmove(sbs->begin, sbs->begin + pos, len); 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) size_t sbs_length(const struct sbstring *sbs)
{ {
assert(sbs->begin + sbs->size >= sbs->end); 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 *str_unescape(char *str) {
char *read = str, *write = str; char *read = str, *write = str;
while (*read) { while (*read) {
char * pos = strchr(read, '\\'); char * pos = strchr(read, '\\');
if (pos) { if (pos > read) {
size_t len = pos - read; size_t len = (size_t)(pos - read);
memmove(write, read, len); memmove(write, read, len);
write += len; write += len;
read += (len + 1); 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); size_t slen = strlen(str);
const char *read = str; const char *read = str;
char *write = buffer; unsigned char *write = (unsigned char *)buffer;
if (size < 1) { if (size < 1) {
return NULL; return NULL;
} }
while (slen > 0 && size > 1 && *read) { while (slen > 0 && size > 1 && *read) {
const char *pos = strpbrk(read, chars); const char *pos = strpbrk(read, chars);
size_t len = size; size_t len = size;
if (pos) { if (pos > read) {
len = pos - read; len = (size_t)(pos - read);
} }
if (len < size) { if (len < size) {
unsigned char ch = *(const unsigned char *)pos; 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; break;
default: default:
if (size > 5) { if (size > 5) {
int n = sprintf(write, "\\%03o", ch); int n = snprintf((char *)write, size, "\\%03o", ch);
if (n > 0) { if (n > 0) {
assert(n == 5); assert(n == 5);
write += n; write += n;