selinux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] libsepol: Remove cil_mem_error_handler() function pointer
@ 2019-09-13 16:47 James Carter
  0 siblings, 0 replies; only message in thread
From: James Carter @ 2019-09-13 16:47 UTC (permalink / raw)
  To: selinux

As reported by Nicolas Iooss (nicolas.iooss@m4x.org), static analyzers
have problems understanding that the default memory error handler does
not return since it is called through the cil_mem_error_handler()
function pointer. This results in a number of false positive warnings
about null pointer dereferencing.

Since the ability to set the cil_mem_error_handler() is only through
the function cil_set_mem_error_handler() which is never used and whose
definition is not in any header file, remove that function, remove the
use of cil_mem_error_handler() and directly in-line the contents of
the default handler, cil_default_mem_error_handler().

Signed-off-by: James Carter <jwcart2@tycho.nsa.gov>
---
 libsepol/cil/src/cil_mem.c | 28 ++++++++++------------------
 1 file changed, 10 insertions(+), 18 deletions(-)

diff --git a/libsepol/cil/src/cil_mem.c b/libsepol/cil/src/cil_mem.c
index 12c59be2..f73021b5 100644
--- a/libsepol/cil/src/cil_mem.c
+++ b/libsepol/cil/src/cil_mem.c
@@ -34,19 +34,6 @@
 
 #include "cil_log.h"
 
-__attribute__((noreturn)) void cil_default_mem_error_handler(void)
-{
-	cil_log(CIL_ERR, "Failed to allocate memory\n");
-	exit(1);
-}
-
-void (*cil_mem_error_handler)(void) = &cil_default_mem_error_handler;
-
-void cil_set_mem_error_handler(void (*handler)(void))
-{
-	cil_mem_error_handler = handler;
-}
-
 void *cil_malloc(size_t size)
 {
 	void *mem = malloc(size);
@@ -54,7 +41,8 @@ void *cil_malloc(size_t size)
 		if (size == 0) {
 			return NULL;
 		}
-		(*cil_mem_error_handler)();
+		cil_log(CIL_ERR, "Failed to allocate memory\n");
+		exit(1);
 	}
 
 	return mem;
@@ -64,7 +52,8 @@ 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)();
+		cil_log(CIL_ERR, "Failed to allocate memory\n");
+		exit(1);
 	}
 
 	return mem;
@@ -77,7 +66,8 @@ void *cil_realloc(void *ptr, size_t size)
 		if (size == 0) {
 			return NULL;
 		}
-		(*cil_mem_error_handler)();
+		cil_log(CIL_ERR, "Failed to allocate memory\n");
+		exit(1);
 	}
 
 	return mem;
@@ -94,7 +84,8 @@ char *cil_strdup(const char *str)
 
 	mem = strdup(str);
 	if (mem == NULL) {
-		(*cil_mem_error_handler)();
+		cil_log(CIL_ERR, "Failed to allocate memory\n");
+		exit(1);
 	}
 
 	return mem;
@@ -110,7 +101,8 @@ __attribute__ ((format (printf, 2, 3))) int cil_asprintf(char **strp, const char
 	va_end(ap);
 
 	if (rc == -1) {
-		(*cil_mem_error_handler)();
+		cil_log(CIL_ERR, "Failed to allocate memory\n");
+		exit(1);
 	}
 
 	return rc;
-- 
2.21.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2019-09-13 16:52 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-13 16:47 [PATCH] libsepol: Remove cil_mem_error_handler() function pointer James Carter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).