From 15d29a145ebe67cae52316871fcdedb5a19ce628 Mon Sep 17 00:00:00 2001
From: Yugend <jugendd@mail.ru>
Date: Fri, 15 Mar 2024 18:00:54 +0300
Subject: [PATCH] second part of NULL pointer checks

Conflict:NA
Reference:https://github.com/linux-audit/audit-userspace/commit/15d29a145ebe67cae52316871fcdedb5a19ce628

---
 audisp/plugins/zos-remote/zos-remote-queue.c |  5 +++++
 audisp/queue.c                               |  5 +++++
 auparse/normalize-llist.c                    |  3 +++
 auparse/normalize.c                          |  9 +++++++++
 lib/gen_tables.c                             | 10 ++++++++--
 src/ausearch-lol.c                           | 12 ++++++++++++
 src/ausearch-nvpair.c                        |  3 +++
 src/ausearch-string.c                        |  3 +++
 8 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/audisp/plugins/zos-remote/zos-remote-queue.c b/audisp/plugins/zos-remote/zos-remote-queue.c
index 37d91bd8..47dd006e 100644
--- a/audisp/plugins/zos-remote/zos-remote-queue.c
+++ b/audisp/plugins/zos-remote/zos-remote-queue.c
@@ -130,6 +130,11 @@ void increase_queue_depth(unsigned int size)
         void *tmp_q;
 
         tmp_q = realloc(q, size * sizeof(BerElement *));
+        if (tmp_q == NULL) {
+			log_err("Memory allocation error");;
+			pthread_mutex_unlock(&queue_lock);
+			return;
+		}
         q = tmp_q;
         for (i=q_depth; i<size; i++)
             q[i] = NULL;
diff --git a/audisp/queue.c b/audisp/queue.c
index 6898d09f..76b62593 100644
--- a/audisp/queue.c
+++ b/audisp/queue.c
@@ -229,6 +229,11 @@ void increase_queue_depth(unsigned int size)
 		void *tmp_q;
 
 		tmp_q = realloc(q, size * sizeof(event_t *));
+		if (tmp_q == NULL) {
+			fprintf(stderr, "Memory allocation error");
+			pthread_mutex_unlock(&queue_lock);
+			return;
+		}
 		q = tmp_q;
 		for (i=q_depth; i<size; i++)
 			q[i] = NULL;
diff --git a/auparse/normalize-llist.c b/auparse/normalize-llist.c
index fd9d6cc8..32d5f124 100644
--- a/auparse/normalize-llist.c
+++ b/auparse/normalize-llist.c
@@ -66,6 +66,9 @@ void cllist_append(cllist *l, uint32_t num, void *data)
 	data_node *newnode;
 
 	newnode = malloc(sizeof(data_node));
+	if (newnode == NULL) {
+		return;
+	}
 
 	newnode->num = num;
 	newnode->data = data;
diff --git a/auparse/normalize.c b/auparse/normalize.c
index ae6e3d2d..58d28213 100644
--- a/auparse/normalize.c
+++ b/auparse/normalize.c
@@ -1191,6 +1191,11 @@ static int normalize_compound(auparse_state_t *au)
 		if (f) {
 			const char *exe = auparse_interpret_field(au);
 			D.how = strdup(exe);
+			if (D.how == NULL) {
+				fprintf(stderr, "Memory allocation error");
+				free((void *)syscall);
+				return 1;
+			}
 			if ((strncmp(D.how, "/usr/bin/python", 15) == 0) ||
 			    (strncmp(D.how, "/usr/bin/sh", 11) == 0) ||
 			    (strncmp(D.how, "/usr/bin/bash", 13) == 0) ||
@@ -1999,6 +2004,10 @@ map:
 	if (f) {
 		const char *exe = auparse_interpret_field(au);
 		D.how = strdup(exe);
+		if (D.how == NULL) {
+			fprintf(stderr, "Memory allocation error");
+			return 1;
+		}
 		if ((strncmp(D.how, "/usr/bin/python", 15) == 0) ||
 		    (strncmp(D.how, "/usr/bin/sh", 11) == 0) ||
 		    (strncmp(D.how, "/usr/bin/bash", 13) == 0) ||
diff --git a/lib/gen_tables.c b/lib/gen_tables.c
index 3326759d..4ff233d0 100644
--- a/lib/gen_tables.c
+++ b/lib/gen_tables.c
@@ -271,7 +271,10 @@ output_i2s(const char *prefix)
 	}
 
 	unique_values = malloc(NUM_VALUES * sizeof(*unique_values));
-	assert(unique_values != NULL);
+	if (unique_values == NULL) {
+		fprintf(stderr, "Memory allocation error");
+		abort();
+	}
 	n = 0;
 	for (i = 0; i < NUM_VALUES; i++) {
 		if (n == 0 || unique_values[n - 1].val != values[i].val) {
@@ -351,7 +354,10 @@ output_i2s_transtab(const char *prefix)
 		printf("{%d,%zu},", values[i].val, values[i].s_offset);
 	}
 	uc_prefix = strdup(prefix);
-	assert(uc_prefix != NULL);
+	if (uc_prefix == NULL) {
+		fprintf(stderr, "Memory allocation error");
+		abort();
+	}
 	for (i = 0; uc_prefix[i] != '\0'; i++)
 		uc_prefix[i] = toupper((unsigned char)uc_prefix[i]);
 	printf("\n"
diff --git a/src/ausearch-lol.c b/src/ausearch-lol.c
index bcfb9ad8..c2140b7e 100644
--- a/src/ausearch-lol.c
+++ b/src/ausearch-lol.c
@@ -47,6 +47,10 @@ void lol_create(lol *lo)
 	lo->maxi = -1;
 	lo->limit = ARRAY_LIMIT;
 	lo->array = (lolnode *)malloc(size);
+	if (lo->array == NULL) {
+		fprintf(stderr, "Memory allocation error");
+		return;
+	}
 	memset(lo->array, 0, size);
 }
 
@@ -305,6 +309,10 @@ int lol_add_record(lol *lo, char *buff)
 	n.a1 = 0L;
 	n.type = e.type;
 	n.message = strdup(buff);
+	if(n.message == NULL) {
+		fprintf(stderr, "Memory allocation error");
+		return 0;
+	}
 	ptr = strchr(n.message, AUDIT_INTERP_SEPARATOR);
 	if (ptr) {
 		n.mlen = ptr - n.message;
@@ -359,6 +367,10 @@ int lol_add_record(lol *lo, char *buff)
 
 	// Create new event and fill it in
 	l = malloc(sizeof(llist));
+	if (l == NULL) {
+		fprintf(stderr, "Memory allocation error");
+		return 0;
+	}
 	list_create(l);
 	l->e.milli = e.milli;
 	l->e.sec = e.sec;
diff --git a/src/ausearch-nvpair.c b/src/ausearch-nvpair.c
index 8d0088e5..c344c27c 100644
--- a/src/ausearch-nvpair.c
+++ b/src/ausearch-nvpair.c
@@ -37,6 +37,9 @@ void search_list_create(nvlist *l)
 void search_list_append(nvlist *l, nvnode *node)
 {
 	nvnode* newnode = malloc(sizeof(nvnode));
+	if (newnode == NULL) {
+		return;
+	}
 
 	newnode->name = node->name;
 	newnode->val = node->val;
diff --git a/src/ausearch-string.c b/src/ausearch-string.c
index fbbacd77..f875bb2c 100644
--- a/src/ausearch-string.c
+++ b/src/ausearch-string.c
@@ -49,6 +49,9 @@ void slist_append(slist *l, const snode *node)
 	snode* newnode;
 
 	newnode = malloc(sizeof(snode));
+	if (newnode == NULL) {
+		return;
+	}
 
 	if (node->str)
 		newnode->str = node->str;
-- 
2.33.0

