From e0df40536771e556475211d677dc028114b7a43c Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 18 Nov 2026 11:18:40 +0100 Subject: [PATCH 7/7] util: restore the locale when parsing a float option fails GNUNET_CONFIGURATION_get_value_float returned early on a malformed value and left the calling thread in the "C" numeric locale; neither it nor set_value_float freed the locale object. Fixes #12173 Assisted by LLM --- src/lib/util/configuration.c | 25 ++++++++++------ src/lib/util/test_configuration.c | 47 +++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/src/lib/util/configuration.c b/src/lib/util/configuration.c index 3a1d51314..5c119295d 100644 --- a/src/lib/util/configuration.c +++ b/src/lib/util/configuration.c @@ -2479,12 +2479,16 @@ GNUNET_CONFIGURATION_set_value_float (struct GNUNET_CONFIGURATION_Handle *cfg, const locale_t cl = newlocale (LC_NUMERIC_MASK, "C", (locale_t) 0); - locale_t old_locale = uselocale (cl); + locale_t old_locale; + + GNUNET_assert ((locale_t) 0 != cl); + old_locale = uselocale (cl); GNUNET_snprintf (s, 64, "%f", (double) number); uselocale (old_locale); + freelocale (cl); GNUNET_CONFIGURATION_set_value_string (cfg, section, option, @@ -2513,14 +2517,19 @@ GNUNET_CONFIGURATION_get_value_float ( const locale_t cl = newlocale (LC_NUMERIC_MASK, "C", (locale_t) 0); - locale_t old_locale = uselocale (cl); - - if (1 != sscanf (e->val, - "%f%1s", - number, - dummy)) - return GNUNET_SYSERR; + locale_t old_locale; + int n; + + GNUNET_assert ((locale_t) 0 != cl); + old_locale = uselocale (cl); + n = sscanf (e->val, + "%f%1s", + number, + dummy); uselocale (old_locale); + freelocale (cl); + if (1 != n) + return GNUNET_SYSERR; } return GNUNET_OK; } diff --git a/src/lib/util/test_configuration.c b/src/lib/util/test_configuration.c index b340373de..23ab25ace 100644 --- a/src/lib/util/test_configuration.c +++ b/src/lib/util/test_configuration.c @@ -1177,6 +1177,52 @@ testParseLines (void) } +/** + * Test that parsing a float option leaves the thread's locale alone. + */ +static int +testFloatLocale (void) +{ + struct GNUNET_CONFIGURATION_Handle *fcfg; + locale_t before = uselocale ((locale_t) 0); + float f; + int ret = 0; + + fcfg = GNUNET_CONFIGURATION_create (GNUNET_OS_project_data_gnunet ()); + GNUNET_CONFIGURATION_set_value_float (fcfg, + "f", + "set", + 1.5f); + GNUNET_CONFIGURATION_set_value_string (fcfg, + "f", + "bad", + "1.5x"); + if ( (GNUNET_OK != + GNUNET_CONFIGURATION_get_value_float (fcfg, + "f", + "set", + &f)) || + (1.5f != f) || + (GNUNET_SYSERR != + GNUNET_CONFIGURATION_get_value_float (fcfg, + "f", + "bad", + &f)) ) + { + GNUNET_break (0); + ret = 1; + } + if (before != uselocale ((locale_t) 0)) + { + fprintf (stderr, + "Parsing a float option changed the thread's locale\n"); + ret = 1; + } + GNUNET_CONFIGURATION_destroy (fcfg); + return ret; +} + + int main (int argc, char *argv[]) { @@ -1191,6 +1237,7 @@ main (int argc, char *argv[]) failureCount += testExpandRecursive (); failureCount += testSerializeInaccessible (); failureCount += testParseLines (); + failureCount += testFloatLocale (); if (failureCount > 0) goto error; -- 2.47.3