mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 06:37:43 +00:00
LibC: Fix off-by-one in snprintf()
snprintf is supposed to *always* NUL-terminate its output, so it has to write one output byte fewer. And yes, I *did* check all existing usages; this shouldn't break anything.
This commit is contained in:
parent
499e953380
commit
2d34f0f93a
1 changed files with 7 additions and 1 deletions
|
@ -889,10 +889,16 @@ ALWAYS_INLINE void sized_buffer_putch(char*& bufptr, char ch)
|
||||||
|
|
||||||
int vsnprintf(char* buffer, size_t size, const char* fmt, va_list ap)
|
int vsnprintf(char* buffer, size_t size, const char* fmt, va_list ap)
|
||||||
{
|
{
|
||||||
__vsnprintf_space_remaining = size;
|
if (size) {
|
||||||
|
__vsnprintf_space_remaining = size - 1;
|
||||||
|
} else {
|
||||||
|
__vsnprintf_space_remaining = 0;
|
||||||
|
}
|
||||||
int ret = printf_internal(sized_buffer_putch, buffer, fmt, ap);
|
int ret = printf_internal(sized_buffer_putch, buffer, fmt, ap);
|
||||||
if (__vsnprintf_space_remaining) {
|
if (__vsnprintf_space_remaining) {
|
||||||
buffer[ret] = '\0';
|
buffer[ret] = '\0';
|
||||||
|
} else if (size > 0) {
|
||||||
|
buffer[size - 1] = '\0';
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue