fix coverity nag about spellbooks.

This commit is contained in:
Enno Rehling 2017-09-18 19:51:47 +02:00
parent 02ed1dfe0c
commit b4fc7a1ab4
4 changed files with 30 additions and 26 deletions

2
clibs

@ -1 +1 @@
Subproject commit 2a55c27fedec76845cf82c758b7b7c3fa649c286 Subproject commit da2c0cc39b27c98ed8d31b0503426788fc236bd8

View File

@ -212,13 +212,13 @@ typedef struct eq_entry {
equipment *get_equipment(const char *eqname) equipment *get_equipment(const char *eqname)
{ {
char *match; const void *match;
assert(strlen(eqname) < EQNAMELEN); assert(strlen(eqname) < EQNAMELEN);
match = cb_find_str(&cb_equipments, eqname); match = cb_find_str(&cb_equipments, eqname);
if (match) { if (match) {
eq_entry *ent = (eq_entry *)match; const eq_entry *ent = (const eq_entry *)match;
return ent->value; return ent->value;
} }
return NULL; return NULL;
@ -272,7 +272,7 @@ static void free_equipment(equipment *eq) {
} }
static int free_equipment_cb(const void * match, const void * key, size_t keylen, void *cbdata) { static int free_equipment_cb(const void * match, const void * key, size_t keylen, void *cbdata) {
eq_entry * ent = (eq_entry *)match; const eq_entry * ent = (const eq_entry *)match;
free_equipment(ent->value); free_equipment(ent->value);
free(ent->value); free(ent->value);
return 0; return 0;

View File

@ -389,7 +389,7 @@ const potion_type *resource2potion(const resource_type * rtype)
resource_type *rt_find(const char *name) resource_type *rt_find(const char *name)
{ {
char *match; const void *match;
size_t len = strlen(name); size_t len = strlen(name);
if (len >= RTYPENAMELEN) { if (len >= RTYPENAMELEN) {
@ -399,7 +399,7 @@ resource_type *rt_find(const char *name)
} }
match = cb_find_str(&cb_resources, name); match = cb_find_str(&cb_resources, name);
if (match) { if (match) {
rt_entry *ent = (rt_entry *)match; const rt_entry *ent = (const rt_entry *)match;
return ent->value; return ent->value;
} }
return NULL; return NULL;

View File

@ -3007,31 +3007,36 @@ int cast_spell(struct castorder *co)
static critbit_tree cb_spellbooks; static critbit_tree cb_spellbooks;
#define SBNAMELEN 16
typedef struct sb_entry {
char key[SBNAMELEN];
spellbook *value;
} sb_entry;
spellbook * get_spellbook(const char * name) spellbook * get_spellbook(const char * name)
{ {
char buffer[64]; size_t len = strlen(name);
spellbook * result; const void * match;
void * match;
if (len >= SBNAMELEN) {
log_error("spellbook name is longer than %d bytes: %s", SBNAMELEN-1, name);
return NULL;
}
match = cb_find_str(&cb_spellbooks, name); match = cb_find_str(&cb_spellbooks, name);
if (match) { if (!match) {
cb_get_kv(match, &result, sizeof(result)); sb_entry ent;
} memset(ent.key, 0, SBNAMELEN);
else { memcpy(ent.key, name, len);
size_t len = strlen(name); ent.value = create_spellbook(name);
result = create_spellbook(name); if (cb_insert(&cb_spellbooks, &ent, sizeof(ent)) == CB_EXISTS) {
assert(strlen(name) + sizeof(result) < sizeof(buffer));
len = cb_new_kv(name, len, &result, sizeof(result), buffer);
if (cb_insert(&cb_spellbooks, buffer, len) == CB_EXISTS) {
log_error("cb_insert failed although cb_find returned nothing for spellbook=%s", name); log_error("cb_insert failed although cb_find returned nothing for spellbook=%s", name);
assert(!"should not happen"); assert(!"should not happen");
} }
result = 0; return ent.value;
if (cb_find_prefix(&cb_spellbooks, name, strlen(name), &match, 1, 0) > 0) {
cb_get_kv(match, &result, sizeof(result));
}
} }
return result; return ((const sb_entry *)match)->value;
} }
void free_spellbook(spellbook *sb) { void free_spellbook(spellbook *sb) {
@ -3040,9 +3045,8 @@ void free_spellbook(spellbook *sb) {
} }
static int free_spellbook_cb(const void *match, const void *key, size_t keylen, void *data) { static int free_spellbook_cb(const void *match, const void *key, size_t keylen, void *data) {
spellbook *sb; const sb_entry *ent = (const sb_entry *)match;
cb_get_kv(match, &sb, sizeof(sb)); free_spellbook(ent->value);
free_spellbook(sb);
return 0; return 0;
} }