Skip to contentSkip to conversation

xdp_rxq_info_user: Add null check after malloc

PATCH In review Gaurav Singh 2 participants 2 messages Jun 10, 2020 – Jun 11, 2020

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

PATCH 1/1 xdp_rxq_info_user: Add null check after malloc

The memset call is made right after malloc call which can return a NULL pointer upon failure causing a segmentation fault. Fix this by adding a null check right after malloc() and then do memset().

---
 samples/bpf/xdp_rxq_info_user.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
samples/bpf/xdp_rxq_info_user.c +3 -3
index 4fe47502ebed..2d03c84a4cca 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;
 }
 
@@ -218,11 +218,11 @@ static struct record *alloc_record_per_rxq(void)
 
 	size = sizeof(struct record) * nr_rxqs;
 	array = malloc(size);
-	memset(array, 0, size);
 	if (!array) {
 		fprintf(stderr, "Mem alloc error (nr_rxqs:%u)\n", nr_rxqs);
 		exit(EXIT_FAIL_MEM);
 	}
+	memset(array, 0, size);
 	return array;
 }
 
@@ -233,11 +233,11 @@ static struct stats_record *alloc_stats_record(void)
 	int i;
 
 	rec = malloc(sizeof(*rec));
-	memset(rec, 0, sizeof(*rec));
 	if (!rec) {
 		fprintf(stderr, "Mem alloc error\n");
 		exit(EXIT_FAIL_MEM);
 	}
+	memset(rec, 0, sizeof(*rec));
 	rec->rxq = alloc_record_per_rxq();
 	for (i = 0; i < nr_rxqs; i++)
 		rec->rxq[i].cpu = alloc_record_per_cpu();
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 6 lines · Gaurav Singh

The memset call is made right after malloc call which can return a NULL pointer upon failure causing a segmentation fault. Fix this by adding a null check right after malloc() and then do memset().

The SoB should come after the commit message here.

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

Quoted 13 lines
---
  samples/bpf/xdp_rxq_info_user.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
samples/bpf/xdp_rxq_info_user.c +0 -1
index 4fe47502ebed..2d03c84a4cca 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);

All these below are candidates for calloc(), can we just use that instead and simplify the below.

Quoted 25 lines
samples/bpf/xdp_rxq_info_user.c +2 -1
  	if (!array) {
  		fprintf(stderr, "Mem alloc error (nr_cpus:%u)\n", nr_cpus);
  		exit(EXIT_FAIL_MEM);
  	}
+	memset(array, 0, size);
  	return array;
  }
 
@@ -218,11 +218,11 @@ static struct record *alloc_record_per_rxq(void)
 
  	size = sizeof(struct record) * nr_rxqs;
  	array = malloc(size);
-	memset(array, 0, size);
  	if (!array) {
  		fprintf(stderr, "Mem alloc error (nr_rxqs:%u)\n", nr_rxqs);
  		exit(EXIT_FAIL_MEM);
  	}
+	memset(array, 0, size);
  	return array;
  }
 
@@ -233,11 +233,11 @@ static struct stats_record *alloc_stats_record(void)
  	int i;
 

samples/bpf/xdp_rxq_info_user.c static struct stats_record *alloc_stats_record(void) · line 243

Quoted 10 lines
samples/bpf/xdp_rxq_info_user.c +1 -1
  	rec = malloc(sizeof(*rec));
-	memset(rec, 0, sizeof(*rec));
  	if (!rec) {
  		fprintf(stderr, "Mem alloc error\n");
  		exit(EXIT_FAIL_MEM);
  	}
+	memset(rec, 0, sizeof(*rec));
  	rec->rxq = alloc_record_per_rxq();
  	for (i = 0; i < nr_rxqs; i++)
  		rec->rxq[i].cpu = alloc_record_per_cpu();

Thanks,
Daniel