From: Bing Zhao <bingz@nvidia.com>
To: <dev@dpdk.org>, <jingjing.wu@intel.com>, <junfeng.guo@intel.com>,
<xiaoyun.li@intel.com>
Cc: <stable@dpdk.org>, <thomas@monjalon.net>, <ferruh.yigit@amd.com>
Subject: [PATCH] examples/ntb: fix the heap allocation check
Date: Wed, 21 May 2025 20:23:33 +0300 [thread overview]
Message-ID: <20250521172333.12859-1-bingz@nvidia.com> (raw)
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
reply other threads:[~2025-05-21 17:24 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250521172333.12859-1-bingz@nvidia.com \
--to=bingz@nvidia.com \
--cc=dev@dpdk.org \
--cc=ferruh.yigit@amd.com \
--cc=jingjing.wu@intel.com \
--cc=junfeng.guo@intel.com \
--cc=stable@dpdk.org \
--cc=thomas@monjalon.net \
--cc=xiaoyun.li@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).