From 8dc7e338f45872783a729b9b7e06ca642c1f2011 Mon Sep 17 00:00:00 2001
From: Alexander Stephan <alexander.stephan@sap.com>
Date: Thu, 28 Aug 2025 11:31:06 +0000
Subject: [PATCH] BUG/MINOR: Add missing NULL checks

This commit fixes several occurrences of missing null checks, improving
the error handling. The error handling was aligned with the error
handling of the surrounding function or file.

Co-authored-by: Christian Norbert Menges <christian.norbert.menges@sap.com>
---
 admin/halog/halog.c   | 8 ++++++++
 src/acl.c             | 4 ++++
 src/cfgparse-listen.c | 6 ++++++
 src/flt_http_comp.c   | 5 +++++
 src/log.c             | 7 ++++---
 src/tools.c           | 2 ++
 6 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/admin/halog/halog.c b/admin/halog/halog.c
index 3d8941140..bbed5b81f 100644
--- a/admin/halog/halog.c
+++ b/admin/halog/halog.c
@@ -1571,6 +1571,10 @@ void filter_count_srv_status(const char *accept_field, const char *time_field, s
 	if (!srv_node) {
 		/* server not yet in the tree, let's create it */
 		srv = (void *)calloc(1, sizeof(struct srv_st) + e - b + 1);
+		if (unlikely(!srv)) {
+			fprintf(stderr, "%s: not enough memory\n", __FUNCTION__);
+			exit(1);
+		}
 		srv_node = &srv->node;
 		memcpy(&srv_node->key, b, e - b);
 		srv_node->key[e - b] = '\0';
@@ -1680,6 +1684,10 @@ void filter_count_url(const char *accept_field, const char *time_field, struct t
 	 */
 	if (unlikely(!ustat))
 		ustat = calloc(1, sizeof(*ustat));
+		if (unlikely(!ustat)) {
+			fprintf(stderr, "%s: not enough memory\n", __FUNCTION__);
+			exit(1);
+		}
 
 	ustat->nb_err = err;
 	ustat->nb_req = 1;
diff --git a/src/acl.c b/src/acl.c
index e607c9b76..1a2ab8b2c 100644
--- a/src/acl.c
+++ b/src/acl.c
@@ -1336,6 +1336,10 @@ int smp_fetch_acl_parse(struct arg *args, char **err_msg)
 	for (i = 0; args[i].type != ARGT_STOP; i++)
 		;
 	acl_sample = calloc(1, sizeof(struct acl_sample) + sizeof(struct acl_term) * i);
+	if (unlikely(!acl_sample)) {
+		memprintf(err_msg, "out of memory when parsing ACL expression");
+		return 0;
+	}
 	LIST_INIT(&acl_sample->suite.terms);
 	LIST_INIT(&acl_sample->cond.suites);
 	LIST_APPEND(&acl_sample->cond.suites, &acl_sample->suite.list);
diff --git a/src/cfgparse-listen.c b/src/cfgparse-listen.c
index 07bb05012..4e74a20d6 100644
--- a/src/cfgparse-listen.c
+++ b/src/cfgparse-listen.c
@@ -2020,6 +2020,12 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int kwm)
 					len += strlen(args[i]) + 1;
 
 				desc = d = calloc(1, len);
+				if (unlikely(!d)) {
+					ha_alert("parsing [%s:%d]: '%s %s' : memory allocation failed\n",
+							 file, linenum, args[0], args[1]);
+					err_code |= ERR_ALERT | ERR_FATAL;
+					goto out;
+				}
 
 				d += snprintf(d, desc + len - d, "%s", args[2]);
 				for (i = 3; *args[i]; i++)
diff --git a/src/flt_http_comp.c b/src/flt_http_comp.c
index 5a5b65c04..a1ccde973 100644
--- a/src/flt_http_comp.c
+++ b/src/flt_http_comp.c
@@ -830,6 +830,11 @@ parse_compression_options(char **args, int section, struct proxy *proxy,
 
 	if (proxy->comp == NULL) {
 		comp = calloc(1, sizeof(*comp));
+		if (unlikely(!comp)) {
+			memprintf(err, "'%s': out of memory.", args[0]);
+			ret = -1;
+			goto end;
+		}
 		/* Always default to compress responses */
 		comp->flags = COMP_FL_DIR_RES;
 		proxy->comp = comp;
diff --git a/src/log.c b/src/log.c
index 0d1e9a35b..c7acb5b29 100644
--- a/src/log.c
+++ b/src/log.c
@@ -573,11 +573,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;
@@ -1558,7 +1558,8 @@ struct logger *dup_logger(struct logger *def)
 
 	BUG_ON(def->flags & LOGGER_FL_RESOLVED);
 	cpy = malloc(sizeof(*cpy));
-
+	if (unlikely(!cpy))
+		return NULL;
 	/* copy everything that can be easily copied */
 	memcpy(cpy, def, sizeof(*cpy));
 
diff --git a/src/tools.c b/src/tools.c
index 4507564bc..1615ef2ae 100644
--- a/src/tools.c
+++ b/src/tools.c
@@ -4689,6 +4689,8 @@ char *indent_msg(char **out, int level)
 
 	needed = 1 + level * (lf + 1) + len + 1;
 	p = ret = malloc(needed);
+	if (unlikely(!ret))
+		return NULL;
 	in = *out;
 
 	/* skip initial LFs */
-- 
2.35.3

