From f7c8b95af6311e2232de96ca6a1ffb71719732ae Mon Sep 17 00:00:00 2001 From: evgeny Date: Wed, 21 Oct 2026 19:03:38 +0200 Subject: [PATCH] Bound INI buffers to allocator limits and log allocation errors --- src/config_ini.h | 10 +++++++--- src/config_updater.c | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/config_ini.h b/src/config_ini.h index 6f633396..b5c226ef 100644 --- a/src/config_ini.h +++ b/src/config_ini.h @@ -54,7 +54,7 @@ static inline int config_ini_key_value(const char *line, char *key, size_t key_c /* Проверить физические строки до разбора, чтобы не принять хвост длинной строки за новую опцию. */ static inline int config_ini_validate(const char *text, size_t length, const char *filename) { size_t column = 0, line = 1; - if (!text && length) return -1; + if (!text && length) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s: NULL configuration buffer", filename); return -1; } for (size_t i = 0; i < length; i++) { if (!text[i] || (text[i] != '\n' && ++column >= CONFIG_INI_LINE_SIZE - 1)) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%zu: INI contains NUL or a line longer than 1022 bytes", filename, line); @@ -73,7 +73,7 @@ static inline int config_ini_read(FILE *file, const char *filename, char **text, *text = NULL; *length = 0; while ((count = fread(chunk, 1, sizeof(chunk), file)) != 0) { - if (memchr(chunk, '\0', count) || output.len > SIZE_MAX - count - 1 || + if (memchr(chunk, '\0', count) || output.len > UINT32_MAX / 2 - count - 1 || strbuf_addf(&output, "%.*s", (int)count, chunk) < 0) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s: cannot read INI text (NUL, size overflow or allocation failure)", filename); strbuf_free(&output); @@ -85,7 +85,11 @@ static inline int config_ini_read(FILE *file, const char *filename, char **text, strbuf_free(&output); return -1; } - if (!output.buf && strbuf_addf(&output, "%s", "") < 0) { strbuf_free(&output); return -1; } + if (!output.buf && strbuf_addf(&output, "%s", "") < 0) { + DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s: cannot allocate empty configuration buffer", filename); + strbuf_free(&output); + return -1; + } *length = output.len; *text = strbuf_detach(&output); return 0; diff --git a/src/config_updater.c b/src/config_updater.c index 8fbdc28b..698e768b 100644 --- a/src/config_updater.c +++ b/src/config_updater.c @@ -217,7 +217,7 @@ static int config_publish(const char *filename, const char *text, size_t length, #endif phase = "create temporary file"; size_t name_length = strlen(filename); - if (name_length > SIZE_MAX - 64) { errno = EOVERFLOW; goto done; } + if (name_length > UINT32_MAX - 64) { errno = EOVERFLOW; goto done; } temp = u_malloc(name_length + 64); if (!temp) goto done; temp[0] = '\0'; @@ -312,7 +312,7 @@ int config_ensure_keys_and_node_id(const char *filename) { return -1; } if (!text) text = u_strdup(""); - if (!text) return -1; + if (!text) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s: cannot allocate configuration buffer", filename); return -1; } struct config_field fields[CFG_FIELD_COUNT]; size_t ends[CFG_SECTION_COUNT]; config_fields_init(fields);