diff --git a/sys/net/crosslayer/ng_netif/ng_netif.c b/sys/net/crosslayer/ng_netif/ng_netif.c index a42a26867d..2b7d7c9eaa 100644 --- a/sys/net/crosslayer/ng_netif/ng_netif.c +++ b/sys/net/crosslayer/ng_netif/ng_netif.c @@ -44,22 +44,28 @@ void ng_netif_init(void) int ng_netif_add(kernel_pid_t pid) { + kernel_pid_t *free_entry = NULL; + for (int i = 0; i < NG_NETIF_NUMOF; i++) { - if (ifs[i] == pid) { /* avoid duplicates */ + if (ifs[i] == pid) { return 0; } - else if (ifs[i] == KERNEL_PID_UNDEF) { - ifs[i] = pid; - - for (int j = 0; if_handler[j].add != NULL; j++) { - if_handler[j].add(pid); - } - - return 0; + else if (ifs[i] == KERNEL_PID_UNDEF && !free_entry) { + free_entry = &ifs[i]; } } - return -ENOMEM; + if (!free_entry) { + return -ENOMEM; + } + + *free_entry = pid; + + for (int j = 0; if_handler[j].add != NULL; j++) { + if_handler[j].add(pid); + } + + return 0; } void ng_netif_remove(kernel_pid_t pid)