examples/ntb: fix the heap allocation check

Message ID 20250521172333.12859-1-bingz@nvidia.com (mailing list archive)
State Accepted
Delegated to: Thomas Monjalon
Headers
Series examples/ntb: fix the heap allocation check |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/iol-marvell-Functional success Functional Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/intel-Functional success Functional PASS
ci/github-robot: build success github build: passed
ci/iol-abi-testing success Testing PASS
ci/iol-unit-amd64-testing success Testing PASS
ci/iol-compile-amd64-testing warning Testing issues
ci/iol-unit-arm64-testing success Testing PASS
ci/iol-compile-arm64-testing success Testing PASS
ci/iol-sample-apps-testing success Testing PASS
ci/aws-unit-testing success Unit Testing PASS

Commit Message

Bing Zhao May 21, 2025, 5:23 p.m. UTC
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(+)
  

Comments

Thomas Monjalon July 9, 2025, 2:50 p.m. UTC | #1
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.
  

Patch

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