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
This commit is contained in:
ArtSin 2024-10-22 14:24:45 +04:00 committed by Daniel Stenberg
parent 7ca164faba
commit 0325e1b9b2
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
4 changed files with 50 additions and 4 deletions

View File

@ -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>

View 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)

View 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>

View 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)