Skip to content

Commit 22ac1f5

Browse files
alexanderstephanGarfield96
authored andcommitted
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 <christian.norbert.menges@sap.com>
1 parent fbd0fb2 commit 22ac1f5

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

src/log.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,11 +573,11 @@ int add_to_logformat_list(char *start, char *end, int type, struct lf_expr *lf_e
573573

574574
if (type == LF_TEXT) { /* type text */
575575
struct logformat_node *node = calloc(1, sizeof(*node));
576-
if (!node) {
576+
str = calloc(1, end - start + 1);
577+
if (unlikely(!node || !str)) {
577578
memprintf(err, "out of memory error");
578579
return 0;
579580
}
580-
str = calloc(1, end - start + 1);
581581
strncpy(str, start, end - start);
582582
str[end - start] = '\0';
583583
node->arg = str;
@@ -1558,7 +1558,8 @@ struct logger *dup_logger(struct logger *def)
15581558

15591559
BUG_ON(def->flags & LOGGER_FL_RESOLVED);
15601560
cpy = malloc(sizeof(*cpy));
1561-
1561+
if (unlikely(!cpy))
1562+
return NULL;
15621563
/* copy everything that can be easily copied */
15631564
memcpy(cpy, def, sizeof(*cpy));
15641565

0 commit comments

Comments
 (0)