From 7964b6bcc657515ee63bfc7dd07f5432d3476d46 Mon Sep 17 00:00:00 2001 From: Alexander Stephan Date: Mon, 1 Sep 2025 09:36:07 +0000 Subject: [PATCH] BUG/MINOR: log: Add OOM checks for calloc() and malloc() in logformat parser and dup_logger() This patch adds missing out-of-memory (OOM) checks after calls to `calloc()` and `malloc()` in the logformat parser and the `dup_logger()` function. If memory allocation fails, an error is reported or NULL is returned, preventing undefined behavior in low-memory conditions. Co-authored-by: Christian Norbert Menges (cherry picked from commit 22ac1f5ee9889eb7273201de0d92449d9fc5e3fa) Signed-off-by: Christopher Faulet (cherry picked from commit 0fd91432f06ee6a7b427664be127b02c854e34ab) Signed-off-by: Christopher Faulet (cherry picked from commit de370ed1e1925fa54778ae021f92c97f96ca1a03) Signed-off-by: Christopher Faulet --- src/log.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/log.c b/src/log.c index 8a46a76..3c50379 100644 --- a/src/log.c +++ b/src/log.c @@ -494,11 +494,11 @@ int add_to_logformat_list(char *start, char *end, int type, struct lf_expr *lf_e if (type == LF_TEXT) { /* type text */ struct logformat_node *node = calloc(1, sizeof(*node)); - if (!node) { + str = calloc(1, end - start + 1); + if (unlikely(!node || !str)) { memprintf(err, "out of memory error"); return 0; } - str = calloc(1, end - start + 1); strncpy(str, start, end - start); str[end - start] = '\0'; node->arg = str; @@ -1443,6 +1443,8 @@ struct logger *dup_logger(struct logger *def) { struct logger *cpy = malloc(sizeof(*cpy)); + if (unlikely(!cpy)) + return NULL; /* copy everything that can be easily copied */ memcpy(cpy, def, sizeof(*cpy)); -- 1.7.10.4