mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 12:35:14 +00:00
LibC+Kernel: Remove global variable use from snprintf and fprintf
The global variable use in these functions is super thread-unsafe and means that any concurrent calls to sprintf or fprintf in a process could race with each other and end up writing unexpected results. We can just replace the function + global variable with a lambda that captures the relevant argument when calling printf_internal instead.
This commit is contained in:
parent
8ec4328fcb
commit
353e72ac9b
Notes:
sideshowbarker
2024-07-17 19:07:08 +09:00
Author: https://github.com/ADKaster Commit: https://github.com/SerenityOS/serenity/commit/353e72ac9b Pull-request: https://github.com/SerenityOS/serenity/pull/12374
2 changed files with 21 additions and 32 deletions
|
@ -123,26 +123,24 @@ 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);
|
||||
size_t space_remaining = 0;
|
||||
if (size) {
|
||||
__vsnprintf_space_remaining = size - 1;
|
||||
space_remaining = size - 1;
|
||||
} else {
|
||||
__vsnprintf_space_remaining = 0;
|
||||
space_remaining = 0;
|
||||
}
|
||||
auto sized_buffer_putch = [&](char*& bufptr, char ch) {
|
||||
if (space_remaining) {
|
||||
*bufptr++ = ch;
|
||||
--space_remaining;
|
||||
}
|
||||
};
|
||||
int ret = printf_internal(sized_buffer_putch, buffer, fmt, ap);
|
||||
if (__vsnprintf_space_remaining) {
|
||||
if (space_remaining) {
|
||||
buffer[ret] = '\0';
|
||||
} else if (size > 0) {
|
||||
buffer[size - 1] = '\0';
|
||||
|
|
|
@ -870,17 +870,10 @@ ALWAYS_INLINE void stdout_putch(char*&, char ch)
|
|||
putchar(ch);
|
||||
}
|
||||
|
||||
static FILE* __current_stream = nullptr;
|
||||
ALWAYS_INLINE static void stream_putch(char*&, char ch)
|
||||
{
|
||||
fputc(ch, __current_stream);
|
||||
}
|
||||
|
||||
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/vfprintf.html
|
||||
int vfprintf(FILE* stream, const char* fmt, va_list ap)
|
||||
{
|
||||
__current_stream = stream;
|
||||
return printf_internal(stream_putch, nullptr, fmt, ap);
|
||||
return printf_internal([stream](auto, char ch) { fputc(ch, stream); }, nullptr, fmt, ap);
|
||||
}
|
||||
|
||||
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html
|
||||
|
@ -957,25 +950,23 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/vsnprintf.html
|
||||
int vsnprintf(char* buffer, size_t size, const char* fmt, va_list ap)
|
||||
{
|
||||
size_t space_remaining = 0;
|
||||
if (size) {
|
||||
__vsnprintf_space_remaining = size - 1;
|
||||
space_remaining = size - 1;
|
||||
} else {
|
||||
__vsnprintf_space_remaining = 0;
|
||||
space_remaining = 0;
|
||||
}
|
||||
auto sized_buffer_putch = [&](char*& bufptr, char ch) {
|
||||
if (space_remaining) {
|
||||
*bufptr++ = ch;
|
||||
--space_remaining;
|
||||
}
|
||||
};
|
||||
int ret = printf_internal(sized_buffer_putch, buffer, fmt, ap);
|
||||
if (__vsnprintf_space_remaining) {
|
||||
if (space_remaining) {
|
||||
buffer[ret] = '\0';
|
||||
} else if (size > 0) {
|
||||
buffer[size - 1] = '\0';
|
||||
|
|
Loading…
Add table
Reference in a new issue