diff mbox

[2/2] libsepol: use the number of elements in calloc first argument

Message ID 20170520101141.6207-2-nicolas.iooss@m4x.org (mailing list archive)
State Not Applicable
Headers show

Commit Message

Nicolas Iooss May 20, 2017, 10:11 a.m. UTC
When allocating an array with calloc(), the first argument usually is
the number of items and the second one the size of an item. Doing so
silences a warning reported by clang's static analyzer:

    kernel_to_cil.c:2050:14: warning: Call to 'calloc' has an allocation
    size of 0 bytes.
            cond_data = calloc(sizeof(struct cond_data), num);
                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org>
---
 libsepol/src/kernel_to_cil.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Stephen Smalley May 23, 2017, 8 p.m. UTC | #1
On Sat, 2017-05-20 at 12:11 +0200, Nicolas Iooss wrote:
> When allocating an array with calloc(), the first argument usually is
> the number of items and the second one the size of an item. Doing so
> silences a warning reported by clang's static analyzer:
> 
>     kernel_to_cil.c:2050:14: warning: Call to 'calloc' has an
> allocation
>     size of 0 bytes.
>             cond_data = calloc(sizeof(struct cond_data), num);
>                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org>

Thanks, applied both.

> ---
>  libsepol/src/kernel_to_cil.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/libsepol/src/kernel_to_cil.c
> b/libsepol/src/kernel_to_cil.c
> index 3a1c0be76f45..498e2947f6cb 100644
> --- a/libsepol/src/kernel_to_cil.c
> +++ b/libsepol/src/kernel_to_cil.c
> @@ -2043,7 +2043,7 @@ static int write_cond_nodes_to_cil(FILE *out,
> struct policydb *pdb)
>  		num++;
>  	}
>  
> -	cond_data = calloc(sizeof(struct cond_data), num);
> +	cond_data = calloc(num, sizeof(struct cond_data));
>  	if (!cond_data) {
>  		rc = -1;
>  		goto exit;
diff mbox

Patch

diff --git a/libsepol/src/kernel_to_cil.c b/libsepol/src/kernel_to_cil.c
index 3a1c0be76f45..498e2947f6cb 100644
--- a/libsepol/src/kernel_to_cil.c
+++ b/libsepol/src/kernel_to_cil.c
@@ -2043,7 +2043,7 @@  static int write_cond_nodes_to_cil(FILE *out, struct policydb *pdb)
 		num++;
 	}
 
-	cond_data = calloc(sizeof(struct cond_data), num);
+	cond_data = calloc(num, sizeof(struct cond_data));
 	if (!cond_data) {
 		rc = -1;
 		goto exit;