resource names are max 23 bytes long.

fix rt_find nul-termination bug
This commit is contained in:
Enno Rehling 2017-09-18 17:56:27 +02:00
parent c45c211dcf
commit 2451a8f637
4 changed files with 18 additions and 17 deletions

2
clibs

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

View File

@ -212,7 +212,7 @@ typedef struct eq_entry {
equipment *get_equipment(const char *eqname)
{
const char *match;
char *match;
assert(strlen(eqname) < EQNAMELEN);

View File

@ -60,7 +60,7 @@ static critbit_tree cb_resources;
luxury_type *luxurytypes;
potion_type *potiontypes;
#define RTYPENAMELEN 16
#define RTYPENAMELEN 24
typedef struct rt_entry {
char key[RTYPENAMELEN];
struct resource_type *value;
@ -176,10 +176,10 @@ static void rt_register(resource_type * rtype)
size_t len = strlen(name);
rt_entry ent;
if (len > RTYPENAMELEN) {
log_error("resource names may not be longer than %d bytes: %s",
RTYPENAMELEN, name);
len = RTYPENAMELEN;
if (len >= RTYPENAMELEN) {
log_error("resource name is longer than %d bytes: %s",
RTYPENAMELEN-1, name);
len = RTYPENAMELEN-1;
}
ent.value = rtype;
memset(ent.key, 0, RTYPENAMELEN);
@ -389,15 +389,16 @@ const potion_type *resource2potion(const resource_type * rtype)
resource_type *rt_find(const char *name)
{
void * match;
char *match;
size_t len = strlen(name);
if (len > RTYPENAMELEN) {
log_error("resource name is longer than $d bytes: %s",
RTYPENAMELEN, name);
len = RTYPENAMELEN;
if (len >= RTYPENAMELEN) {
log_error("resource name is longer than %d bytes: %s",
RTYPENAMELEN-1, name);
return NULL;
}
if (cb_find_prefix(&cb_resources, name, len, &match, 1, 0)) {
match = cb_find_str(&cb_resources, name);
if (match) {
rt_entry *ent = (rt_entry *)match;
return ent->value;
}

View File

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