Skip to content

Commit be6ed6e

Browse files
committed
Fixed sscanf("026", "%1x%1x%1x", &r, &g, &b)
Fixes libsdl-org#12510
1 parent 2433952 commit be6ed6e

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

src/stdlib/SDL_string.c

+10-14
Original file line numberDiff line numberDiff line change
@@ -368,14 +368,12 @@ static size_t SDL_ScanUnsignedLongLongInternal(const char *text, int count, int
368368
negative = *text == '-';
369369
++text;
370370
}
371-
if ((radix == 0 || radix == 16) && *text == '0' && text[1] != '\0') {
371+
if ((radix == 0 || radix == 16) && *text == '0' && (text[1] == 'x' || text[1] == 'X')) {
372+
text += 2;
373+
radix = 16;
374+
} else if (radix == 0 && *text == '0' && (text[1] >= '0' && text[1] <= '9')) {
372375
++text;
373-
if (*text == 'x' || *text == 'X') {
374-
radix = 16;
375-
++text;
376-
} else if (radix == 0) {
377-
radix = 8;
378-
}
376+
radix = 8;
379377
} else if (radix == 0) {
380378
radix = 10;
381379
}
@@ -462,14 +460,12 @@ static size_t SDL_ScanUnsignedLongLongInternalW(const wchar_t *text, int count,
462460
negative = *text == '-';
463461
++text;
464462
}
465-
if ((radix == 0 || radix == 16) && *text == '0') {
463+
if ((radix == 0 || radix == 16) && *text == '0' && (text[1] == 'x' || text[1] == 'X')) {
464+
text += 2;
465+
radix = 16;
466+
} else if (radix == 0 && *text == '0' && (text[1] >= '0' && text[1] <= '9')) {
466467
++text;
467-
if (*text == 'x' || *text == 'X') {
468-
radix = 16;
469-
++text;
470-
} else if (radix == 0) {
471-
radix = 8;
472-
}
468+
radix = 8;
473469
} else if (radix == 0) {
474470
radix = 10;
475471
}

test/testautomation_stdlib.c

+12
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ static int SDLCALL stdlib_sscanf(void *arg)
750750
size_t size_output, expected_size_output;
751751
void *ptr_output, *expected_ptr_output;
752752
char text[128], text2[128];
753+
unsigned int r = 0, g = 0, b = 0;
753754

754755
expected_output = output = 123;
755756
expected_result = -1;
@@ -785,6 +786,17 @@ static int SDLCALL stdlib_sscanf(void *arg)
785786
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
786787
SDLTest_AssertCheck(length == 1, "Check length, expected: 1, got: %i", length);
787788

789+
expected_result = 3;
790+
result = SDL_sscanf("#026", "#%1x%1x%1x", &r, &g, &b);
791+
SDLTest_AssertPass("Call to SDL_sscanf(\"#026\", \"#%%1x%%1x%%1x\", &r, &g, &b)");
792+
expected_output = 0;
793+
SDLTest_AssertCheck(r == expected_output, "Check output for r, expected: %i, got: %i", expected_output, r);
794+
expected_output = 2;
795+
SDLTest_AssertCheck(g == expected_output, "Check output for g, expected: %i, got: %i", expected_output, g);
796+
expected_output = 6;
797+
SDLTest_AssertCheck(b == expected_output, "Check output for b, expected: %i, got: %i", expected_output, b);
798+
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
799+
788800
#define SIZED_TEST_CASE(type, var, printf_specifier, scanf_specifier) \
789801
var##_output = 123; \
790802
var##_length = 0; \

0 commit comments

Comments
 (0)