diff mbox series

libselinux: add check for calloc in check_booleans

Message ID 20230618231702.118125-1-luhuaxin1@huawei.com (mailing list archive)
State Accepted
Commit d8edd363be31
Delegated to: Petr Lautrbach
Headers show
Series libselinux: add check for calloc in check_booleans | expand

Commit Message

Huaxin Lu June 18, 2023, 11:17 p.m. UTC
Check the return value of calloc() to avoid null pointer reference.

Signed-off-by: Huaxin Lu <luhuaxin1@huawei.com>
---
 libselinux/src/audit2why.c | 6 ++++++
 1 file changed, 6 insertions(+)

Comments

Petr Lautrbach June 27, 2023, 2:57 p.m. UTC | #1
Huaxin Lu <luhuaxin1@huawei.com> writes:

> Check the return value of calloc() to avoid null pointer reference.
>
> Signed-off-by: Huaxin Lu <luhuaxin1@huawei.com>
> ---
>  libselinux/src/audit2why.c | 6 ++++++
>  1 file changed, 6 insertions(+)
>
> diff --git a/libselinux/src/audit2why.c b/libselinux/src/audit2why.c
> index ba1a66e..2def89f 100644
> --- a/libselinux/src/audit2why.c
> +++ b/libselinux/src/audit2why.c
> @@ -149,6 +149,12 @@ static int check_booleans(struct boolean_t **bools)
>  
>  	if (fcnt > 0) {
>  		*bools = calloc(sizeof(struct boolean_t), fcnt + 1);
> +		if (!*bools) {
> +			PyErr_SetString( PyExc_MemoryError, "Out of memory\n");
> +			free(foundlist);
> +			return 0;
> +		}
> +	

There's a trailing whitespace, but it's not necessary to resend the patch.

Acked-by: Petr Lautrbach <lautrbach@redhat.com>


>  		struct boolean_t *b = *bools;
>  		for (i = 0; i < fcnt; i++) {
>  			int ctr = foundlist[i];
> -- 
> 2.33.0
Petr Lautrbach June 30, 2023, 12:29 p.m. UTC | #2
Petr Lautrbach <plautrba@redhat.com> writes:

> Huaxin Lu <luhuaxin1@huawei.com> writes:
>
>> Check the return value of calloc() to avoid null pointer reference.
>>
>> Signed-off-by: Huaxin Lu <luhuaxin1@huawei.com>
>> ---
>>  libselinux/src/audit2why.c | 6 ++++++
>>  1 file changed, 6 insertions(+)
>>
>> diff --git a/libselinux/src/audit2why.c b/libselinux/src/audit2why.c
>> index ba1a66e..2def89f 100644
>> --- a/libselinux/src/audit2why.c
>> +++ b/libselinux/src/audit2why.c
>> @@ -149,6 +149,12 @@ static int check_booleans(struct boolean_t **bools)
>>  
>>  	if (fcnt > 0) {
>>  		*bools = calloc(sizeof(struct boolean_t), fcnt + 1);
>> +		if (!*bools) {
>> +			PyErr_SetString( PyExc_MemoryError, "Out of memory\n");
>> +			free(foundlist);
>> +			return 0;
>> +		}
>> +	
>
> There's a trailing whitespace, but it's not necessary to resend the patch.
>
> Acked-by: Petr Lautrbach <lautrbach@redhat.com>
>

merged, thanks

>>  		struct boolean_t *b = *bools;
>>  		for (i = 0; i < fcnt; i++) {
>>  			int ctr = foundlist[i];
>> -- 
>> 2.33.0
diff mbox series

Patch

diff --git a/libselinux/src/audit2why.c b/libselinux/src/audit2why.c
index ba1a66e..2def89f 100644
--- a/libselinux/src/audit2why.c
+++ b/libselinux/src/audit2why.c
@@ -149,6 +149,12 @@  static int check_booleans(struct boolean_t **bools)
 
 	if (fcnt > 0) {
 		*bools = calloc(sizeof(struct boolean_t), fcnt + 1);
+		if (!*bools) {
+			PyErr_SetString( PyExc_MemoryError, "Out of memory\n");
+			free(foundlist);
+			return 0;
+		}
+	
 		struct boolean_t *b = *bools;
 		for (i = 0; i < fcnt; i++) {
 			int ctr = foundlist[i];