DPDK-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Bruce Richardson <bruce.richardson@intel.com>
To: dev@dpdk.org
Cc: Bruce Richardson <bruce.richardson@intel.com>,
	Cristian Dumitrescu <cristian.dumitrescu@intel.com>
Subject: [PATCH 1/6] cfgfile: add null checks to public APIs
Date: Thu,  7 May 2026 15:59:44 +0100	[thread overview]
Message-ID: <20260507145950.197753-2-bruce.richardson@intel.com> (raw)
In-Reply-To: <20260507145950.197753-1-bruce.richardson@intel.com>

For safety, add NULL checks to each of the public APIs to avoid crashes
in the library. Even though NULL values are likely symptoms of a wider
problem, this is not a datapath library so there is no harm in taking a
few cycles for additional parameter checking.

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
 lib/cfgfile/rte_cfgfile.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/lib/cfgfile/rte_cfgfile.c b/lib/cfgfile/rte_cfgfile.c
index c495bdf6ae..25fc792274 100644
--- a/lib/cfgfile/rte_cfgfile.c
+++ b/lib/cfgfile/rte_cfgfile.c
@@ -198,6 +198,10 @@ rte_cfgfile_load_with_params(const char *filename, int flags,
 		return NULL;
 
 	cfg = rte_cfgfile_create(flags);
+	if (cfg == NULL) {
+		fclose(f);
+		return NULL;
+	}
 
 	while (fgets(buffer, sizeof(buffer), f) != NULL) {
 		char *pos;
@@ -506,6 +510,9 @@ rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sectionname,
 	int num_sections = 0;
 	int i;
 
+	if (cfg == NULL)
+		return -1;
+
 	if (sectionname == NULL)
 		return cfg->num_sections;
 
@@ -523,8 +530,14 @@ rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
 {
 	int i;
 
-	for (i = 0; i < cfg->num_sections && i < max_sections; i++)
+	if (cfg == NULL || sections == NULL || max_sections < 0)
+		return -1;
+
+	for (i = 0; i < cfg->num_sections && i < max_sections; i++) {
+		if (sections[i] == NULL)
+			return -1;
 		strlcpy(sections[i], cfg->sections[i].name, CFG_NAME_LEN);
+	}
 
 	return i;
 }
@@ -533,6 +546,9 @@ RTE_EXPORT_SYMBOL(rte_cfgfile_has_section)
 int
 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
 {
+	if (cfg == NULL || sectionname == NULL)
+		return 0;
+
 	return _get_section(cfg, sectionname) != NULL;
 }
 
@@ -541,6 +557,9 @@ int
 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
 	const char *sectionname)
 {
+	if (cfg == NULL || sectionname == NULL)
+		return -1;
+
 	const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
 	if (s == NULL)
 		return -1;
@@ -552,6 +571,9 @@ int
 rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg,
 	char *sectionname, int index)
 {
+	if (cfg == NULL || sectionname == NULL)
+		return -1;
+
 	if (index < 0 || index >= cfg->num_sections)
 		return -1;
 
@@ -566,6 +588,10 @@ rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
 		struct rte_cfgfile_entry *entries, int max_entries)
 {
 	int i;
+
+	if (cfg == NULL || sectionname == NULL || entries == NULL)
+		return -1;
+
 	const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
 	if (sect == NULL)
 		return -1;
@@ -583,6 +609,9 @@ rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
 	int i;
 	const struct rte_cfgfile_section *sect;
 
+	if (cfg == NULL || sectionname == NULL || entries == NULL)
+		return -1;
+
 	if (index < 0 || index >= cfg->num_sections)
 		return -1;
 	sect = &cfg->sections[index];
@@ -598,6 +627,10 @@ rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
 		const char *entryname)
 {
 	int i;
+
+	if (cfg == NULL || sectionname == NULL || entryname == NULL)
+		return NULL;
+
 	const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
 	if (sect == NULL)
 		return NULL;
-- 
2.51.0


  reply	other threads:[~2026-05-07 15:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-07 14:59 [PATCH 0/6] add hardening checks to cmdline and cfgfile libs Bruce Richardson
2026-05-07 14:59 ` Bruce Richardson [this message]
2026-05-07 14:59 ` [PATCH 2/6] cfgfile: prevent issues with overflow on resize Bruce Richardson
2026-05-07 14:59 ` [PATCH 3/6] cmdline: harden parser result buffer handling Bruce Richardson
2026-05-07 14:59 ` [PATCH 4/6] cmdline: add explicit help function for bool type Bruce Richardson
2026-05-07 14:59 ` [PATCH 5/6] cmdline: guard zero-size destination buffers Bruce Richardson
2026-05-07 14:59 ` [PATCH 6/6] cmdline: add null checks for invalid input Bruce Richardson

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=20260507145950.197753-2-bruce.richardson@intel.com \
    --to=bruce.richardson@intel.com \
    --cc=cristian.dumitrescu@intel.com \
    --cc=dev@dpdk.org \
    /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