[PATCH 3/6] alfred: don't drop the mesh interface name on OOM

Sven Eckelmann <[email protected]> Fri, 31 Jul 2026 13:44:52 +0200
Newsgroups org.open-mesh.lists.batman
Message-ID <[email protected]>
unix_sock_change_bat_iface() freed the old mesh_iface and then assigned
the result of strdup() directly to globals->mesh_iface. When strdup()
failed the old name was already gone and mesh_iface was left NULL. But it
still reported a success success.

Duplicate the name into a temporary first and only replace the stored
pointer once the allocation succeeded.

Fixes: b96cc742ef3e ("alfred: introduce 'change batman-adv interface' IPC call")
Signed-off-by: Sven Eckelmann <[email protected]>
---
 unix_sock.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/unix_sock.c b/unix_sock.c
index 64fde70..417b380 100644
--- a/unix_sock.c
+++ b/unix_sock.c
@@ -404,6 +404,7 @@ unix_sock_change_bat_iface(struct globals *globals,
 			   struct alfred_change_bat_iface_v0 *change_bat_iface,
 			   int client_sock)
 {
+	char *mesh_iface;
 	int ret = -1;
 	int len;
 
@@ -412,9 +413,13 @@ unix_sock_change_bat_iface(struct globals *globals,
 	if (len < (int)(sizeof(*change_bat_iface) - sizeof(change_bat_iface->header)))
 		goto err;
 
-	free(globals->mesh_iface);
 	change_bat_iface->bat_iface[sizeof(change_bat_iface->bat_iface) - 1] = '\0';
-	globals->mesh_iface = strdup(change_bat_iface->bat_iface);
+	mesh_iface = strdup(change_bat_iface->bat_iface);
+	if (!mesh_iface)
+		goto err;
+
+	free(globals->mesh_iface);
+	globals->mesh_iface = mesh_iface;
 
 	ret = 0;
 err:

-- 
2.47.3