From 2a8d00634ddf4ece71c11ec664996ed8ea05205b Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 20 Sep 2022 07:25:49 -0700
Subject: [PATCH] Fixed scanning a negative number as an unsigned value
e.g. sscanf("-1", "%zu", &v)
Thanks to @sezero for the test case
---
src/stdlib/SDL_string.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/src/stdlib/SDL_string.c b/src/stdlib/SDL_string.c
index 9e862792e90..d5ce0010dbc 100644
--- a/src/stdlib/SDL_string.c
+++ b/src/stdlib/SDL_string.c
@@ -105,6 +105,10 @@ SDL_ScanUnsignedLong(const char *text, int count, int radix, unsigned long *valu
const char *textstart = text;
unsigned long value = 0;
+ if (*text == '-') {
+ return SDL_ScanLong(text, count, radix, (long *)valuep);
+ }
+
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
text += 2;
}
@@ -218,6 +222,10 @@ SDL_ScanUnsignedLongLong(const char *text, int count, int radix, Uint64 * valuep
const char *textstart = text;
Uint64 value = 0;
+ if (*text == '-') {
+ return SDL_ScanLongLong(text, count, radix, (Sint64 *)valuep);
+ }
+
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
text += 2;
}