handler: fix resource leak on error in netifd_init_script_handlers()
authorHans Dedecker <dedeckeh@gmail.com>
Thu, 12 Apr 2018 20:46:35 +0000 (22:46 +0200)
committerHans Dedecker <dedeckeh@gmail.com>
Thu, 12 Apr 2018 20:46:35 +0000 (22:46 +0200)
Detected by Coverity in CID 1412486

Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
handler.c

index a0b2a57b146ca030c91edb4d60cc1bc1c4211d4a..8608a9751ea9f5705b182b762cb0a9b5b8090852 100644 (file)
--- a/handler.c
+++ b/handler.c
@@ -34,6 +34,9 @@ netifd_dir_push(int fd)
 static void
 netifd_dir_pop(int prev_fd)
 {
+       if (prev_fd < 0)
+               return;
+
        if (fchdir(prev_fd)) {}
        close(prev_fd);
 }
@@ -128,8 +131,10 @@ void netifd_init_script_handlers(int dir_fd, script_dump_cb cb)
        int i, prev_fd;
 
        prev_fd = netifd_dir_push(dir_fd);
-       if (glob("./*.sh", 0, NULL, &g))
+       if (glob("./*.sh", 0, NULL, &g)) {
+               netifd_dir_pop(prev_fd);
                return;
+       }
 
        for (i = 0; i < g.gl_pathc; i++)
                netifd_parse_script_handler(g.gl_pathv[i], cb);