stdlib: Use new parser for scanf %p specifier

This commit is contained in:
Carl Åstholm 2024-09-12 00:26:36 +02:00 committed by Sam Lantinga
parent e109aa09aa
commit 61bc856b04
2 changed files with 26 additions and 29 deletions

View file

@ -305,11 +305,6 @@ static Uint32 StepUTF32(const Uint32 **_str, const size_t slen)
} }
#endif #endif
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOL) || !defined(HAVE_WCSTOL) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD) || !defined(HAVE_STRTOLL) || !defined(HAVE_STRTOULL)
#define SDL_isupperhex(X) (((X) >= 'A') && ((X) <= 'F'))
#define SDL_islowerhex(X) (((X) >= 'a') && ((X) <= 'f'))
#endif
#define UTF8_IsLeadByte(c) ((c) >= 0xC0 && (c) <= 0xF4) #define UTF8_IsLeadByte(c) ((c) >= 0xC0 && (c) <= 0xF4)
#define UTF8_IsTrailingByte(c) ((c) >= 0x80 && (c) <= 0xBF) #define UTF8_IsTrailingByte(c) ((c) >= 0x80 && (c) <= 0xBF)
@ -534,33 +529,25 @@ static size_t SDL_ScanUnsignedLong(const char *text, int count, int radix, unsig
#endif #endif
#ifndef HAVE_VSSCANF #ifndef HAVE_VSSCANF
static size_t SDL_ScanUintPtrT(const char *text, int radix, uintptr_t *valuep) static size_t SDL_ScanUintPtrT(const char *text, uintptr_t *valuep)
{ {
const char *textstart = text; const uintptr_t uintptr_max = ~(uintptr_t)0;
uintptr_t value = 0; unsigned long long value;
bool negative;
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) { size_t len = SDL_ScanUnsignedLongLongInternal(text, 0, 16, &value, &negative);
text += 2; if (negative) {
} if (value == 0 || value > uintptr_max) {
for (;;) { value = uintptr_max;
int v; } else if (value == uintptr_max) {
if (SDL_isdigit((unsigned char)*text)) { value = 1;
v = *text - '0';
} else if (radix == 16 && SDL_isupperhex(*text)) {
v = 10 + (*text - 'A');
} else if (radix == 16 && SDL_islowerhex(*text)) {
v = 10 + (*text - 'a');
} else { } else {
break; value = 0ULL - value;
} }
value *= radix; } else if (value > uintptr_max) {
value += v; value = uintptr_max;
++text;
} }
if (valuep && text > textstart) { *valuep = value;
*valuep = value; return len;
}
return text - textstart;
} }
#endif #endif
@ -1616,7 +1603,7 @@ int SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_li
case 'p': case 'p':
{ {
uintptr_t value = 0; uintptr_t value = 0;
advance = SDL_ScanUintPtrT(text, 16, &value); advance = SDL_ScanUintPtrT(text, &value);
text += advance; text += advance;
if (advance && !suppress) { if (advance && !suppress) {
void **valuep = va_arg(ap, void **); void **valuep = va_arg(ap, void **);

View file

@ -726,6 +726,7 @@ static int SDLCALL stdlib_sscanf(void *arg)
long long_output, expected_long_output; long long_output, expected_long_output;
long long long_long_output, expected_long_long_output; long long long_long_output, expected_long_long_output;
size_t size_output, expected_size_output; size_t size_output, expected_size_output;
uintptr_t uintptr_output, expected_uintptr_output;
char text[128], text2[128]; char text[128], text2[128];
expected_output = output = 123; expected_output = output = 123;
@ -790,6 +791,15 @@ static int SDLCALL stdlib_sscanf(void *arg)
SDLTest_AssertCheck(expected_size_output == size_output, "Check output, expected: %zu, got: %zu", expected_size_output, size_output); SDLTest_AssertCheck(expected_size_output == size_output, "Check output, expected: %zu, got: %zu", expected_size_output, size_output);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
uintptr_output = 123;
expected_uintptr_output = 0x1234567;
expected_result = 1;
result = SDL_snprintf(text, sizeof(text), "%p", expected_uintptr_output);
result = SDL_sscanf(text, "%p", &uintptr_output);
SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", \"%%p\", &output)", text);
SDLTest_AssertCheck(expected_uintptr_output == uintptr_output, "Check output, expected: %p, got: %p", expected_uintptr_output, uintptr_output);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
expected_result = 1; expected_result = 1;
text[0] = '\0'; text[0] = '\0';
result = SDL_sscanf("abc def", "%s", text); result = SDL_sscanf("abc def", "%s", text);