]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
test_utils: Reset all locale related entries
authorTobias Stoeckmann <tobias@stoeckmann.org>
Fri, 30 May 2025 15:32:02 +0000 (17:32 +0200)
committerTobias Stoeckmann <tobias@stoeckmann.org>
Fri, 30 May 2025 15:32:02 +0000 (17:32 +0200)
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 <tobias@stoeckmann.org>
test_utils/test_main.c

index 98f77b671a8876af5a229f2084a2e1ca31f988ae..afd077fc6d0bf4408e46661c9a1b2cc35c2fd615 100644 (file)
@@ -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",