From 2021874ca9c847de28b9fa4c3b6c4c948488ab49 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Sat, 3 Oct 2026 09:58:14 +1000 Subject: [PATCH] [common/client] windows: avoid deprecated CRT calls --- client/renderers/EGL/postprocess.c | 6 ++++- client/renderers/EGL/shader.c | 32 ++++++++++++++++++++---- client/src/config.c | 12 +++------ common/include/common/filesystem.h | 2 ++ common/src/option.c | 12 +++------ common/src/platform/linux/filesystem.c | 16 ++++++++++++ common/src/platform/windows/filesystem.c | 9 +++++++ common/src/stringutils.c | 9 +++++-- 8 files changed, 74 insertions(+), 24 deletions(-) diff --git a/client/renderers/EGL/postprocess.c b/client/renderers/EGL/postprocess.c index edaf5685..f7825dcd 100644 --- a/client/renderers/EGL/postprocess.c +++ b/client/renderers/EGL/postprocess.c @@ -234,7 +234,11 @@ static bool savePreset(struct EGL_PostProcess * this, const char * name) FILE * file = lgOpenFile(path, "w"); if (!file) { - const char * strError = strerror(errno); + const int errorCode = errno; + char strError[256] = { 0 }; + if (!lgFileErrorString(errorCode, strError, sizeof(strError))) + snprintf(strError, sizeof(strError), "Error %d", errorCode); + DEBUG_ERROR("Failed to open preset \"%s\" for writing: %s", name, strError); free(path); diff --git a/client/renderers/EGL/shader.c b/client/renderers/EGL/shader.c index 6c67315f..b13ab76d 100644 --- a/client/renderers/EGL/shader.c +++ b/client/renderers/EGL/shader.c @@ -24,6 +24,7 @@ #include "common/stringutils.h" #include "util.h" +#include #include #include #include @@ -384,12 +385,22 @@ bool egl_shaderCompile(EGL_Shader * this, const char * vertex_code, } } - int processedLen = fragment_size; + int processedLen = fragment_size; const char * defineFormat = "#define %s %s\n"; for(const EGL_ShaderDefine * define = defines; define->name; ++define) - processedLen += snprintf(NULL, 0, defineFormat, define->name, define->value); + { + const int defineLength = snprintf( + NULL, 0, defineFormat, define->name, define->value); + if (defineLength < 0 || processedLen > INT_MAX - defineLength) + { + DEBUG_ERROR("Failed to format shader defines"); + goto exit; + } - processed = malloc(processedLen); + processedLen += defineLength; + } + + processed = malloc((size_t)processedLen + 1); if (!processed) { DEBUG_ERROR("Out of memory"); @@ -400,13 +411,24 @@ bool egl_shaderCompile(EGL_Shader * this, const char * vertex_code, int offset = insertPos; for(const EGL_ShaderDefine * define = defines; define->name; ++define) - offset += sprintf(processed + offset, defineFormat, - define->name, define->value); + { + const size_t available = (size_t)(processedLen - offset) + 1; + const int written = snprintf(processed + offset, available, + defineFormat, define->name, define->value); + if (written < 0 || (size_t)written >= available) + { + DEBUG_ERROR("Failed to format shader defines"); + goto exit; + } + + offset += written; + } memcpy( processed + offset, fragment_code + insertPos, fragment_size - insertPos); + processed[processedLen] = '\0'; fragment_code = processed; fragment_size = processedLen; diff --git a/client/src/config.c b/client/src/config.c index 077810e1..ed4f9519 100644 --- a/client/src/config.c +++ b/client/src/config.c @@ -840,14 +840,12 @@ static char * optPosToString(struct Option * opt) if (g_params.center) return lg_strdup("center"); - int len = snprintf(NULL, 0, "%dx%d", g_params.x, g_params.y); - char * str = malloc(len + 1); - if (!str) + char * str; + if (alloc_sprintf(&str, "%dx%d", g_params.x, g_params.y) < 0) { DEBUG_ERROR("out of memory"); return NULL; } - sprintf(str, "%dx%d", g_params.x, g_params.y); return str; } @@ -877,14 +875,12 @@ static StringList optSizeValues(struct Option * opt) static char * optSizeToString(struct Option * opt) { - int len = snprintf(NULL, 0, "%ux%u", g_params.w, g_params.h); - char * str = malloc(len + 1); - if (!str) + char * str; + if (alloc_sprintf(&str, "%ux%u", g_params.w, g_params.h) < 0) { DEBUG_ERROR("out of memory"); return NULL; } - sprintf(str, "%ux%u", g_params.w, g_params.h); return str; } diff --git a/common/include/common/filesystem.h b/common/include/common/filesystem.h index 37e33513..ed7c641a 100644 --- a/common/include/common/filesystem.h +++ b/common/include/common/filesystem.h @@ -24,6 +24,7 @@ #include "common/stringlist.h" #include +#include #include typedef enum LGFileType @@ -42,6 +43,7 @@ bool lgCreateDirectory (const char * path); bool lgDeleteFile (const char * path); bool lgDeleteDirectory (const char * path); FILE * lgOpenFile (const char * path, const char * mode); +bool lgFileErrorString (int error, char * buffer, size_t size); /* Returns an owned list of entry names, or NULL when the directory cannot be * opened or read. The special entries "." and ".." are omitted. */ diff --git a/common/src/option.c b/common/src/option.c index dd7be8a5..942e7f2a 100644 --- a/common/src/option.c +++ b/common/src/option.c @@ -103,15 +103,13 @@ static bool string_parser(struct Option * opt, const char * str) static char * int_toString(struct Option * opt) { - int len = snprintf(NULL, 0, "%d", opt->value.x_int); - char * ret = malloc(len + 1); - if (!ret) + char * ret; + if (alloc_sprintf(&ret, "%d", opt->value.x_int) < 0) { DEBUG_ERROR("out of memory"); return NULL; } - sprintf(ret, "%d", opt->value.x_int); return ret; } @@ -122,15 +120,13 @@ static char * bool_toString(struct Option * opt) static char * float_toString(struct Option * opt) { - int len = snprintf(NULL, 0, "%f", opt->value.x_float); - char * ret = malloc(len + 1); - if (!ret) + char * ret; + if (alloc_sprintf(&ret, "%f", opt->value.x_float) < 0) { DEBUG_ERROR("out of memory"); return NULL; } - sprintf(ret, "%f", opt->value.x_float); return ret; } diff --git a/common/src/platform/linux/filesystem.c b/common/src/platform/linux/filesystem.c index 095e64ab..e40f6d78 100644 --- a/common/src/platform/linux/filesystem.c +++ b/common/src/platform/linux/filesystem.c @@ -77,6 +77,22 @@ FILE * lgOpenFile(const char * path, const char * mode) return fopen(path, mode); } +bool lgFileErrorString(int error, char * buffer, size_t size) +{ + if (!buffer || !size) + return false; + + const char * message = strerror(error); + if (!message) + { + buffer[0] = '\0'; + return false; + } + + str_copy(buffer, size, message, strlen(message)); + return true; +} + StringList lgListDirectory(const char * path) { DIR * directory = opendir(path); diff --git a/common/src/platform/windows/filesystem.c b/common/src/platform/windows/filesystem.c index db93a840..2e33b4ae 100644 --- a/common/src/platform/windows/filesystem.c +++ b/common/src/platform/windows/filesystem.c @@ -151,6 +151,15 @@ FILE * lgOpenFile(const char * path, const char * mode) return result; } +bool lgFileErrorString(int error, char * buffer, size_t size) +{ + if (!buffer || !size) + return false; + + buffer[0] = '\0'; + return strerror_s(buffer, size, error) == 0; +} + StringList lgListDirectory(const char * path) { wchar_t * wide = widePath(path); diff --git a/common/src/stringutils.c b/common/src/stringutils.c index 811fd1b9..685b1b75 100644 --- a/common/src/stringutils.c +++ b/common/src/stringutils.c @@ -51,9 +51,14 @@ int valloc_sprintf(char ** str, const char * format, va_list ap) if (len < 0) return len; - *str = malloc(len + 1); + *str = malloc((size_t)len + 1); + if (!*str) + { + errno = ENOMEM; + return -1; + } - int ret = vsnprintf(*str, len + 1, format, ap); + int ret = vsnprintf(*str, (size_t)len + 1, format, ap); if (ret < 0) { free(*str);