Skip to contentSkip to conversation

batctl: bat-hosts: free bat_host when hash_add fails

PATCH New Sven Eckelmann 1 participant 1 message Jul 4

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 batctl: bat-hosts: free bat_host when hash_add fails

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")
---
 bat-hosts.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
bat-hosts.c +8 -1
index 6ad8247..160d317 100644
@@ -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,

Signature · 1 line
Sven Eckelmann <[email protected]>