patches for DPDK stable branches
 help / color / mirror / Atom feed
* [PATCH] examples/ntb: fix the heap allocation check
@ 2025-05-21 17:23 Bing Zhao
  2025-07-09 14:50 ` Thomas Monjalon
  0 siblings, 1 reply; 2+ messages in thread
From: Bing Zhao @ 2025-05-21 17:23 UTC (permalink / raw)
  To: dev, jingjing.wu, junfeng.guo, xiaoyun.li; +Cc: stable, thomas, ferruh.yigit

In some rare case, the libc memory heap allocation may fail and
return NULL pointer. Before accessing the memory via the pointer,
the NULL pointer check should be done to ensure the code locates
in the safe side and no crash.

Some newer GCC version will check this by default and report warning
on this. Adding the NULL pointer check will help to get rid of such
warning.

Fixes: 5194299d6ef5 ("examples/ntb: support more functions")
Cc: xiaoyun.li@intel.com

Signed-off-by: Bing Zhao <bingz@nvidia.com>
---
 examples/ntb/ntb_fwd.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/examples/ntb/ntb_fwd.c b/examples/ntb/ntb_fwd.c
index 37d60208e3..bd4f038516 100644
--- a/examples/ntb/ntb_fwd.c
+++ b/examples/ntb/ntb_fwd.c
@@ -843,9 +843,20 @@ ntb_stats_display(void)
 		return;
 	}
 	ids = malloc(sizeof(uint32_t) * nb_ids);
+	if (ids == NULL) {
+		printf("Cannot allocate memory for statistics IDs\n");
+		free(xstats_names);
+		return;
+	}
 	for (i = 0; i < nb_ids; i++)
 		ids[i] = i;
 	values = malloc(sizeof(uint64_t) * nb_ids);
+	if (values == NULL) {
+		printf("Cannot allocate memory to save fetching values\n");
+		free(xstats_names);
+		free(ids);
+		return;
+	}
 	if (nb_ids != rte_rawdev_xstats_get(dev_id, ids, values, nb_ids)) {
 		printf("Error: Unable to get xstats\n");
 		free(xstats_names);
-- 
2.34.1


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

* Re: [PATCH] examples/ntb: fix the heap allocation check
  2025-05-21 17:23 [PATCH] examples/ntb: fix the heap allocation check Bing Zhao
@ 2025-07-09 14:50 ` Thomas Monjalon
  0 siblings, 0 replies; 2+ messages in thread
From: Thomas Monjalon @ 2025-07-09 14:50 UTC (permalink / raw)
  To: Bing Zhao; +Cc: dev, jingjing.wu, junfeng.guo, xiaoyun.li, stable, ferruh.yigit

21/05/2025 19:23, Bing Zhao:
> In some rare case, the libc memory heap allocation may fail and
> return NULL pointer. Before accessing the memory via the pointer,
> the NULL pointer check should be done to ensure the code locates
> in the safe side and no crash.
> 
> Some newer GCC version will check this by default and report warning
> on this. Adding the NULL pointer check will help to get rid of such
> warning.
> 
> Fixes: 5194299d6ef5 ("examples/ntb: support more functions")
> Cc: xiaoyun.li@intel.com
> 
> Signed-off-by: Bing Zhao <bingz@nvidia.com>

Applied, thanks.



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

end of thread, other threads:[~2025-07-09 14:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-05-21 17:23 [PATCH] examples/ntb: fix the heap allocation check Bing Zhao
2025-07-09 14:50 ` Thomas Monjalon

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