SELinux Archive on lore.kernel.org
 help / color / Atom feed
From: James Carter <jwcart2@tycho.nsa.gov>
To: selinux@vger.kernel.org
Subject: [PATCH] libsepol: Remove cil_mem_error_handler() function pointer
Date: Fri, 13 Sep 2019 12:47:13 -0400
Message-ID: <20190913164713.29432-1-jwcart2@tycho.nsa.gov> (raw)

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


                 reply index

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publically to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190913164713.29432-1-jwcart2@tycho.nsa.gov \
    --to=jwcart2@tycho.nsa.gov \
    --cc=selinux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

SELinux Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/selinux/0 selinux/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 selinux selinux/ https://lore.kernel.org/selinux \
		selinux@vger.kernel.org selinux@archiver.kernel.org
	public-inbox-index selinux


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.selinux


AGPL code for this site: git clone https://public-inbox.org/ public-inbox