Skip to content

Commit 1caae3e

Browse files
committed
Fixed sscanf("026", "%1x%1x%1x", &r, &g, &b)
Fixes #12510 (cherry picked from commit be6ed6e)
1 parent 22a87a2 commit 1caae3e

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/stdlib/SDL_string.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,12 @@ static size_t SDL_ScanUnsignedLongLongInternal(const char *text, int count, int
8181
negative = *text == '-';
8282
++text;
8383
}
84-
if ((radix == 0 || radix == 16) && *text == '0' && text[1] != '\0') {
84+
if ((radix == 0 || radix == 16) && *text == '0' && (text[1] == 'x' || text[1] == 'X')) {
85+
text += 2;
86+
radix = 16;
87+
} else if (radix == 0 && *text == '0' && (text[1] >= '0' && text[1] <= '9')) {
8588
++text;
86-
if (*text == 'x' || *text == 'X') {
87-
radix = 16;
88-
++text;
89-
} else if (radix == 0) {
90-
radix = 8;
91-
}
89+
radix = 8;
9290
} else if (radix == 0) {
9391
radix = 10;
9492
}

test/testautomation_stdlib.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ int stdlib_sscanf(void *arg)
412412
long long long_long_output, expected_long_long_output, long_long_length;
413413
size_t size_output, expected_size_output;
414414
char text[128], text2[128];
415+
unsigned int r = 0, g = 0, b = 0;
415416

416417
expected_output = output = 123;
417418
expected_result = -1;
@@ -447,6 +448,17 @@ int stdlib_sscanf(void *arg)
447448
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
448449
SDLTest_AssertCheck(length == 1, "Check length, expected: 1, got: %i", length);
449450

451+
expected_result = 3;
452+
result = SDL_sscanf("#026", "#%1x%1x%1x", &r, &g, &b);
453+
SDLTest_AssertPass("Call to SDL_sscanf(\"#026\", \"#%%1x%%1x%%1x\", &r, &g, &b)");
454+
expected_output = 0;
455+
SDLTest_AssertCheck(r == expected_output, "Check output for r, expected: %i, got: %i", expected_output, r);
456+
expected_output = 2;
457+
SDLTest_AssertCheck(g == expected_output, "Check output for g, expected: %i, got: %i", expected_output, g);
458+
expected_output = 6;
459+
SDLTest_AssertCheck(b == expected_output, "Check output for b, expected: %i, got: %i", expected_output, b);
460+
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
461+
450462
#define SIZED_TEST_CASE(type, var, printf_specifier, scanf_specifier) \
451463
var##_output = 123; \
452464
var##_length = 0; \

0 commit comments

Comments
 (0)