iptables: fix regression with unintended free in need_protomatch
[project/firewall3.git] / utils.c
diff --git a/utils.c b/utils.c
index 5667bcfd76db5d91191a90e0e705b51d2978745f..faa51a1a589b277421f1e5c16ee35d43eea89d92 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -315,23 +315,19 @@ fw3_command_close(void)
        pipe_pid = -1;
 }
 
-bool
-fw3_has_table(bool ipv6, const char *table)
+static bool
+file_contains(const char *path, const char *str)
 {
        FILE *f;
-
        char line[12];
        bool seen = false;
 
-       const char *path = ipv6
-               ? "/proc/net/ip6_tables_names" : "/proc/net/ip_tables_names";
-
        if (!(f = fopen(path, "r")))
                return false;
 
        while (fgets(line, sizeof(line), f))
        {
-               if (!strncmp(line, table, strlen(table)))
+               if (!strncmp(line, str, strlen(str)))
                {
                        seen = true;
                        break;
@@ -346,29 +342,10 @@ fw3_has_table(bool ipv6, const char *table)
 bool
 fw3_has_target(const bool ipv6, const char *target)
 {
-       FILE *f;
-
-       char line[12];
-       bool seen = false;
-
        const char *path = ipv6
                ? "/proc/net/ip6_tables_targets" : "/proc/net/ip_tables_targets";
 
-       if (!(f = fopen(path, "r")))
-               return false;
-
-       while (fgets(line, sizeof(line), f))
-       {
-               if (!strcmp(line, target))
-               {
-                       seen = true;
-                       break;
-               }
-       }
-
-       fclose(f);
-
-       return seen;
+       return file_contains(path, target);
 }
 
 bool
@@ -411,7 +388,6 @@ fw3_unlock_path(int *fd, const char *lockpath)
                warn("Cannot release exclusive lock: %s", strerror(errno));
 
        close(*fd);
-       unlink(FW3_LOCKFILE);
 
        *fd = -1;
 }