src/linux/device.c: Fix segfault when running without `--net'.

If running without `--net', the (global) variable `netname' is NULL. This
creates a segmentation fault because this NULL-pointer is passed to strdup:

 Program terminated with signal 11, Segmentation fault.
 #0  0xb7d30463 in strlen () from /lib/tls/i686/cmov/libc.so.6
 (gdb) bt
 #0  0xb7d30463 in strlen () from /lib/tls/i686/cmov/libc.so.6
 #1  0xb7d30175 in strdup () from /lib/tls/i686/cmov/libc.so.6
 #2  0x0805bf47 in xstrdup (s=0x0) at xmalloc.c:118  <---
 #3  0x0805be33 in setup_device () at device.c:66
 #4  0x0805072e in setup_myself () at net_setup.c:432
 #5  0x08050db2 in setup_network () at net_setup.c:536
 #6  0x0805b27f in main (argc=Cannot access memory at address 0x0) at tincd.c:580

This patch fixes this by checking `netname' in `setup_device'. An alternative
would be to check for NULL-pointers in `xstrdup' and return NULL in this case.

Signed-off-by: Florian Forster <octo@verplant.org>
This commit is contained in:
Florian Forster 2009-05-27 09:27:44 +02:00 committed by Guus Sliepen
parent a8a65cee08
commit 6b415a1a7f

View file

@ -63,7 +63,8 @@ bool setup_device(void)
if(!get_config_string(lookup_config(config_tree, "Interface"), &iface)) if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
#ifdef HAVE_LINUX_IF_TUN_H #ifdef HAVE_LINUX_IF_TUN_H
iface = xstrdup(netname); if (netname != NULL)
iface = xstrdup(netname);
#else #else
iface = xstrdup(rindex(device, '/') ? rindex(device, '/') + 1 : device); iface = xstrdup(rindex(device, '/') ? rindex(device, '/') + 1 : device);
#endif #endif