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)
{
char *match;
const void *match;
assert(strlen(eqname) < EQNAMELEN);
match = cb_find_str(&cb_equipments, eqname);
if (match) {
eq_entry *ent = (eq_entry *)match;
const eq_entry *ent = (const eq_entry *)match;
return ent->value;
}
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) {
eq_entry * ent = (eq_entry *)match;
const eq_entry * ent = (const eq_entry *)match;
free_equipment(ent->value);
free(ent->value);
return 0;

View File

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

View File

@ -3007,31 +3007,36 @@ int cast_spell(struct castorder *co)
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)
{
char buffer[64];
spellbook * result;
void * match;
size_t len = strlen(name);
const 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);
if (match) {
cb_get_kv(match, &result, sizeof(result));
}
else {
size_t len = strlen(name);
result = create_spellbook(name);
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) {
if (!match) {
sb_entry ent;
memset(ent.key, 0, SBNAMELEN);
memcpy(ent.key, name, len);
ent.value = create_spellbook(name);
if (cb_insert(&cb_spellbooks, &ent, sizeof(ent)) == CB_EXISTS) {
log_error("cb_insert failed although cb_find returned nothing for spellbook=%s", name);
assert(!"should not happen");
}
result = 0;
if (cb_find_prefix(&cb_spellbooks, name, strlen(name), &match, 1, 0) > 0) {
cb_get_kv(match, &result, sizeof(result));
}
return ent.value;
}
return result;
return ((const sb_entry *)match)->value;
}
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) {
spellbook *sb;
cb_get_kv(match, &sb, sizeof(sb));
free_spellbook(sb);
const sb_entry *ent = (const sb_entry *)match;
free_spellbook(ent->value);
return 0;
}