Netdev List
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: netdev@vger.kernel.org
Cc: dsahern@kernel.org, Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH iproute2-next 2/2] lib: use flex array for names
Date: Fri, 27 Nov 2026 08:50:46 -0800	[thread overview]
Message-ID: <20261127165046.3656340-2-stephen@networkplumber.org> (raw)
In-Reply-To: <20261127165046.3656340-1-stephen@networkplumber.org>

Can save an allocation here by using flexible array to store the names.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 include/names.h |  2 +-
 lib/names.c     | 11 +++--------
 2 files changed, 4 insertions(+), 9 deletions(-)

diff --git a/include/names.h b/include/names.h
index 7ea83681..dd491a20 100644
--- a/include/names.h
+++ b/include/names.h
@@ -11,7 +11,7 @@
 struct db_entry {
 	struct db_entry *next;
 	unsigned int id;
-	char *name;
+	char name[];
 };
 
 struct db_names {
diff --git a/lib/names.c b/lib/names.c
index 6295a981..299da9d2 100644
--- a/lib/names.c
+++ b/lib/names.c
@@ -53,17 +53,13 @@ int db_names_load(struct db_names *db, db_id_parser_t parse_id,
 		}
 		ret = -1;
 
-		entry = malloc(sizeof(*entry));
+		entry = malloc(sizeof(*entry) + strlen(namebuf) + 1);
 		if (!entry)
 			goto exit;
 
-		entry->name = strdup(namebuf);
-		if (!entry->name) {
-			free(entry);
-			goto exit;
-		}
-
 		entry->id   = id;
+		strcpy(entry->name, namebuf);
+
 		entry->next = db->hash[id & (db->size - 1)];
 		db->hash[id & (db->size - 1)] = entry;
 	}
@@ -87,7 +83,6 @@ void db_names_free(struct db_names *db)
 		while (entry) {
 			struct db_entry *next = entry->next;
 
-			free(entry->name);
 			free(entry);
 			entry = next;
 		}
-- 
2.53.0


      reply	other threads:[~2026-11-27 16:50 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 16:50 [PATCH iproute2-next 1/2] rt_names: parse name databases without sscanf Stephen Hemminger
2026-11-27 16:50 ` Stephen Hemminger [this message]

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=20261127165046.3656340-2-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=dsahern@kernel.org \
    --cc=netdev@vger.kernel.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