[PATCH] batctl: bat-hosts: free bat_host when hash_add fails

Sven Eckelmann <[email protected]> Sat, 04 Jul 2026 13:39:10 +0200
Newsgroups org.open-mesh.lists.batman
Message-ID <[email protected]>
If the hash_add fails (OOM or duplicated entry), then the allocated
bat_host would leak. The user would also not be informed about this
problem.

Check the return value and handle the error to make this problem visible to
the user.

Fixes: c7c76f63c1f9 ("[batctl] integrating batman hash implementation / move bat-hosts stuff into an extra set of files")
Signed-off-by: Sven Eckelmann <[email protected]>
---
 bat-hosts.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/bat-hosts.c b/bat-hosts.c
index 6ad8247..160d317 100644
--- a/bat-hosts.c
+++ b/bat-hosts.c
@@ -130,7 +130,14 @@ static void parse_hosts_file(struct hashtable_t **hash, const char path[], int r
 		strncpy(bat_host->name, name, HOST_NAME_MAX_LEN);
 		bat_host->name[HOST_NAME_MAX_LEN - 1] = '\0';
 
-		hash_add(*hash, bat_host);
+		if (hash_add(*hash, bat_host) < 0) {
+			if (read_opt & USE_BAT_HOSTS)
+				fprintf(stderr,
+					"Error - could not add bat host: %s\n",
+					name);
+			free(bat_host);
+			continue;
+		}
 
 		if ((*hash)->elements * 4 > (*hash)->size) {
 			swaphash = hash_resize((*hash), (*hash)->size * 2);

---
base-commit: e93995999e80513db80eba200ea682b5b15556af
change-id: 20260704-bugfixes-bat-hosts-5e6951bebcbf

Best regards,
--  
Sven Eckelmann <[email protected]>