forked from github/server
resource names are max 23 bytes long.
fix rt_find nul-termination bug
This commit is contained in:
parent
c45c211dcf
commit
2451a8f637
4 changed files with 18 additions and 17 deletions
2
clibs
2
clibs
|
@ -1 +1 @@
|
||||||
Subproject commit d286006a28c8aa7cd70ed7fd4cd172b50ade9727
|
Subproject commit 2a55c27fedec76845cf82c758b7b7c3fa649c286
|
|
@ -212,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);
|
||||||
|
|
||||||
|
|
|
@ -60,7 +60,7 @@ static critbit_tree cb_resources;
|
||||||
luxury_type *luxurytypes;
|
luxury_type *luxurytypes;
|
||||||
potion_type *potiontypes;
|
potion_type *potiontypes;
|
||||||
|
|
||||||
#define RTYPENAMELEN 16
|
#define RTYPENAMELEN 24
|
||||||
typedef struct rt_entry {
|
typedef struct rt_entry {
|
||||||
char key[RTYPENAMELEN];
|
char key[RTYPENAMELEN];
|
||||||
struct resource_type *value;
|
struct resource_type *value;
|
||||||
|
@ -176,10 +176,10 @@ static void rt_register(resource_type * rtype)
|
||||||
size_t len = strlen(name);
|
size_t len = strlen(name);
|
||||||
rt_entry ent;
|
rt_entry ent;
|
||||||
|
|
||||||
if (len > RTYPENAMELEN) {
|
if (len >= RTYPENAMELEN) {
|
||||||
log_error("resource names may not be longer than %d bytes: %s",
|
log_error("resource name is longer than %d bytes: %s",
|
||||||
RTYPENAMELEN, name);
|
RTYPENAMELEN-1, name);
|
||||||
len = RTYPENAMELEN;
|
len = RTYPENAMELEN-1;
|
||||||
}
|
}
|
||||||
ent.value = rtype;
|
ent.value = rtype;
|
||||||
memset(ent.key, 0, RTYPENAMELEN);
|
memset(ent.key, 0, RTYPENAMELEN);
|
||||||
|
@ -389,15 +389,16 @@ 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;
|
||||||
size_t len = strlen(name);
|
size_t len = strlen(name);
|
||||||
|
|
||||||
if (len > RTYPENAMELEN) {
|
if (len >= RTYPENAMELEN) {
|
||||||
log_error("resource name is longer than $d bytes: %s",
|
log_error("resource name is longer than %d bytes: %s",
|
||||||
RTYPENAMELEN, name);
|
RTYPENAMELEN-1, name);
|
||||||
len = RTYPENAMELEN;
|
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;
|
rt_entry *ent = (rt_entry *)match;
|
||||||
return ent->value;
|
return ent->value;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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");
|
||||||
|
|
Loading…
Reference in a new issue