From 84f5e594d47abac9a0a450f70ceb2f79322a5c3a Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Sat, 19 Oct 2024 11:23:44 -0400 Subject: lib: Only use snprintf variants Signed-off-by: Andrew Opalach --- include/al/lib.h | 3 ++- src/log.c | 12 ++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/include/al/lib.h b/include/al/lib.h index 606d24d..6e0b1a9 100644 --- a/include/al/lib.h +++ b/include/al/lib.h @@ -48,7 +48,8 @@ void al_set_alloc(void *(*malloc_func)(size_t), void *(*calloc_func)(size_t, siz #define al_memset memset #define al_bzero bzero #define al_strlen strlen -#define al_sprintf sprintf +#define al_snprintf snprintf +#define al_vsnprintf vsnprintf #if defined _DEBUG_ #define al_assert assert #else diff --git a/src/log.c b/src/log.c index 3466320..c64ca11 100644 --- a/src/log.c +++ b/src/log.c @@ -8,13 +8,13 @@ #define AL_LOG_TEMPLATE "%s:%d %s(): %s -> " #endif -#define AL_LOG_MESSAGE_SIZE 511 +#define AL_LOG_MESSAGE_MAX 511 #if !AL_LOG_SKIP static s32 al_print_default(void *userdata, char *s) { (void)userdata; - s32 ret = al_printf("%*.*s\n", 0, AL_LOG_MESSAGE_SIZE, s); + s32 ret = al_printf("%*.*s\n", 0, AL_LOG_MESSAGE_MAX, s); al_free(s); return ret; } @@ -30,14 +30,14 @@ void al_set_print(s32 (*print_func)(void *, char *), void *userdata) static char *get_buffer(const char *level, const char *section, const char *fmt, const char *name, const s32 line, const char *func, va_list args) { - char *buffer = al_calloc(1, AL_LOG_MESSAGE_SIZE + 1); + char *buffer = al_malloc(AL_LOG_MESSAGE_MAX + 1); #if AL_LOG_USE_SECTION - s32 prefix = snprintf(buffer, AL_LOG_MESSAGE_SIZE, AL_LOG_TEMPLATE, name, line, func, level, section); + s32 prefix = al_snprintf(buffer, AL_LOG_MESSAGE_MAX, AL_LOG_TEMPLATE, name, line, func, level, section); #else (void)section; - s32 prefix = snprintf(buffer, AL_LOG_MESSAGE_SIZE, AL_LOG_TEMPLATE, name, line, func, level); + s32 prefix = al_snprintf(buffer, AL_LOG_MESSAGE_MAX, AL_LOG_TEMPLATE, name, line, func, level); #endif - prefix += vsnprintf(buffer + prefix, AL_LOG_MESSAGE_SIZE - prefix, fmt, args); + prefix += al_vsnprintf(buffer + prefix, AL_LOG_MESSAGE_MAX - prefix, fmt, args); buffer[strcspn(buffer, "\r\n")] = '\0'; return buffer; } -- cgit v1.2.3-101-g0448