]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
test: promote the win32-repairing systemf wrapper to all systemf calls
authorDustin L. Howett <dustin@howett.net>
Sun, 14 Jun 2026 20:01:00 +0000 (15:01 -0500)
committerDustin L. Howett <dustin@howett.net>
Mon, 15 Jun 2026 20:08:02 +0000 (15:08 -0500)
tar/test/test_option_mtime.c
test_utils/test_main.c

index 75cdb8390bc0c93a9fd586551f96e89f60723df3..8e337a46da8555e9d574d3e2f0f18138bb446e3a 100644 (file)
@@ -6,23 +6,6 @@
  */
 #include "test.h"
 
-#if defined(_WIN32) && !defined(__CYGWIN__)
-/* system() on Windows runs its arguments through CMD.EXE, which has
- * notoriously unfriendly quoting rules. The current best documented way around
- * them is to wrap your *entire commandline* in sacrificial quotes.
- *
- * See CMD.EXE /? for more information. Excerpted here:
- * | Otherwise, old behavior is to see if the first character is
- * | a quote character and if so, strip the leading character and
- * | remove the last quote character on the command line, preserving
- * | any text after the last quote character.
- *
- * Since this test makes heavy use of systemf() with quoted arguments inside
- * the commandline, this macro is unfortunately an easier workaround.
- */
-#define systemf(command, ...) systemf("\"" command "\"", __VA_ARGS__)
-#endif
-
 DEFINE_TEST(test_option_mtime)
 {
        /* Create three files with different mtimes. */
index d2fcb09f9221dbcb5d128dc201c5d7576d1b232c..f5de428f2890a5c9d990f6be1f92c1fa806dc3b0 100644 (file)
@@ -3056,11 +3056,41 @@ systemf(const char *fmt, ...)
        pid_t pid;
 #endif
        va_list ap;
+       size_t off = 0, avail = sizeof(buff);
        int r;
 
+#if defined(_WIN32) && !defined(__CYGWIN__)
+       /* system() on Windows runs its arguments through CMD.EXE, which has
+        * notoriously unfriendly quoting rules. The current best documented way around
+        * them is to wrap your *entire commandline* in sacrificial quotes.
+        *
+        * See CMD.EXE /? for more information. Excerpted here:
+        * | Otherwise, old behavior is to see if the first character is
+        * | a quote character and if so, strip the leading character and
+        * | remove the last quote character on the command line, preserving
+        * | any text after the last quote character.
+        *
+        * Since these tests often make use of systemf() with quoted arguments inside
+        * the commandline, wrap every formatted commandline in quotes.
+        */
+       avail -= 2;
+       off++;
+       buff[0] = '"';
+#endif
+
        va_start(ap, fmt);
-       vsnprintf(buff, sizeof(buff), fmt, ap);
+       r = vsnprintf(buff + off, avail, fmt, ap);
        va_end(ap);
+
+       if (r < 0 || (size_t)r > avail) {
+               return (-1);
+       }
+
+#if defined(_WIN32) && !defined(__CYGWIN__)
+       buff[off + r] = '"';
+       buff[off + r + 1] = '\0';
+#endif
+
        if (verbosity > VERBOSITY_FULL)
                logprintf("Cmd: %s\n", buff);
 #if USE_POSIX_SPAWN