From 93eca32fbdeca07afdf0050d8591d6a6308bf16a Mon Sep 17 00:00:00 2001 From: Jamiras <32680403+Jamiras@users.noreply.github.com> Date: Sat, 21 Dec 2024 07:41:25 -0700 Subject: [PATCH] report 0x0xADDR as invalid (#388) --- src/rcheevos/memref.c | 7 ++++++- src/rcheevos/richpresence.c | 9 +++++++++ test/rcheevos/test_operand.c | 3 +++ test/rcheevos/test_richpresence.c | 15 +++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/rcheevos/memref.c b/src/rcheevos/memref.c index 70622bfb..ca94077a 100644 --- a/src/rcheevos/memref.c +++ b/src/rcheevos/memref.c @@ -268,7 +268,12 @@ int rc_parse_memref(const char** memaddr, uint8_t* size, uint32_t* address) { /* case 'y': case 'Y': 64 bit? */ /* case 'z': case 'Z': 128 bit? */ - case '0': case '1': case '2': case '3': case '4': + case '0': + if (*aux == 'x') /* user mistyped an extra 0x: 0x0xabcd */ + return RC_INVALID_MEMORY_OPERAND; + /* fallthrough */ + + case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': diff --git a/src/rcheevos/richpresence.c b/src/rcheevos/richpresence.c index a9d9b2f0..8539aec6 100644 --- a/src/rcheevos/richpresence.c +++ b/src/rcheevos/richpresence.c @@ -598,11 +598,20 @@ void rc_parse_richpresence_internal(rc_richpresence_t* self, const char* script, *nextdisplay = rc_parse_richpresence_display_internal(ptr + 1, endline, parse, firstlookup); if (parse->offset < 0) return; + trigger = &((*nextdisplay)->trigger); rc_parse_trigger_internal(trigger, &line, parse); if (parse->offset < 0) return; + + if (line != ptr) { + /* incomplete read */ + parse->offset = RC_INVALID_OPERATOR; + return; + } + (*nextdisplay)->has_required_hits = parse->has_required_hits; + if (parse->buffer) nextdisplay = &((*nextdisplay)->next); } diff --git a/test/rcheevos/test_operand.c b/test/rcheevos/test_operand.c index f46a2135..b8588f32 100644 --- a/test/rcheevos/test_operand.c +++ b/test/rcheevos/test_operand.c @@ -187,6 +187,9 @@ static void test_parse_memory_references() { TEST_PARAMS4(test_parse_operand, "0xHABCD", RC_OPERAND_ADDRESS, RC_MEMSIZE_8_BITS, 0xABCDU); TEST_PARAMS4(test_parse_operand, "0xhabcd", RC_OPERAND_ADDRESS, RC_MEMSIZE_8_BITS, 0xABCDU); TEST_PARAMS4(test_parse_operand, "fFABCD", RC_OPERAND_ADDRESS, RC_MEMSIZE_FLOAT, 0xABCDU); + + /* doubled up prefix */ + TEST_PARAMS3(test_parse_error_operand, "0x0xH1234", 0, RC_INVALID_MEMORY_OPERAND); } static void test_parse_delta_memory_references() { diff --git a/test/rcheevos/test_richpresence.c b/test/rcheevos/test_richpresence.c index 691a607c..62649e52 100644 --- a/test/rcheevos/test_richpresence.c +++ b/test/rcheevos/test_richpresence.c @@ -391,6 +391,15 @@ static void test_conditional_display_unnecessary_measured_indirect() { assert_richpresence_output(richpresence, &memory, "True"); } +static void test_conditional_display_invalid() { + int lines_read = 0; + ASSERT_NUM_EQUALS(rc_richpresence_size_lines("Display:\n?I:0x0x0000=1?True\nFalse\n", &lines_read), RC_INVALID_MEMORY_OPERAND); + ASSERT_NUM_EQUALS(lines_read, 2); + + ASSERT_NUM_EQUALS(rc_richpresence_size_lines("Display:\n?0x0000=1 0x0001=2?True\nFalse\n", &lines_read), RC_INVALID_OPERATOR); + ASSERT_NUM_EQUALS(lines_read, 2); +} + static void test_macro_value_adjusted_negative() { uint8_t ram[] = { 0x00, 0x12, 0x34, 0xAB, 0x56 }; memory_t memory; @@ -527,6 +536,10 @@ static void test_macro_value_remember_recall() { assert_richpresence_output(richpresence, &memory, "Result is 3"); } +static void test_macro_value_invalid() { + ASSERT_NUM_EQUALS(rc_richpresence_size("Format:Points\nFormatType=VALUE\n\nDisplay:\n@Points(0x0x0001) Points"), RC_INVALID_MEMORY_OPERAND); +} + static void test_macro_hundreds() { uint8_t ram[] = { 0x00, 0x12, 0x34, 0xAB, 0x56 }; memory_t memory; @@ -1343,6 +1356,7 @@ void test_richpresence(void) { TEST(test_conditional_display_indirect); TEST(test_conditional_display_unnecessary_measured); TEST(test_conditional_display_unnecessary_measured_indirect); + TEST(test_conditional_display_invalid); /* value macros */ TEST(test_macro_value); @@ -1356,6 +1370,7 @@ void test_richpresence(void) { TEST(test_macro_value_divide_by_zero); TEST(test_macro_value_divide_by_self); TEST(test_macro_value_remember_recall); + TEST(test_macro_value_invalid); /* hundreds macro */ TEST(test_macro_hundreds);