Bound INI buffers to allocator limits and log allocation errors

This commit is contained in:
evgeny committed 2026-10-21 19:03:38 +02:00
1 parent 33130c1d74
commit f7c8b95af6
2 files changed
+9 -5

No files matched your search

+7 -3
View File
@@ -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;
+2 -2
View File
@@ -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);