bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] xdp_rxq_info_user: Add null check after malloc
@ 2020-06-10  3:01 Gaurav Singh
  2020-06-11 14:12 ` Daniel Borkmann
  0 siblings, 1 reply; 2+ messages in thread
From: Gaurav Singh @ 2020-06-10  3:01 UTC (permalink / raw)
  To: gaurav1086, Alexei Starovoitov, Daniel Borkmann, David S. Miller,
	Jakub Kicinski, Jesper Dangaard Brouer, John Fastabend,
	Martin KaFai Lau, Song Liu, Yonghong Song, Andrii Nakryiko,
	KP Singh, open list:XDP (eXpress Data Path),
	open list:XDP (eXpress Data Path),
	open list

Signed-off-by: Gaurav Singh <gaurav1086@gmail.com>

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(-)

diff --git a/samples/bpf/xdp_rxq_info_user.c b/samples/bpf/xdp_rxq_info_user.c
index 4fe47502ebed..2d03c84a4cca 100644
--- a/samples/bpf/xdp_rxq_info_user.c
+++ b/samples/bpf/xdp_rxq_info_user.c
@@ -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();
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-06-11 14:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-10  3:01 [PATCH] xdp_rxq_info_user: Add null check after malloc Gaurav Singh
2020-06-11 14:12 ` Daniel Borkmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).