Merge branch 'coverity-equipment'

This commit is contained in:
Enno Rehling 2017-09-18 19:13:39 +02:00
commit 6d79f5fad1
3 changed files with 45 additions and 25 deletions

View File

@ -204,6 +204,7 @@ void free_ls(void *arg) {
static critbit_tree cb_equipments = { 0 }; static critbit_tree cb_equipments = { 0 };
#define EQNAMELEN 16 #define EQNAMELEN 16
typedef struct eq_entry { typedef struct eq_entry {
char key[EQNAMELEN]; char key[EQNAMELEN];
equipment *value; equipment *value;
@ -211,7 +212,7 @@ typedef struct eq_entry {
equipment *get_equipment(const char *eqname) equipment *get_equipment(const char *eqname)
{ {
const char *match; char *match;
assert(strlen(eqname) < EQNAMELEN); assert(strlen(eqname) < EQNAMELEN);

View File

@ -60,6 +60,12 @@ static critbit_tree cb_resources;
luxury_type *luxurytypes; luxury_type *luxurytypes;
potion_type *potiontypes; potion_type *potiontypes;
#define RTYPENAMELEN 24
typedef struct rt_entry {
char key[RTYPENAMELEN];
struct resource_type *value;
} rt_entry;
static int res_changeaura(unit * u, const resource_type * rtype, int delta) static int res_changeaura(unit * u, const resource_type * rtype, int delta)
{ {
assert(rtype != NULL); assert(rtype != NULL);
@ -166,13 +172,19 @@ static int num_resources;
static void rt_register(resource_type * rtype) static void rt_register(resource_type * rtype)
{ {
char buffer[64];
const char * name = rtype->_name; const char * name = rtype->_name;
size_t len = strlen(name); size_t len = strlen(name);
rt_entry ent;
assert(len < sizeof(buffer) - sizeof(rtype)); if (len >= RTYPENAMELEN) {
len = cb_new_kv(name, len, &rtype, sizeof(rtype), buffer); log_error("resource name is longer than %d bytes: %s",
cb_insert(&cb_resources, buffer, len); RTYPENAMELEN-1, name);
len = RTYPENAMELEN-1;
}
ent.value = rtype;
memset(ent.key, 0, RTYPENAMELEN);
memcpy(ent.key, name, len);
cb_insert(&cb_resources, &ent, sizeof(ent));
++num_resources; ++num_resources;
} }
@ -186,7 +198,6 @@ resource_type *rt_get_or_create(const char *name) {
else { else {
rtype->_name = strdup(name); rtype->_name = strdup(name);
rt_register(rtype); rt_register(rtype);
return rt_find(name);
} }
} }
return rtype; return rtype;
@ -378,13 +389,20 @@ const potion_type *resource2potion(const resource_type * rtype)
resource_type *rt_find(const char *name) resource_type *rt_find(const char *name)
{ {
void * match; char *match;
resource_type *result = 0; size_t len = strlen(name);
if (cb_find_prefix(&cb_resources, name, strlen(name) + 1, &match, 1, 0)) { if (len >= RTYPENAMELEN) {
cb_get_kv(match, &result, sizeof(result)); log_error("resource name is longer than %d bytes: %s",
RTYPENAMELEN-1, name);
return NULL;
} }
return result; match = cb_find_str(&cb_resources, name);
if (match) {
rt_entry *ent = (rt_entry *)match;
return ent->value;
}
return NULL;
} }
item **i_find(item ** i, const item_type * it) item **i_find(item ** i, const item_type * it)
@ -781,14 +799,14 @@ int change_money(unit * u, int v)
return 0; return 0;
} }
static int add_resourcename_cb(const void * match, const void * key, size_t keylen, void *data) static int add_resourcename_cb(const void * match, const void * key,
size_t keylen, void *data)
{ {
struct locale * lang = (struct locale *)data; struct locale * lang = (struct locale *)data;
int i = locale_index(lang); int i = locale_index(lang);
critbit_tree * cb = rnames + i; critbit_tree * cb = rnames + i;
resource_type *rtype; resource_type *rtype = ((rt_entry *)match)->value;
cb_get_kv(match, &rtype, sizeof(rtype));
for (i = 0; i != 2; ++i) { for (i = 0; i != 2; ++i) {
char buffer[128]; char buffer[128];
const char * name = LOC(lang, resourcename(rtype, (i == 0) ? 0 : NMF_PLURAL)); const char * name = LOC(lang, resourcename(rtype, (i == 0) ? 0 : NMF_PLURAL));
@ -824,20 +842,20 @@ const resource_type *findresourcetype(const char *name, const struct locale *lan
else { else {
log_debug("findresourcetype: transliterate failed for '%s'\n", name); log_debug("findresourcetype: transliterate failed for '%s'\n", name);
} }
return 0; return NULL;
} }
attrib_type at_showitem = { attrib_type at_showitem = {
"showitem" "showitem"
}; };
static int add_itemname_cb(const void * match, const void * key, size_t keylen, void *data) static int add_itemname_cb(const void * match, const void * key,
size_t keylen, void *data)
{ {
struct locale * lang = (struct locale *)data; struct locale * lang = (struct locale *)data;
critbit_tree * cb = inames + locale_index(lang); critbit_tree * cb = inames + locale_index(lang);
resource_type *rtype; resource_type *rtype = ((rt_entry *)match)->value;
cb_get_kv(match, &rtype, sizeof(rtype));
if (rtype->itype) { if (rtype->itype) {
int i; int i;
for (i = 0; i != 2; ++i) { for (i = 0; i != 2; ++i) {
@ -969,9 +987,10 @@ void free_rtype(resource_type *rtype) {
free(rtype); free(rtype);
} }
int free_rtype_cb(const void * match, const void * key, size_t keylen, void *cbdata) { static int free_rtype_cb(const void * match, const void * key,
resource_type *rtype; size_t keylen, void *cbdata)
cb_get_kv(match, &rtype, sizeof(rtype)); {
resource_type *rtype = ((rt_entry *)match)->value;;
free_rtype(rtype); free_rtype(rtype);
return 0; return 0;
} }

View File

@ -178,11 +178,11 @@ static void test_get_resource(CuTest *tc) {
test_setup(); test_setup();
CuAssertPtrEquals(tc, NULL, rt_find("catapultammo")); CuAssertPtrEquals(tc, NULL, rt_find("catapultammo123"));
rtype = rt_get_or_create("catapultammo"); rtype = rt_get_or_create("catapultammo123");
CuAssertPtrNotNull(tc, rtype); CuAssertPtrNotNull(tc, rtype);
CuAssertPtrEquals(tc, rtype, rt_find("catapultammo")); CuAssertPtrEquals(tc, rtype, rt_find("catapultammo123"));
CuAssertStrEquals(tc, "catapultammo", rtype->_name); CuAssertStrEquals(tc, "catapultammo123", rtype->_name);
CuAssertPtrEquals(tc, NULL, rt_find("catapult")); CuAssertPtrEquals(tc, NULL, rt_find("catapult"));
rtype = rt_get_or_create("catapult"); rtype = rt_get_or_create("catapult");