Skip to content

Commit

Permalink
detect invalid lookup key
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamiras committed Mar 23, 2019
1 parent dc79017 commit 855a820
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/rcheevos/richpresence.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ const char* rc_parse_richpresence_lookup(rc_richpresence_lookup_t* lookup, const
const char* line;
const char* endline;
const char* defaultlabel = 0;
char* endptr = 0;
unsigned key;
int chars;

Expand Down Expand Up @@ -212,9 +213,14 @@ const char* rc_parse_richpresence_lookup(rc_richpresence_lookup_t* lookup, const
}

if (number[0] == '0' && number[1] == 'x')
key = strtoul(&number[2], 0, 16);
key = strtoul(&number[2], &endptr, 16);
else
key = strtoul(&number[0], 0, 10);
key = strtoul(&number[0], &endptr, 10);

if (*endptr && !isspace(*endptr)) {
*ret = RC_INVALID_CONST_OPERAND;
return nextline;
}

item = RC_ALLOC(rc_richpresence_lookup_item_t, buffer, ret, scratch);
item->value = key;
Expand Down Expand Up @@ -266,6 +272,8 @@ void rc_parse_richpresence_internal(rc_richpresence_t* self, int* ret, void* buf
nextlookup = &lookup->next;

nextline = rc_parse_richpresence_lookup(lookup, nextline, ret, buffer, scratch, L, funcs_ndx);
if (*ret < 0)
return;

} else if (strncmp(line, "Format:", 7) == 0) {
line += 7;
Expand Down
25 changes: 25 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3181,6 +3181,31 @@ static void test_richpresence(void) {
assert(result == 6);
}

{
/*------------------------------------------------------------------------
TestLookupInvalid
------------------------------------------------------------------------*/
int result;

result = rc_richpresence_size("Lookup:Location\nOx0=Zero\n1=One\n\nDisplay:\nAt @Location(0xH0000)");
assert(result == RC_INVALID_CONST_OPERAND);

result = rc_richpresence_size("Lookup:Location\n0xO=Zero\n1=One\n\nDisplay:\nAt @Location(0xH0000)");
assert(result == RC_INVALID_CONST_OPERAND);

result = rc_richpresence_size("Lookup:Location\nZero=Zero\n1=One\n\nDisplay:\nAt @Location(0xH0000)");
assert(result == RC_INVALID_CONST_OPERAND);

result = rc_richpresence_size("Lookup:Location\n0=Zero\n1=One\n\nDisplay:\nAt @Location");
assert(result == RC_MISSING_VALUE);

result = rc_richpresence_size("Lookup:Location\n0=Zero\n1=One\n\nDisplay:\nAt @Location()");
assert(result == RC_INVALID_MEMORY_OPERAND);

result = rc_richpresence_size("Lookup:Location\n0=Zero\n1=One\n\nDisplay:\nAt @Location(Zero)");
assert(result == RC_INVALID_MEMORY_OPERAND);
}

{
/*------------------------------------------------------------------------
TestRandomTextBetweenSections
Expand Down

0 comments on commit 855a820

Please sign in to comment.