diff mbox

iptables-save: Exit with error if unable to open proc file

Message ID CA+kHd+db15eUCFb+_-T1zJVPSkogyLUTp2Fe-2u9z8B0E3a-_A@mail.gmail.com
State Accepted
Delegated to: Florian Westphal
Headers show

Commit Message

Thomas Habets Jan. 18, 2017, 1:58 p.m. UTC
On Wed, 18 Jan 2017 14:32:30 +0100, Florian Westphal <fw@strlen.de> said:
> static const char filename[] =

Done.

> iptables uses kernel coding style, so
>
> 	if (errno == ENOENT)
> 		return ret;

Gotcha. Making the code unambiguously worse, then. (cough, goto fail, cough)
Done.

> Looks like your mua mangled the patch and broke long lines.
> Can you send with git-send-email?

Using a better client this time.


commit 0d18c3e9488ac2e36a5c5ecccce93de795f6fe25
Author: Thomas Habets <habets@google.com>
Date:   Wed Jan 18 13:46:54 2017 +0000

    iptables-save: Exit with error if unable to open proc file

 		if (tablename[strlen(tablename) - 1] != '\n')
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Comments

Florian Westphal Jan. 18, 2017, 2:07 p.m. UTC | #1
thomas@habets.se <thomas@habets.se> wrote:
> diff --git a/iptables/ip6tables-save.c b/iptables/ip6tables-save.c
> index f35e921..053413a 100644
> --- a/iptables/ip6tables-save.c
> +++ b/iptables/ip6tables-save.c
> @@ -35,10 +35,16 @@ static int for_each_table(int (*func)(const char
> *tablename))

Still a linewrap here, rest was fine so I fixed this up and applied
the patch, thanks!

I did not notce on 1st review but a 'Signed-off-by' line would be
good for future submissions.
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/iptables/ip6tables-save.c b/iptables/ip6tables-save.c
index f35e921..053413a 100644
--- a/iptables/ip6tables-save.c
+++ b/iptables/ip6tables-save.c
@@ -35,10 +35,16 @@  static int for_each_table(int (*func)(const char
*tablename))
 	int ret = 1;
 	FILE *procfile = NULL;
 	char tablename[XT_TABLE_MAXNAMELEN+1];
-
-	procfile = fopen("/proc/net/ip6_tables_names", "re");
-	if (!procfile)
-		return ret;
+	static const char filename[] = "/proc/net/ip6_tables_names";
+
+	procfile = fopen(filename, "re");
+	if (!procfile) {
+		if (errno == ENOENT)
+			return ret;
+		fprintf(stderr, "Failed to list table names in %s: %s\n",
+		        filename, strerror(errno));
+		exit(1);
+	}

 	while (fgets(tablename, sizeof(tablename), procfile)) {
 		if (tablename[strlen(tablename) - 1] != '\n')
diff --git a/iptables/iptables-save.c b/iptables/iptables-save.c
index 238f368..e8ae9c6 100644
--- a/iptables/iptables-save.c
+++ b/iptables/iptables-save.c
@@ -33,10 +33,16 @@  static int for_each_table(int (*func)(const char
*tablename))
 	int ret = 1;
 	FILE *procfile = NULL;
 	char tablename[XT_TABLE_MAXNAMELEN+1];
-
-	procfile = fopen("/proc/net/ip_tables_names", "re");
-	if (!procfile)
-		return ret;
+	static const char filename[] = "/proc/net/ip_tables_names";
+
+	procfile = fopen(filename, "re");
+	if (!procfile) {
+		if (errno == ENOENT)
+			return ret;
+		fprintf(stderr, "Failed to list table names in %s: %s\n",
+		        filename, strerror(errno));
+		exit(1);
+	}

 	while (fgets(tablename, sizeof(tablename), procfile)) {