]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
mprintf: treat `%o` as unsigned, add tests for `%o`, `%x`, `%X`
authorArtSin <artsin666@gmail.com>
Tue, 22 Oct 2024 10:24:45 +0000 (14:24 +0400)
committerDaniel Stenberg <daniel@haxx.se>
Thu, 24 Oct 2024 08:17:24 +0000 (10:17 +0200)
`%x` and `%X` were already treated as unsigned, but `%o` was not, even
though it was used with unsigned numbers.

Closes #15348

.mailmap
lib/mprintf.c
tests/data/test557
tests/libtest/lib557.c

index 9705bf088003abbe0c289251b31e0835875346d6..56a1e7c4e268a8aba9ea6c27ea91996587201617 100644 (file)
--- a/.mailmap
+++ b/.mailmap
@@ -111,3 +111,4 @@ Michael Osipov <michael.osipov@siemens.com> <michael-o@users.sf.net>
 Christian Weisgerber <naddy@mips.inka.de> <curl-library@lists.haxx.se>
 Moritz Buhl <git@moritzbuhl.de>
 Aki Sakurai <75532970+AkiSakurai@users.noreply.github.com>
+Sinkevich Artem <artsin666@gmail.com>
index 11551a5d7a10adc0732b8913391f5a5165d3ccb6..6576f3ebf9a395ee3f14a4d59e4e5b53c4d339e6 100644 (file)
@@ -456,12 +456,12 @@ static int parsefmt(const char *format,
         break;
       case 'o':
         if(flags & FLAGS_LONGLONG)
-          type = FORMAT_LONGLONG;
+          type = FORMAT_LONGLONGU;
         else if(flags & FLAGS_LONG)
-          type = FORMAT_LONG;
+          type = FORMAT_LONGU;
         else
-          type = FORMAT_INT;
-        flags |= FLAGS_OCTAL;
+          type = FORMAT_INTU;
+        flags |= FLAGS_OCTAL|FLAGS_UNSIGNED;
         break;
       case 'x':
         if(flags & FLAGS_LONGLONG)
index 1963774946ff52c2fc76ceb2029e6b5a14019bd2..9d86c0ac2182d6b12d517d31c5ef27e99c615a2f 100644 (file)
@@ -41,6 +41,7 @@ All curl_mprintf() signed long tests OK!
 All curl_mprintf() curl_off_t tests OK!
 All curl_mprintf() strings tests OK!
 All float strings tests OK!
+All curl_mprintf() octal & hexadecimal tests OK!
 </stdout>
 </verify>
 
index d715fc575c7399446c8fddfacb1afe92bfd669e5..d57f644a6b81479f86bf0bc3965d2866e7a48f90 100644 (file)
@@ -1439,6 +1439,48 @@ static int test_float_formatting(void)
 
   return errors;
 }
+
+static int test_oct_hex_formatting(void)
+{
+  int errors = 0;
+  char buf[256];
+
+  curl_msnprintf(buf, sizeof(buf), "%ho %hx %hX", 0xFA10U, 0xFA10U, 0xFA10U);
+  errors += string_check(buf, "175020 fa10 FA10");
+
+#if (SIZEOF_INT == 2)
+  curl_msnprintf(buf, sizeof(buf), "%o %x %X", 0xFA10U, 0xFA10U, 0xFA10U);
+  errors += string_check(buf, "175020 fa10 FA10");
+#elif (SIZEOF_INT == 4)
+  curl_msnprintf(buf, sizeof(buf), "%o %x %X",
+                 0xFABC1230U, 0xFABC1230U, 0xFABC1230U);
+  errors += string_check(buf, "37257011060 fabc1230 FABC1230");
+#elif (SIZEOF_INT == 8)
+  curl_msnprintf(buf, sizeof(buf), "%o %x %X",
+                 0xFABCDEF123456780U, 0xFABCDEF123456780U, 0xFABCDEF123456780U);
+  errors += string_check(buf, "1752746757044321263600 fabcdef123456780 FABCDEF123456780");
+#endif
+
+#if (SIZEOF_LONG == 2)
+  curl_msnprintf(buf, sizeof(buf), "%lo %lx %lX", 0xFA10UL, 0xFA10UL, 0xFA10UL);
+  errors += string_check(buf, "175020 fa10 FA10");
+#elif (SIZEOF_LONG == 4)
+  curl_msnprintf(buf, sizeof(buf), "%lo %lx %lX",
+                 0xFABC1230UL, 0xFABC1230UL, 0xFABC1230UL);
+  errors += string_check(buf, "37257011060 fabc1230 FABC1230");
+#elif (SIZEOF_LONG == 8)
+  curl_msnprintf(buf, sizeof(buf), "%lo %lx %lX",
+                 0xFABCDEF123456780UL, 0xFABCDEF123456780UL, 0xFABCDEF123456780UL);
+  errors += string_check(buf, "1752746757044321263600 fabcdef123456780 FABCDEF123456780");
+#endif
+
+  if(!errors)
+    printf("All curl_mprintf() octal & hexadecimal tests OK!\n");
+  else
+    printf("Some curl_mprintf() octal & hexadecimal tests Failed!\n");
+
+  return errors;
+}
 /* !checksrc! enable LONGLINE */
 
 static int test_return_codes(void)
@@ -1507,6 +1549,8 @@ CURLcode test(char *URL)
 
   errors += test_float_formatting();
 
+  errors += test_oct_hex_formatting();
+
   errors += test_return_codes();
 
   if(errors)