Skip to content

Commit c2fe55b

Browse files
inaky-intcnashif
authored andcommitted
libc/minimal: snprintf(): KILL negative len parameter
snprintf() implements the ability to foce a negative value through the (unsigned) size_t len parameter to allow the formatter to use a maximum size string. This is point less, we don't have as much memory and this is a recipe for all kinds of vulnerabilities. Kill the whole thing, the testcase it represents and thank Coverity for finding this thing. Whatever use it had before, it has no more. Change-Id: If422246548664699d8aa328a1b9304ef13cab7ea Coverity-ID: 131625 Coverity-ID: 131626 Signed-off-by: Inaky Perez-Gonzalez <[email protected]>
1 parent 70028dd commit c2fe55b

File tree

2 files changed

+4
-53
lines changed

2 files changed

+4
-53
lines changed

lib/libc/minimal/source/stdout/sprintf.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,8 @@ int snprintf(char *_Restrict s, size_t len, const char *_Restrict format, ...)
4545
int r;
4646
char dummy;
4747

48-
if ((int) len <= 0) {
49-
if (len == 0) {
50-
s = &dummy; /* write final NUL to dummy, since can't change *s */
51-
} else {
52-
len = 0x7fffffff; /* allow up to "maxint" characters */
53-
}
48+
if (len == 0) {
49+
s = &dummy; /* write final NUL to dummy, can't change *s */
5450
}
5551

5652
p.ptr = s;
@@ -88,12 +84,8 @@ int vsnprintf(char *_Restrict s, size_t len, const char *_Restrict format, va_li
8884
int r;
8985
char dummy;
9086

91-
if ((int) len <= 0) {
92-
if (len == 0) {
93-
s = &dummy; /* write final NUL to dummy, since can't change *s */
94-
} else {
95-
len = 0x7fffffff; /* allow up to "maxint" characters */
96-
}
87+
if (len == 0) {
88+
s = &dummy; /* write final NUL to dummy, can't change * *s */
9789
}
9890

9991
p.ptr = s;

tests/kernel/test_sprintf/src/test_sprintf.c

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -235,27 +235,6 @@ int vsnprintfTest(void)
235235
int status = TC_PASS;
236236
char buffer[100];
237237

238-
/*
239-
* The string size may be handled in a non-standard manner.
240-
* If a negative value is supplied for the string size, it is converted
241-
* to 0x7fffffff--maximum integer size. Since there is insufficient
242-
* memory to test a string of that length, we just check that the string
243-
* was fully written so that we can exercise the code path.
244-
*/
245-
buffer[0] = '\0';
246-
len = tvsnprintf(buffer, (size_t)(-4), "%x", DEADBEEF);
247-
if (len != strlen(DEADBEEF_LHEX_STR)) {
248-
TC_ERROR("vsnprintf(%%x). Expected return value %d, not %d\n",
249-
strlen(DEADBEEF_LHEX_STR), len);
250-
status = TC_FAIL;
251-
}
252-
253-
if (strcmp(buffer, DEADBEEF_LHEX_STR) != 0) {
254-
TC_ERROR("vsnprintf(%%x). Expected '%s', got '%s'\n",
255-
DEADBEEF_LHEX_STR, buffer);
256-
status = TC_FAIL;
257-
}
258-
259238
/*******************/
260239
buffer[0] = '\0';
261240
len = tvsnprintf(buffer, 0, "%x", DEADBEEF);
@@ -356,26 +335,6 @@ int snprintfTest(void)
356335
int status = TC_PASS;
357336
char buffer[100];
358337

359-
/*
360-
* The string size may be handled in a non-standard manner.
361-
* If a negative value is supplied for the string size, it is converted
362-
* to 0x7fffffff--maximum integer size. Since there is insufficient
363-
* memory to test a string of that length, we just check that the string
364-
* was fully written so that we can exercise the code path.
365-
*/
366-
buffer[0] = '\0';
367-
len = snprintf(buffer, (size_t)(-4), "%x", DEADBEEF);
368-
if (len != strlen(DEADBEEF_LHEX_STR)) {
369-
TC_ERROR("snprintf(%%x). Expected return value %d, not %d\n",
370-
strlen(DEADBEEF_LHEX_STR), len);
371-
status = TC_FAIL;
372-
}
373-
374-
if (strcmp(buffer, DEADBEEF_LHEX_STR) != 0) {
375-
TC_ERROR("snprintf(%%x). Expected '%s', got '%s'\n",
376-
DEADBEEF_LHEX_STR, buffer);
377-
status = TC_FAIL;
378-
}
379338

380339
/*******************/
381340
buffer[0] = '\0';

0 commit comments

Comments
 (0)