From: ArtSin Date: Tue, 22 Oct 2024 10:24:45 +0000 (+0400) Subject: mprintf: treat `%o` as unsigned, add tests for `%o`, `%x`, `%X` X-Git-Tag: curl-8_11_0~76 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0325e1b9b22656e367af729f65d39eb00f1d6808;p=thirdparty%2Fcurl.git mprintf: treat `%o` as unsigned, add tests for `%o`, `%x`, `%X` `%x` and `%X` were already treated as unsigned, but `%o` was not, even though it was used with unsigned numbers. Closes #15348 --- diff --git a/.mailmap b/.mailmap index 9705bf0880..56a1e7c4e2 100644 --- a/.mailmap +++ b/.mailmap @@ -111,3 +111,4 @@ Michael Osipov Christian Weisgerber Moritz Buhl Aki Sakurai <75532970+AkiSakurai@users.noreply.github.com> +Sinkevich Artem diff --git a/lib/mprintf.c b/lib/mprintf.c index 11551a5d7a..6576f3ebf9 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -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) diff --git a/tests/data/test557 b/tests/data/test557 index 1963774946..9d86c0ac21 100644 --- a/tests/data/test557 +++ b/tests/data/test557 @@ -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! diff --git a/tests/libtest/lib557.c b/tests/libtest/lib557.c index d715fc575c..d57f644a6b 100644 --- a/tests/libtest/lib557.c +++ b/tests/libtest/lib557.c @@ -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)