diff mbox

[8/8] Introduce memory allocation return value check

Message ID 3ded3d5442aea11028d1eddfb21f2eee50f27e14.1435048542.git.kamalesh@linux.vnet.ibm.com
State Changes Requested
Headers show

Commit Message

Kamalesh Babulal June 23, 2015, 8:37 a.m. UTC
In pci_std_swizzle_irq_map(), check if the memory allocation
of interrupt-mask returns a valid pointer before using it.

Fixes Coverity defect#97854.

Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
---
 core/pci.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

Comments

Sam Mendoza-Jonas June 24, 2015, 12:03 a.m. UTC | #1
On 23/06/15 18:37, Kamalesh Babulal wrote:
> In pci_std_swizzle_irq_map(), check if the memory allocation
> of interrupt-mask returns a valid pointer before using it.
> 
> Fixes Coverity defect#97854.
> 
> Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
> ---
>  core/pci.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/core/pci.c b/core/pci.c
> index fe23d6c..ae71814 100644
> --- a/core/pci.c
> +++ b/core/pci.c
> @@ -1087,6 +1087,10 @@ void pci_std_swizzle_irq_map(struct dt_node *np,
>  	}
>  	map_size = esize * edevcount * 4 * sizeof(uint32_t);
>  	map = p = zalloc(map_size);
> +	if (!map) {
> +		prlog(PR_DEBUG, "Failed to allocate interrupt-map-mask !\n");
> +		goto fail;
> +	}
>  
>  	for (dev = 0; dev < edevcount; dev++) {
>  		for (irq = 0; irq < 4; irq++) {
> @@ -1111,7 +1115,9 @@ void pci_std_swizzle_irq_map(struct dt_node *np,
>  	}
>  
>  	dt_add_property(np, "interrupt-map", map, map_size);
> -	free(map);
> +fail:
> +	if (map)
> +		free(map);
>  }

All good but you don't need to do if(map) since free(NULL) is a noop.

Cheers,
Sam

>  
>  static void pci_add_slot_properties(struct phb *phb, struct pci_slot_info *info,
>
Gavin Shan June 24, 2015, 12:06 a.m. UTC | #2
On Tue, Jun 23, 2015 at 02:07:38PM +0530, Kamalesh Babulal wrote:
>In pci_std_swizzle_irq_map(), check if the memory allocation
>of interrupt-mask returns a valid pointer before using it.
>
>Fixes Coverity defect#97854.
>
>Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
>---
> core/pci.c | 8 +++++++-
> 1 file changed, 7 insertions(+), 1 deletion(-)
>
>diff --git a/core/pci.c b/core/pci.c
>index fe23d6c..ae71814 100644
>--- a/core/pci.c
>+++ b/core/pci.c
>@@ -1087,6 +1087,10 @@ void pci_std_swizzle_irq_map(struct dt_node *np,
> 	}
> 	map_size = esize * edevcount * 4 * sizeof(uint32_t);
> 	map = p = zalloc(map_size);
>+	if (!map) {
>+		prlog(PR_DEBUG, "Failed to allocate interrupt-map-mask !\n");
>+		goto fail;
>+	}

If the memory chunk can't be allocated, we needn't try to free it
with check.

> 
> 	for (dev = 0; dev < edevcount; dev++) {
> 		for (irq = 0; irq < 4; irq++) {
>@@ -1111,7 +1115,9 @@ void pci_std_swizzle_irq_map(struct dt_node *np,
> 	}
> 
> 	dt_add_property(np, "interrupt-map", map, map_size);
>-	free(map);
>+fail:
>+	if (map)
>+		free(map);

The free() won't be called for ever.

> }
> 
> static void pci_add_slot_properties(struct phb *phb, struct pci_slot_info *info,

Thanks,
Gavin
Kamalesh Babulal June 24, 2015, 6:09 a.m. UTC | #3
On 06/24/2015 05:36 AM, Gavin Shan wrote:
> On Tue, Jun 23, 2015 at 02:07:38PM +0530, Kamalesh Babulal wrote:
>> In pci_std_swizzle_irq_map(), check if the memory allocation
>> of interrupt-mask returns a valid pointer before using it.
>>
>> Fixes Coverity defect#97854.
>>
>> Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
>> ---
>> core/pci.c | 8 +++++++-
>> 1 file changed, 7 insertions(+), 1 deletion(-)
>>
>> diff --git a/core/pci.c b/core/pci.c
>> index fe23d6c..ae71814 100644
>> --- a/core/pci.c
>> +++ b/core/pci.c
>> @@ -1087,6 +1087,10 @@ void pci_std_swizzle_irq_map(struct dt_node *np,
>> 	}
>> 	map_size = esize * edevcount * 4 * sizeof(uint32_t);
>> 	map = p = zalloc(map_size);
>> +	if (!map) {
>> +		prlog(PR_DEBUG, "Failed to allocate interrupt-map-mask !\n");
>> +		goto fail;
>> +	}
> If the memory chunk can't be allocated, we needn't try to free it
> with check.
>
>> 	for (dev = 0; dev < edevcount; dev++) {
>> 		for (irq = 0; irq < 4; irq++) {
>> @@ -1111,7 +1115,9 @@ void pci_std_swizzle_irq_map(struct dt_node *np,
>> 	}
>>
>> 	dt_add_property(np, "interrupt-map", map, map_size);
>> -	free(map);
>> +fail:
>> +	if (map)
>> +		free(map);
> The free() won't be called for ever.
>
>

Thanks for the review. Will repost the patch.
diff mbox

Patch

diff --git a/core/pci.c b/core/pci.c
index fe23d6c..ae71814 100644
--- a/core/pci.c
+++ b/core/pci.c
@@ -1087,6 +1087,10 @@  void pci_std_swizzle_irq_map(struct dt_node *np,
 	}
 	map_size = esize * edevcount * 4 * sizeof(uint32_t);
 	map = p = zalloc(map_size);
+	if (!map) {
+		prlog(PR_DEBUG, "Failed to allocate interrupt-map-mask !\n");
+		goto fail;
+	}
 
 	for (dev = 0; dev < edevcount; dev++) {
 		for (irq = 0; irq < 4; irq++) {
@@ -1111,7 +1115,9 @@  void pci_std_swizzle_irq_map(struct dt_node *np,
 	}
 
 	dt_add_property(np, "interrupt-map", map, map_size);
-	free(map);
+fail:
+	if (map)
+		free(map);
 }
 
 static void pci_add_slot_properties(struct phb *phb, struct pci_slot_info *info,