Skip to contentSkip to conversation

bpf: alloc_record_per_cpu Add null check after malloc

PATCH In review Gaurav Singh 2 participants 2 messages Jun 9, 2020

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: alloc_record_per_cpu Add null check after malloc

The memset call is made right after malloc call. To fix this, add the null check right after malloc and then do memset.

---
 samples/bpf/xdp_rxq_info_user.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
samples/bpf/xdp_rxq_info_user.c +1 -1
index 4fe47502ebed..490b07b7df78 100644
@@ -202,11 +202,11 @@ static struct datarec *alloc_record_per_cpu(void)
 
 	size = sizeof(struct datarec) * nr_cpus;
 	array = malloc(size);
-	memset(array, 0, size);
 	if (!array) {
 		fprintf(stderr, "Mem alloc error (nr_cpus:%u)\n", nr_cpus);
 		exit(EXIT_FAIL_MEM);
 	}
+	memset(array, 0, size);
 	return array;
 }
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 2 lines · On Tue, 9 Jun 2020 08:08:03 -0400 Gaurav Singh <[email protected]> wrote:

The memset call is made right after malloc call. To fix this, add the null check right after malloc and then do memset.

Did you read the section about how long lines should be in desc?

Quoted 11 lines
---
 samples/bpf/xdp_rxq_info_user.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
samples/bpf/xdp_rxq_info_user.c +0 -0
index 4fe47502ebed..490b07b7df78 100644
@@ -202,11 +202,11 @@ static struct datarec *alloc_record_per_cpu(void)
 

samples/bpf/xdp_rxq_info_user.c static struct datarec *alloc_record_per_cpu(void) · line 211

Quoted 10 lines
samples/bpf/xdp_rxq_info_user.c +1 -1
 	size = sizeof(struct datarec) * nr_cpus;
 	array = malloc(size);
-	memset(array, 0, size);
 	if (!array) {
 		fprintf(stderr, "Mem alloc error (nr_cpus:%u)\n", nr_cpus);
 		exit(EXIT_FAIL_MEM);
 	}
+	memset(array, 0, size);
 	return array;
 }

Looking at code, this bug happen in more places. Please fix up all locations.

I think this fix should go through the "bpf" tree. Please read:
https://github.com/torvalds/linux/blob/master/Documentation/bpf/bpf_devel_QA.rst

Signature · 4 lines
Best regards,
  Jesper Dangaard Brouer
  MSc.CS, Principal Kernel Engineer at Red Hat
  LinkedIn: http://www.linkedin.com/in/brouer