[PATCH 2/9] libsepol/cil: help static analyzers by aborting when an allocation fails

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

 



When allocating memory with cil_* helpers, if malloc/calloc/realloc/...
failed, (*cil_mem_error_handler)() is called. Implementations of this
function are expected not to return to the caller, and the default one
calls exit(1) to ensure this. In order for static analyzers to find out
that cil_malloc/cil_realloc/... never returns a NULL pointer when
failing to allocate some memory, introduce a call to abort().

This decreases the number of false positive warnings about null pointer
dereferences reported by Infer static analyzer.

Signed-off-by: Nicolas Iooss <nicolas.iooss@xxxxxxx>
---
 libsepol/cil/src/cil_mem.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/libsepol/cil/src/cil_mem.c b/libsepol/cil/src/cil_mem.c
index 12c59be21914..885431d8a8fd 100644
--- a/libsepol/cil/src/cil_mem.c
+++ b/libsepol/cil/src/cil_mem.c
@@ -55,6 +55,7 @@ void *cil_malloc(size_t size)
 			return NULL;
 		}
 		(*cil_mem_error_handler)();
+		abort();
 	}
 
 	return mem;
@@ -65,6 +66,7 @@ void *cil_calloc(size_t num_elements, size_t element_size)
 	void *mem = calloc(num_elements, element_size);
 	if (mem == NULL){
 		(*cil_mem_error_handler)();
+		abort();
 	}
 
 	return mem;
@@ -78,6 +80,7 @@ void *cil_realloc(void *ptr, size_t size)
 			return NULL;
 		}
 		(*cil_mem_error_handler)();
+		abort();
 	}
 
 	return mem;
@@ -95,6 +98,7 @@ char *cil_strdup(const char *str)
 	mem = strdup(str);
 	if (mem == NULL) {
 		(*cil_mem_error_handler)();
+		abort();
 	}
 
 	return mem;
@@ -111,6 +115,7 @@ __attribute__ ((format (printf, 2, 3))) int cil_asprintf(char **strp, const char
 
 	if (rc == -1) {
 		(*cil_mem_error_handler)();
+		abort();
 	}
 
 	return rc;
-- 
2.22.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