[PATCH] secilc: add check for malloc in secilc

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Check the return value of malloc() to avoid null pointer reference.

Signed-off-by: Huaxin Lu <luhuaxin1@xxxxxxxxxx>
---
 secilc/secil2conf.c | 6 ++++++
 secilc/secil2tree.c | 6 ++++++
 secilc/secilc.c     | 6 ++++++
 3 files changed, 18 insertions(+)

diff --git a/secilc/secil2conf.c b/secilc/secil2conf.c
index c49522e..bf050f3 100644
--- a/secilc/secil2conf.c
+++ b/secilc/secil2conf.c
@@ -152,6 +152,12 @@ int main(int argc, char *argv[])
 		file_size = filedata.st_size;
 
 		buffer = malloc(file_size);
+		if (!buffer) {
+			fprintf(stderr, "Out of memory\n");
+			rc = SEPOL_ERR;
+			goto exit;
+		}
+
 		rc = fread(buffer, file_size, 1, file);
 		if (rc != 1) {
 			fprintf(stderr, "Failure reading file: %s\n", argv[i]);
diff --git a/secilc/secil2tree.c b/secilc/secil2tree.c
index e5cdf6b..d04566d 100644
--- a/secilc/secil2tree.c
+++ b/secilc/secil2tree.c
@@ -158,6 +158,12 @@ int main(int argc, char *argv[])
 		file_size = filedata.st_size;
 
 		buffer = malloc(file_size);
+		if (!buffer) {
+			fprintf(stderr, "Out of memory\n");
+			rc = SEPOL_ERR;
+			goto exit;
+		}
+
 		rc = fread(buffer, file_size, 1, file);
 		if (rc != 1) {
 			fprintf(stderr, "Failure reading file: %s\n", argv[i]);
diff --git a/secilc/secilc.c b/secilc/secilc.c
index 80d3583..f3102ca 100644
--- a/secilc/secilc.c
+++ b/secilc/secilc.c
@@ -286,6 +286,12 @@ int main(int argc, char *argv[])
 		}
 
 		buffer = malloc(file_size);
+		if (!buffer) {
+			fprintf(stderr, "Out of memory\n");
+			rc = SEPOL_ERR;
+			goto exit;
+		}
+
 		rc = fread(buffer, file_size, 1, file);
 		if (rc != 1) {
 			fprintf(stderr, "Failure reading file: %s\n", argv[i]);
-- 
2.33.0




[Index of Archives]     [Selinux Refpolicy]     [Linux SGX]     [Fedora Users]     [Fedora Desktop]     [Yosemite Photos]     [Yosemite Camping]     [Yosemite Campsites]     [KDE Users]     [Gnome Users]

  Powered by Linux