Fixed digit count in sscanf, e.g. "%1x"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
diff --git a/src/stdlib/SDL_string.c b/src/stdlib/SDL_string.c
index 943e727..fa1bcc1 100644
--- a/src/stdlib/SDL_string.c
+++ b/src/stdlib/SDL_string.c
@@ -51,7 +51,7 @@ static unsigned UTF8_TrailingBytes(unsigned char c)
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOL)
static size_t
-SDL_ScanLong(const char *text, int radix, long *valuep)
+SDL_ScanLong(const char *text, int count, int radix, long *valuep)
{
const char *textstart = text;
long value = 0;
@@ -78,6 +78,10 @@ SDL_ScanLong(const char *text, int radix, long *valuep)
value *= radix;
value += v;
++text;
+
+ if (count > 0 && (text - textstart) == count) {
+ break;
+ }
}
if (valuep && text > textstart) {
if (negative && value) {
@@ -92,7 +96,7 @@ SDL_ScanLong(const char *text, int radix, long *valuep)
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
static size_t
-SDL_ScanUnsignedLong(const char *text, int radix, unsigned long *valuep)
+SDL_ScanUnsignedLong(const char *text, int count, int radix, unsigned long *valuep)
{
const char *textstart = text;
unsigned long value = 0;
@@ -114,6 +118,10 @@ SDL_ScanUnsignedLong(const char *text, int radix, unsigned long *valuep)
value *= radix;
value += v;
++text;
+
+ if (count > 0 && (text - textstart) == count) {
+ break;
+ }
}
if (valuep && text > textstart) {
*valuep = value;
@@ -156,7 +164,7 @@ SDL_ScanUintPtrT(const char *text, int radix, uintptr_t * valuep)
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOLL)
static size_t
-SDL_ScanLongLong(const char *text, int radix, Sint64 * valuep)
+SDL_ScanLongLong(const char *text, int count, int radix, Sint64 * valuep)
{
const char *textstart = text;
Sint64 value = 0;
@@ -183,6 +191,10 @@ SDL_ScanLongLong(const char *text, int radix, Sint64 * valuep)
value *= radix;
value += v;
++text;
+
+ if (count > 0 && (text - textstart) == count) {
+ break;
+ }
}
if (valuep && text > textstart) {
if (negative && value) {
@@ -197,7 +209,7 @@ SDL_ScanLongLong(const char *text, int radix, Sint64 * valuep)
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOULL)
static size_t
-SDL_ScanUnsignedLongLong(const char *text, int radix, Uint64 * valuep)
+SDL_ScanUnsignedLongLong(const char *text, int count, int radix, Uint64 * valuep)
{
const char *textstart = text;
Uint64 value = 0;
@@ -219,6 +231,10 @@ SDL_ScanUnsignedLongLong(const char *text, int radix, Uint64 * valuep)
value *= radix;
value += v;
++text;
+
+ if (count > 0 && (text - textstart) == count) {
+ break;
+ }
}
if (valuep && text > textstart) {
*valuep = value;
@@ -240,7 +256,7 @@ SDL_ScanFloat(const char *text, double *valuep)
negative = SDL_TRUE;
++text;
}
- text += SDL_ScanUnsignedLong(text, 10, &lvalue);
+ text += SDL_ScanUnsignedLong(text, 0, 10, &lvalue);
value += lvalue;
if (*text == '.') {
int mult = 10;
@@ -872,7 +888,7 @@ SDL_strtol(const char *string, char **endp, int base)
}
}
- len = SDL_ScanLong(string, base, &value);
+ len = SDL_ScanLong(string, 0, base, &value);
if (endp) {
*endp = (char *) string + len;
}
@@ -897,7 +913,7 @@ SDL_strtoul(const char *string, char **endp, int base)
}
}
- len = SDL_ScanUnsignedLong(string, base, &value);
+ len = SDL_ScanUnsignedLong(string, 0, base, &value);
if (endp) {
*endp = (char *) string + len;
}
@@ -922,7 +938,7 @@ SDL_strtoll(const char *string, char **endp, int base)
}
}
- len = SDL_ScanLongLong(string, base, &value);
+ len = SDL_ScanLongLong(string, 0, base, &value);
if (endp) {
*endp = (char *) string + len;
}
@@ -947,7 +963,7 @@ SDL_strtoull(const char *string, char **endp, int base)
}
}
- len = SDL_ScanUnsignedLongLong(string, base, &value);
+ len = SDL_ScanUnsignedLongLong(string, 0, base, &value);
if (endp) {
*endp = (char *) string + len;
}
@@ -1125,7 +1141,7 @@ SDL_vsscanf(const char *text, const char *fmt, va_list ap)
suppress = SDL_TRUE;
++fmt;
}
- fmt += SDL_ScanLong(fmt, 10, &count);
+ fmt += SDL_ScanLong(fmt, 0, 10, &count);
if (*fmt == 'c') {
if (!count) {
@@ -1189,7 +1205,7 @@ SDL_vsscanf(const char *text, const char *fmt, va_list ap)
case 'd':
if (inttype == DO_LONGLONG) {
Sint64 value;
- advance = SDL_ScanLongLong(text, radix, &value);
+ advance = SDL_ScanLongLong(text, count, radix, &value);
text += advance;
if (advance && !suppress) {
Sint64 *valuep = va_arg(ap, Sint64 *);
@@ -1198,7 +1214,7 @@ SDL_vsscanf(const char *text, const char *fmt, va_list ap)
}
} else {
long value;
- advance = SDL_ScanLong(text, radix, &value);
+ advance = SDL_ScanLong(text, count, radix, &value);
text += advance;
if (advance && !suppress) {
switch (inttype) {
@@ -1243,7 +1259,7 @@ SDL_vsscanf(const char *text, const char *fmt, va_list ap)
case 'u':
if (inttype == DO_LONGLONG) {
Uint64 value = 0;
- advance = SDL_ScanUnsignedLongLong(text, radix, &value);
+ advance = SDL_ScanUnsignedLongLong(text, count, radix, &value);
text += advance;
if (advance && !suppress) {
Uint64 *valuep = va_arg(ap, Uint64 *);
@@ -1252,7 +1268,7 @@ SDL_vsscanf(const char *text, const char *fmt, va_list ap)
}
} else {
unsigned long value = 0;
- advance = SDL_ScanUnsignedLong(text, radix, &value);
+ advance = SDL_ScanUnsignedLong(text, count, radix, &value);
text += advance;
if (advance && !suppress) {
switch (inttype) {