From 678fb43239dd345ee3911feda237a1b4bcfa67ae Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 18 Nov 2026 11:15:15 +0100 Subject: [PATCH 1/7] util: continue $-expansion after the expanded term GNUNET_CONFIGURATION_expand_dollar always resumed one character after the start of a term, so it read past the end of the string when the last term expanded to "", and skipped a '$' right after a term that expanded to "". Fixes #12168 Assisted by LLM --- src/lib/util/configuration.c | 24 ++++++++++--- src/lib/util/test_configuration.c | 56 +++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 4 deletions(-) diff --git a/src/lib/util/configuration.c b/src/lib/util/configuration.c index 3afd8c3e4..7ecf76044 100644 --- a/src/lib/util/configuration.c +++ b/src/lib/util/configuration.c @@ -2693,12 +2693,16 @@ GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg, * @param cfg configuration to use for path expansion * @param orig string to $-expand (will be freed!) * @param depth recursion depth, used to detect recursive expansions + * @param[out] skip set to the length of the expanded term at the start + * of the result, or to 1 if the term was not expanded, so that the + * caller can continue after it * @return $-expanded string, never NULL unless @a orig was NULL */ static char * expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg, char *orig, - unsigned int depth) + unsigned int depth, + size_t *skip) { char *prefix; char *result; @@ -2711,7 +2715,9 @@ expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg, char erased_char; char *erased_pos; size_t len; + size_t def_skip; + *skip = 1; if (NULL == orig) return NULL; if (depth > 128) @@ -2814,7 +2820,8 @@ expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg, /* try default */ def = expand_dollar (cfg, def, - depth + 1); + depth + 1, + &def_skip); env = def; } if (NULL == env) @@ -2840,6 +2847,7 @@ expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg, prefix[len - 1] = erased_char; prefix[len] = '\0'; } + *skip = strlen (prefix); result = GNUNET_malloc (strlen (prefix) + strlen (post) + 1); strcpy (result, prefix); strcat (result, post); @@ -2858,18 +2866,26 @@ GNUNET_CONFIGURATION_expand_dollar ( char *dup; size_t i; size_t len; + size_t skip; - for (i = 0; '\0' != orig[i]; i++) + i = 0; + while ('\0' != orig[i]) { if ('$' != orig[i]) + { + i++; continue; + } dup = GNUNET_strdup (orig + i); - dup = expand_dollar (cfg, dup, 0); + dup = expand_dollar (cfg, dup, 0, &skip); GNUNET_assert (NULL != dup); /* make compiler happy */ len = strlen (dup) + 1; orig = GNUNET_realloc (orig, i + len); GNUNET_memcpy (orig + i, dup, len); GNUNET_free (dup); + /* The expansion is final; continue right after it, which may well be + the end of the string or another '$' if it expanded to "". */ + i += skip; } return orig; } diff --git a/src/lib/util/test_configuration.c b/src/lib/util/test_configuration.c index 0d86b886a..12067a91f 100644 --- a/src/lib/util/test_configuration.c +++ b/src/lib/util/test_configuration.c @@ -960,6 +960,61 @@ testConfigFilenames () } +/** + * Check one $-expansion. + */ +static int +check_expansion (const struct GNUNET_CONFIGURATION_Handle *ecfg, + const char *in, + const char *want) +{ + char *out = GNUNET_CONFIGURATION_expand_dollar (ecfg, + GNUNET_strdup (in)); + int ret = 0; + + if (0 != strcmp (out, + want)) + { + fprintf (stderr, + "Expanding `%s' gave `%s', wanted `%s'\n", + in, + out, + want); + ret = 1; + } + GNUNET_free (out); + return ret; +} + + +/** + * Test $-expansion of terms that expand to the empty string. + */ +static int +testExpandEmpty (void) +{ + struct GNUNET_CONFIGURATION_Handle *ecfg; + int ret = 0; + + ecfg = GNUNET_CONFIGURATION_create (GNUNET_OS_project_data_gnunet ()); + GNUNET_CONFIGURATION_set_value_string (ecfg, + "PATHS", + "EMPTY", + ""); + GNUNET_CONFIGURATION_set_value_string (ecfg, + "PATHS", + "B", + "/b"); + ret += check_expansion (ecfg, "abc${EMPTY}", "abc"); + ret += check_expansion (ecfg, "$EMPTY", ""); + ret += check_expansion (ecfg, "${EMPTY}$B", "/b"); + ret += check_expansion (ecfg, "x${EMPTY}${EMPTY}y$B/z", "xy/b/z"); + ret += check_expansion (ecfg, "$NOT_SET_ANYWHERE/$B", "$NOT_SET_ANYWHERE//b"); + GNUNET_CONFIGURATION_destroy (ecfg); + return ret; +} + + int main (int argc, char *argv[]) { @@ -970,6 +1025,7 @@ main (int argc, char *argv[]) failureCount += testDiagnostics (); failureCount += testDiagnosticsInlineSecret (); failureCount += testClean (); + failureCount += testExpandEmpty (); if (failureCount > 0) goto error; -- 2.47.3