mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 06:27:45 +00:00
AK+Kernel: Support snprintf
In contrast to sprintf, which might overflow the given buffer. I feel bad about the code duplication, but that is a pre-existing issue.
This commit is contained in:
parent
2d34f0f93a
commit
0240baa42d
3 changed files with 30 additions and 0 deletions
|
@ -144,6 +144,34 @@ int sprintf(char* buffer, const char* fmt, ...)
|
|||
return ret;
|
||||
}
|
||||
|
||||
static size_t __vsnprintf_space_remaining;
|
||||
ALWAYS_INLINE void sized_buffer_putch(char*& bufptr, char ch)
|
||||
{
|
||||
if (__vsnprintf_space_remaining) {
|
||||
*bufptr++ = ch;
|
||||
--__vsnprintf_space_remaining;
|
||||
}
|
||||
}
|
||||
|
||||
int snprintf(char* buffer, size_t size, const char* fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
if (size) {
|
||||
__vsnprintf_space_remaining = size - 1;
|
||||
} else {
|
||||
__vsnprintf_space_remaining = 0;
|
||||
}
|
||||
int ret = printf_internal(sized_buffer_putch, buffer, fmt, ap);
|
||||
if (__vsnprintf_space_remaining) {
|
||||
buffer[ret] = '\0';
|
||||
} else if (size > 0) {
|
||||
buffer[size - 1] = '\0';
|
||||
}
|
||||
va_end(ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void debugger_out(char ch)
|
||||
{
|
||||
if (serial_debug)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue