From 8baf0f6340c92a542d070a9490d286a4009b8b29 Mon Sep 17 00:00:00 2001 From: Jamiras Date: Wed, 4 Mar 2020 16:36:33 -0700 Subject: [PATCH] prevent buffer overflow when encountering an unknown macro --- cheevos-new/cheevos.c | 6 +++--- deps/rcheevos/src/rcheevos/richpresence.c | 18 ++++++++++++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/cheevos-new/cheevos.c b/cheevos-new/cheevos.c index d9d7dacc36..3520c94427 100644 --- a/cheevos-new/cheevos.c +++ b/cheevos-new/cheevos.c @@ -666,7 +666,7 @@ static int rcheevos_parse(const char* json) lboard->format = rc_parse_format(lboard->info->format); } - if (rcheevos_locals.patchdata.richpresence_script) + if (rcheevos_locals.patchdata.richpresence_script && *rcheevos_locals.patchdata.richpresence_script) { int buffer_size = rc_richpresence_size(rcheevos_locals.patchdata.richpresence_script); if (buffer_size <= 0) @@ -681,8 +681,8 @@ static int rcheevos_parse(const char* json) } else { - char *buffer = (char*)malloc(buffer_size); - rcheevos_locals.richpresence.richpresence = rc_parse_richpresence(buffer, rcheevos_locals.patchdata.richpresence_script, NULL, 0); + char *rp_buffer = (char*)malloc(buffer_size); + rcheevos_locals.richpresence.richpresence = rc_parse_richpresence(rp_buffer, rcheevos_locals.patchdata.richpresence_script, NULL, 0); } rcheevos_locals.richpresence.evaluation[0] = '\0'; diff --git a/deps/rcheevos/src/rcheevos/richpresence.c b/deps/rcheevos/src/rcheevos/richpresence.c index 0e8b4e70df..1f9d7c3470 100644 --- a/deps/rcheevos/src/rcheevos/richpresence.c +++ b/deps/rcheevos/src/rcheevos/richpresence.c @@ -8,7 +8,8 @@ /* special formats only used by rc_richpresence_display_part_t.display_type. must not overlap other RC_FORMAT values */ enum { RC_FORMAT_STRING = 101, - RC_FORMAT_LOOKUP = 102 + RC_FORMAT_LOOKUP = 102, + RC_FORMAT_UNKNOWN_MACRO = 103 }; static const char* rc_parse_line(const char* line, const char** end) { @@ -161,10 +162,15 @@ static rc_richpresence_display_t* rc_parse_richpresence_display_internal(const c *next = part; next = &part->next; - ptr = line; + /* find the closing parenthesis */ + while (ptr < endline && *ptr != ')') + ++ptr; + if (*ptr == ')') + ++ptr; - part->display_type = RC_FORMAT_STRING; - part->text = rc_alloc_str(parse, "[Unknown macro]", 15); + /* assert: the allocated string is going to be smaller than the memory used for the parameter of the macro */ + part->display_type = RC_FORMAT_UNKNOWN_MACRO; + part->text = rc_alloc_str(parse, line, ptr - line); } } } @@ -422,6 +428,10 @@ int rc_evaluate_richpresence(rc_richpresence_t* richpresence, char* buffer, unsi } break; + case RC_FORMAT_UNKNOWN_MACRO: + chars = snprintf(ptr, buffersize, "[Unknown macro]%s", part->text); + break; + default: value = rc_evaluate_value(&part->value, peek, peek_ud, L); chars = rc_format_value(ptr, buffersize, value, part->display_type);