From a51fd5156082071d81bf871d996914b4cffda585 Mon Sep 17 00:00:00 2001 From: Solar Designer Date: Sun, 28 Apr 2024 18:41:17 +0200 Subject: [PATCH] Dynamic: Avoid copying of constant strings without backslash escapes It appears OK for dynamic_Demangle() to return a pointer to its input string, rather than to a copy, because the only uses are: 1. In dynamic_parser.c, on pointers to config file lines, and we pre-read the entire config file (and includes) into a linked list of separate line structs, and we don't de-allocate those. 2. In dynamic_compiler.c: #define APP_CFUNC(N), we only pass the returned pointer to dyna_helper_appendn(), which does not store the pointer. Mostly fixes #5465 (memory leak) --- src/dynamic_utils.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/dynamic_utils.c b/src/dynamic_utils.c index b2eb902f786..e488da3e20b 100644 --- a/src/dynamic_utils.c +++ b/src/dynamic_utils.c @@ -101,13 +101,23 @@ static int ishexdigit(char c) { return 1; return 0; } -// Only called at load time, so does not have to be overly optimal + +// Was only called at load time, so did not have to be overly optimal and could +// leak memory. +// Now is also called as fallback by the RDP dynamic compiler format, so should +// avoid leaking memory, but in non-trivial cases still does. char *dynamic_Demangle(char *Line, int *Len) { char *tmp, *cp, *cp2, digits[3]; - if (!Line || !strlen(Line)) { - if (Len) *Len = 0; - return str_alloc_copy(""); + if (!Line) { + if (Len) + *Len = 0; + return ""; + } + if (!strchr(Line, '\\')) { + if (Len) + *Len = strlen(Line); + return Line; } tmp = str_alloc_copy(Line); cp = tmp;