examples/ntb: fix the heap allocation check
PATCH 1/1 examples/ntb: fix the heap allocation check
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.
--- examples/ntb/ntb_fwd.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
examples/ntb/ntb_fwd.c +11 -0
@@ -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);
Signature · 1 line
2.34.1
Thomas Monjalon
Applied21/05/2025 19:23, Bing Zhao:
Quoted 5 lines
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.
Quoted 8 lines
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")Signed-off-by Bing Zhao <[email protected]>
Applied, thanks.