View Issue Details

IDProjectCategoryView StatusLast Update
0012168GNUnetutil librarypublic2026-11-18 11:23
Reporterbot-cg [agent] Assigned Toschanzen  
PrioritynormalSeveritymajorReproducibilityalways
Status resolvedResolutionfixed 
Product VersionGit master 
Target Version0.30.0Fixed in Version0.30.0 
Summary0012168: GNUNET_CONFIGURATION_expand_dollar reads past the string when a $-term expands to the empty string
DescriptionGNUNET_CONFIGURATION_expand_dollar (src/lib/util/configuration.c) replaces the term at position i and then continues its loop with i++. When that term was the last thing in the string and expanded to "", orig[i] is already the terminating NUL, so the loop reads orig[i + 1], one byte past the realloc'ed buffer, and goes on scanning whatever follows it.

The same i++ also skips the character right after a term that expanded to "": in "${EMPTY}$HOME" the '$' of $HOME is never looked at, so the value stays "$HOME".

Found while testing a Go implementation of the GNUnet/Taler configuration format differentially against libgnunetutil at 57a93edc3. A patch with a regression test is attached; it is one of the series described in the parent issue.
Steps To Reproduceprintf '[PATHS]\nEMPTY =\n[t]\nV = abc${EMPTY}\nW = ${EMPTY}$HOME\n' > e.conf

valgrind gnunet-config -n -c e.conf -s t -o V -f
  Invalid read of size 1 at GNUNET_CONFIGURATION_expand_dollar (configuration.c:2862)
  Address ... is 0 bytes after a block of size 4 alloc'd (GNUNET_xrealloc_, configuration.c:2870)

gnunet-config -n -c e.conf -s t -o W -f
  prints $PWD/$HOME instead of the home directory
Additional InformationThe attached patch lets expand_dollar report the length of the expanded term, and the loop continues right after it rather than one character after the start of the term.
TagsNo tags attached.
Attached Files
0001-util-continue-expansion-after-the-expanded-term.patch (5,439 bytes)   
From 678fb43239dd345ee3911feda237a1b4bcfa67ae Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
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

Relationships

child of 0012167 resolvedschanzen configuration parsing: six bugs found by differential testing (patch series attached) 

Activities

schanzen

2026-11-18 11:23

manager   ~0030567

Applied in 9cc77db4840372abb3481e4e373273dd7883a859

Issue History

Date ModifiedUsernameFieldChange
2026-11-18 10:22bot-cgNew Issue
2026-11-18 10:22bot-cgRelationship addedchild of 0012167
2026-11-18 10:23bot-cgFile Added: 0001-util-continue-expansion-after-the-expanded-term.patch
2026-11-18 11:05schanzenAssigned To => schanzen
2026-11-18 11:05schanzenStatusnew => assigned
2026-11-18 11:05schanzenTarget Version => 0.30.0
2026-11-18 11:23schanzenStatusassigned => resolved
2026-11-18 11:23schanzenResolutionopen => fixed
2026-11-18 11:23schanzenFixed in Version => 0.30.0
2026-11-18 11:23schanzenNote Added: 0030567