mirror of
https://github.com/jemalloc/jemalloc.git
synced 2026-04-14 14:41:42 +03:00
Fix out-of-bounds write in malloc_vsnprintf when size is 0
When called with size==0, the else branch wrote to str[size-1] which is str[(size_t)-1], a massive out-of-bounds write. Standard vsnprintf allows size==0 to mean "compute length only, write nothing". Add unit test for the size==0 case.
This commit is contained in:
parent
eab2b29736
commit
c2d57040f0
2 changed files with 20 additions and 2 deletions
|
|
@ -692,7 +692,7 @@ malloc_vsnprintf(char *str, size_t size, const char *format, va_list ap) {
|
||||||
label_out:
|
label_out:
|
||||||
if (i < size) {
|
if (i < size) {
|
||||||
str[i] = '\0';
|
str[i] = '\0';
|
||||||
} else {
|
} else if (size != 0) {
|
||||||
str[size - 1] = '\0';
|
str[size - 1] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -252,8 +252,26 @@ TEST_BEGIN(test_malloc_snprintf) {
|
||||||
}
|
}
|
||||||
TEST_END
|
TEST_END
|
||||||
|
|
||||||
|
TEST_BEGIN(test_malloc_snprintf_zero_size) {
|
||||||
|
char buf[8];
|
||||||
|
size_t result;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* malloc_snprintf with size==0 should not write anything but should
|
||||||
|
* return the length that would have been written. A previous bug
|
||||||
|
* caused an out-of-bounds write via str[size - 1] when size was 0.
|
||||||
|
*/
|
||||||
|
memset(buf, 'X', sizeof(buf));
|
||||||
|
result = malloc_snprintf(buf, 0, "%s", "hello");
|
||||||
|
expect_zu_eq(result, 5, "Expected length 5 for \"hello\"");
|
||||||
|
/* buf should be untouched. */
|
||||||
|
expect_c_eq(buf[0], 'X', "Buffer should not have been modified");
|
||||||
|
}
|
||||||
|
TEST_END
|
||||||
|
|
||||||
int
|
int
|
||||||
main(void) {
|
main(void) {
|
||||||
return test(test_malloc_strtoumax_no_endptr, test_malloc_strtoumax,
|
return test(test_malloc_strtoumax_no_endptr, test_malloc_strtoumax,
|
||||||
test_malloc_snprintf_truncated, test_malloc_snprintf);
|
test_malloc_snprintf_truncated, test_malloc_snprintf,
|
||||||
|
test_malloc_snprintf_zero_size);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue