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

Reference:https://github.com/linux-audit/audit-userspace/commit/b046de44454fa2616dbb8899f1b41d65ce876e33
Conflict:auparse/auparse.c

---
 audisp/audispd-llist.c            | 3 +++
 auparse/auparse.c                 | 7 +++++++
 src/auditctl-llist.c              | 3 +++
 src/auditctl.c                    | 5 +++++
 src/ausearch-avc.c                | 3 +++
 src/ausearch-int.c                | 3 +++
 src/ausearch-llist.c              | 3 +++
 tools/aulastlog/aulastlog-llist.c | 3 +++
 8 files changed, 30 insertions(+)

diff --git a/audisp/audispd-llist.c b/audisp/audispd-llist.c
index c562a72..c338327 100644
--- a/audisp/audispd-llist.c
+++ b/audisp/audispd-llist.c
@@ -74,6 +74,9 @@ void plist_append(conf_llist *l, plugin_conf_t *p)
 	lnode* newnode;
 
 	newnode = malloc(sizeof(lnode));
+	if (newnode == NULL) {
+		return;
+	}
 
 	if (p) {
 		void *pp = malloc(sizeof(struct plugin_conf));
diff --git a/auparse/auparse.c b/auparse/auparse.c
index 036f022..abbc17b 100644
--- a/auparse/auparse.c
+++ b/auparse/auparse.c
@@ -95,6 +95,11 @@ static int setup_log_file_array(auparse_state_t *au)
 	}
 	num--;
 	tmp = malloc((num+2)*sizeof(char *));
+	if (!tmp) {
+		fprintf(stderr, "No memory\n");
+		aup_free_config(&config);
+		return 1;
+	}
 
         /* Got it, now process logs from last to first */
 	if (num > 0)
@@ -468,6 +473,8 @@ auparse_state_t *auparse_init(ausource_t source, const void *b)
 			if (access(b, R_OK))
 				goto bad_exit;
 			tmp = malloc(2*sizeof(char *));
+			if (tmp == NULL) 
+				goto bad_exit;
 			tmp[0] = strdup(b);
 			tmp[1] = NULL;
 			au->source_list = tmp;
diff --git a/src/auditctl-llist.c b/src/auditctl-llist.c
index 6e70ef1..ae9776b 100644
--- a/src/auditctl-llist.c
+++ b/src/auditctl-llist.c
@@ -64,6 +64,9 @@ void list_append(llist *l, struct audit_rule_data *r, size_t sz)
 	lnode* newnode;
 
 	newnode = malloc(sizeof(lnode));
+	if (newnode == NULL) {
+		return;
+	}
 
 	if (r) {
 		void *rr = malloc(sz);
diff --git a/src/auditctl.c b/src/auditctl.c
index 778b374..8942195 100644
--- a/src/auditctl.c
+++ b/src/auditctl.c
@@ -1323,6 +1323,11 @@ static int fileopt(const char *file)
 		}
 		i = 0;
 		fields = malloc(nf * sizeof(char *));
+		if (fields == NULL) {
+			audit_msg(LOG_ERR, "Memory allocation error");
+			return 1;
+		}
+		
 		fields[i++] = "auditctl";
 		fields[i++] = ptr;
 		while( (ptr=audit_strsplit(NULL)) && (i < nf-1)) {
diff --git a/src/ausearch-avc.c b/src/ausearch-avc.c
index 10d153f..6aa98c7 100644
--- a/src/ausearch-avc.c
+++ b/src/ausearch-avc.c
@@ -67,6 +67,9 @@ void alist_append(alist *l, anode *node)
 	anode* newnode;
 
 	newnode = malloc(sizeof(anode));
+	if (newnode == NULL) {
+		return;
+	}
 
 	if (node->scontext)
 		newnode->scontext = node->scontext;
diff --git a/src/ausearch-int.c b/src/ausearch-int.c
index 718dacd..0e8b0ff 100644
--- a/src/ausearch-int.c
+++ b/src/ausearch-int.c
@@ -46,6 +46,9 @@ void ilist_append(ilist *l, int num, unsigned int hits, int aux)
 	int_node* newnode;
 
 	newnode = malloc(sizeof(int_node));
+	if (newnode == NULL) {
+		return;
+	}
 
 	newnode->num = num;
 	newnode->hits = hits;
diff --git a/src/ausearch-llist.c b/src/ausearch-llist.c
index ef5503c..3b4ff26 100644
--- a/src/ausearch-llist.c
+++ b/src/ausearch-llist.c
@@ -107,6 +107,9 @@ void list_append(llist *l, lnode *node)
 	lnode* newnode;
 
 	newnode = malloc(sizeof(lnode));
+	if (newnode == NULL) {
+		return;
+	}
 
 	if (node->message)
 		newnode->message = node->message;
diff --git a/tools/aulastlog/aulastlog-llist.c b/tools/aulastlog/aulastlog-llist.c
index 84882ca..779afb5 100644
--- a/tools/aulastlog/aulastlog-llist.c
+++ b/tools/aulastlog/aulastlog-llist.c
@@ -46,6 +46,9 @@ void list_append(llist *l, lnode *node)
 	lnode* newnode;
 
 	newnode = malloc(sizeof(lnode));
+	if (newnode == NULL) {
+		return;
+	}
 
 	newnode->sec = node->sec;
 	newnode->uid = node->uid;
-- 
2.33.0

