diff mbox series

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

Message ID 20190901180636.31586-3-nicolas.iooss@m4x.org (mailing list archive)
State Accepted
Headers show
Series Fix issues found by static analyzers | expand

Commit Message

Nicolas Iooss Sept. 1, 2019, 6:06 p.m. UTC
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@m4x.org>
---
 libsepol/cil/src/cil_mem.c | 5 +++++
 1 file changed, 5 insertions(+)

Comments

James Carter Sept. 10, 2019, 6:48 p.m. UTC | #1
On 9/1/19 2:06 PM, Nicolas Iooss wrote:
> 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@m4x.org>
> ---
>   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();
>   	}
> 

I am going to have to think about this one.

I think the real answer might be to rip out the (*cil_mem_error_handler)() 
statements and replace them with what is in the default handler. I notice now 
that even though there is a cil_set_mem_error_handler() function, it is not used 
anywhere.

>   	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;
>
diff mbox series

Patch

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;