naccyde / rpms / iproute

Forked from rpms/iproute 10 months ago
Clone

Blame SOURCES/0002-nstat-print-useful-error-messages-in-abort-cases.patch

d8fb9e
From d82789f3e39983a41752d322c52f0a9c36d9419a Mon Sep 17 00:00:00 2001
d8fb9e
From: Andrea Claudi <aclaudi@redhat.com>
d8fb9e
Date: Tue, 21 Apr 2020 12:44:38 +0200
d8fb9e
Subject: [PATCH] nstat: print useful error messages in abort() cases
d8fb9e
d8fb9e
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1792908
d8fb9e
Upstream Status: iproute2.git commit 2c7056ac26412
d8fb9e
Conflicts: context change due to missing commit 72cdb77d1a31a
d8fb9e
           ("nstat: fix load_ugly_table() limits")
d8fb9e
d8fb9e
commit 2c7056ac26412fe99443a283f0c1261cb81ccea2
d8fb9e
Author: Andrea Claudi <aclaudi@redhat.com>
d8fb9e
Date:   Mon Feb 17 14:46:18 2020 +0100
d8fb9e
d8fb9e
    nstat: print useful error messages in abort() cases
d8fb9e
d8fb9e
    When nstat temporary file is corrupted or in some other corner cases,
d8fb9e
    nstat use abort() to stop its execution. This can puzzle some users,
d8fb9e
    wondering what is the reason for the crash.
d8fb9e
d8fb9e
    This commit replaces abort() with some meaningful error messages and exit()
d8fb9e
d8fb9e
    Reported-by: Renaud Métrich <rmetrich@redhat.com>
d8fb9e
    Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
d8fb9e
    Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
d8fb9e
---
d8fb9e
 misc/nstat.c | 47 +++++++++++++++++++++++++++++++++--------------
d8fb9e
 1 file changed, 33 insertions(+), 14 deletions(-)
d8fb9e
d8fb9e
diff --git a/misc/nstat.c b/misc/nstat.c
d8fb9e
index a4dd405d43a93..33c428c8b2418 100644
d8fb9e
--- a/misc/nstat.c
d8fb9e
+++ b/misc/nstat.c
d8fb9e
@@ -143,14 +143,19 @@ static void load_good_table(FILE *fp)
d8fb9e
 		}
d8fb9e
 		/* idbuf is as big as buf, so this is safe */
d8fb9e
 		nr = sscanf(buf, "%s%llu%lg", idbuf, &val, &rate;;
d8fb9e
-		if (nr < 2)
d8fb9e
-			abort();
d8fb9e
+		if (nr < 2) {
d8fb9e
+			fprintf(stderr, "%s:%d: error parsing history file\n",
d8fb9e
+				__FILE__, __LINE__);
d8fb9e
+			exit(-2);
d8fb9e
+		}
d8fb9e
 		if (nr < 3)
d8fb9e
 			rate = 0;
d8fb9e
 		if (useless_number(idbuf))
d8fb9e
 			continue;
d8fb9e
-		if ((n = malloc(sizeof(*n))) == NULL)
d8fb9e
-			abort();
d8fb9e
+		if ((n = malloc(sizeof(*n))) == NULL) {
d8fb9e
+			perror("nstat: malloc");
d8fb9e
+			exit(-1);
d8fb9e
+		}
d8fb9e
 		n->id = strdup(idbuf);
d8fb9e
 		n->val = val;
d8fb9e
 		n->rate = rate;
d8fb9e
@@ -189,8 +194,11 @@ static void load_ugly_table(FILE *fp)
d8fb9e
 		int count1, count2, skip = 0;
d8fb9e
 
d8fb9e
 		p = strchr(buf, ':');
d8fb9e
-		if (!p)
d8fb9e
-			abort();
d8fb9e
+		if (!p) {
d8fb9e
+			fprintf(stderr, "%s:%d: error parsing history file\n",
d8fb9e
+				__FILE__, __LINE__);
d8fb9e
+			exit(-2);
d8fb9e
+		}
d8fb9e
 		count1 = count_spaces(buf);
d8fb9e
 		*p = 0;
d8fb9e
 		idbuf[0] = 0;
d8fb9e
@@ -210,8 +218,10 @@ static void load_ugly_table(FILE *fp)
d8fb9e
 				strncat(idbuf, p, sizeof(idbuf) - off - 1);
d8fb9e
 			}
d8fb9e
 			n = malloc(sizeof(*n));
d8fb9e
-			if (!n)
d8fb9e
-				abort();
d8fb9e
+			if (!n) {
d8fb9e
+				perror("nstat: malloc");
d8fb9e
+				exit(-1);
d8fb9e
+			}
d8fb9e
 			n->id = strdup(idbuf);
d8fb9e
 			n->rate = 0;
d8fb9e
 			n->next = db;
d8fb9e
@@ -219,18 +229,27 @@ static void load_ugly_table(FILE *fp)
d8fb9e
 			p = next;
d8fb9e
 		}
d8fb9e
 		n = db;
d8fb9e
-		if (fgets(buf, sizeof(buf), fp) == NULL)
d8fb9e
-			abort();
d8fb9e
+		if (fgets(buf, sizeof(buf), fp) == NULL) {
d8fb9e
+			fprintf(stderr, "%s:%d: error parsing history file\n",
d8fb9e
+				__FILE__, __LINE__);
d8fb9e
+			exit(-2);
d8fb9e
+		}
d8fb9e
 		count2 = count_spaces(buf);
d8fb9e
 		if (count2 > count1)
d8fb9e
 			skip = count2 - count1;
d8fb9e
 		do {
d8fb9e
 			p = strrchr(buf, ' ');
d8fb9e
-			if (!p)
d8fb9e
-				abort();
d8fb9e
+			if (!p) {
d8fb9e
+				fprintf(stderr, "%s:%d: error parsing history file\n",
d8fb9e
+					__FILE__, __LINE__);
d8fb9e
+				exit(-2);
d8fb9e
+			}
d8fb9e
 			*p = 0;
d8fb9e
-			if (sscanf(p+1, "%llu", &n->val) != 1)
d8fb9e
-				abort();
d8fb9e
+			if (sscanf(p+1, "%llu", &n->val) != 1) {
d8fb9e
+				fprintf(stderr, "%s:%d: error parsing history file\n",
d8fb9e
+					__FILE__, __LINE__);
d8fb9e
+				exit(-2);
d8fb9e
+			}
d8fb9e
 			/* Trick to skip "dummy" trailing ICMP MIB in 2.4 */
d8fb9e
 			if (skip)
d8fb9e
 				skip--;
d8fb9e
-- 
d8fb9e
2.25.3
d8fb9e