[v2,2/4] cfgfile: support verify name and value

Message ID 20240705093115.4437-3-fengchengwen@huawei.com (mailing list archive)
State New
Delegated to: Thomas Monjalon
Headers
Series cfgfile: enhance error detecting |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

fengchengwen July 5, 2024, 9:31 a.m. UTC
  This patch supports verify section's name, entry's name and entry's
value validity.

Cc: stable@dpdk.org

Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
Acked-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/cfgfile/rte_cfgfile.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
  

Patch

diff --git a/lib/cfgfile/rte_cfgfile.c b/lib/cfgfile/rte_cfgfile.c
index 6a98e8fb11..97a399635e 100644
--- a/lib/cfgfile/rte_cfgfile.c
+++ b/lib/cfgfile/rte_cfgfile.c
@@ -105,6 +105,16 @@  static int
 _add_entry(struct rte_cfgfile_section *section, const char *entryname,
 		const char *entryvalue)
 {
+	int name_len, value_len;
+
+	name_len = strlen(entryname);
+	value_len = strlen(entryvalue);
+	if (name_len == 0 || name_len >= CFG_NAME_LEN || value_len >= CFG_VALUE_LEN) {
+		CFG_LOG(ERR, "invalid entry name %s or value %s in section %s",
+			entryname, entryvalue, section->name);
+		return -EINVAL;
+	}
+
 	/* resize entry structure if we don't have room for more entries */
 	if (section->num_entries == section->allocated_entries) {
 		struct rte_cfgfile_entry *n_entries = realloc(
@@ -322,6 +332,7 @@  rte_cfgfile_create(int flags)
 int
 rte_cfgfile_add_section(struct rte_cfgfile *cfg, const char *sectionname)
 {
+	int len;
 	int i;
 
 	if (cfg == NULL)
@@ -330,6 +341,12 @@  rte_cfgfile_add_section(struct rte_cfgfile *cfg, const char *sectionname)
 	if (sectionname == NULL)
 		return -EINVAL;
 
+	len = strlen(sectionname);
+	if (len == 0 || len >= CFG_NAME_LEN) {
+		CFG_LOG(ERR, "invalid section name %s", sectionname);
+		return -EINVAL;
+	}
+
 	/* resize overall struct if we don't have room for more	sections */
 	if (cfg->num_sections == cfg->allocated_sections) {