From: Tobias Stoeckmann Date: Fri, 30 May 2025 15:32:02 +0000 (+0200) Subject: test_utils: Reset all locale related entries X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e70e24a73871e4e23f068c7e556e2cb6106f1f36;p=thirdparty%2Flibarchive.git test_utils: Reset all locale related entries Reset current locale settings through setlocale and also all environment variables which might affect test cases which spawn children through systemf which in turn would call setlocale on their own, e.g. bsdtar. Signed-off-by: Tobias Stoeckmann --- diff --git a/test_utils/test_main.c b/test_utils/test_main.c index 98f77b671..afd077fc6 100644 --- a/test_utils/test_main.c +++ b/test_utils/test_main.c @@ -3554,6 +3554,59 @@ test_summarize(int failed, int skips_num) memset(failed_lines, 0, sizeof(failed_lines)); } +/* + * Set or unset environment variable. + */ +static void +set_environment(const char *key, const char *value) +{ + +#if defined(_WIN32) && !defined(__CYGWIN__) + if (!SetEnvironmentVariable(key, value)) { + fprintf(stderr, "SetEnvironmentVariable failed with %d\n", + (int)GetLastError()); + } +#else + if (value == NULL) { + if (unsetenv(key) == -1) + fprintf(stderr, "unsetenv: %s\n", strerror(errno)); + } else { + if (setenv(key, value, 1) == -1) + fprintf(stderr, "setenv: %s\n", strerror(errno)); + } +#endif +} + +/* + * Enforce C locale for (sub)processes. + */ +static void +set_c_locale() +{ + static const char *lcs[] = { + "LC_ADDRESS", + "LC_ALL", + "LC_COLLATE", + "LC_CTYPE", + "LC_IDENTIFICATION", + "LC_MEASUREMENT", + "LC_MESSAGES", + "LC_MONETARY", + "LC_NAME", + "LC_NUMERIC", + "LC_PAPER", + "LC_TELEPHONE", + "LC_TIME", + NULL + }; + size_t i; + + setlocale(LC_ALL, "C"); + set_environment("LANG", "C"); + for (i = 0; lcs[i] != NULL; i++) + set_environment(lcs[i], NULL); +} + /* * Actually run a single test, with appropriate setup and cleanup. */ @@ -3629,7 +3682,7 @@ test_run(int i, const char *tmpdir) exit(1); } /* Explicitly reset the locale before each test. */ - setlocale(LC_ALL, "C"); + set_c_locale(); /* Record the umask before we run the test. */ umask(oldumask = umask(0)); /* @@ -3643,7 +3696,7 @@ test_run(int i, const char *tmpdir) /* Restore umask */ umask(oldumask); /* Reset locale. */ - setlocale(LC_ALL, "C"); + set_c_locale(); /* Reset directory. */ if (!assertChdir(tmpdir)) { fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",