naccyde / rpms / iproute

Forked from rpms/iproute 10 months ago
Clone

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

0ac2f3
From 5723280683d940b33647b8dc92a3aa3b1a9a5466 Mon Sep 17 00:00:00 2001
0ac2f3
From: Andrea Claudi <aclaudi@redhat.com>
0ac2f3
Date: Thu, 30 Apr 2020 12:20:17 +0200
0ac2f3
Subject: [PATCH] nstat: print useful error messages in abort() cases
0ac2f3
MIME-Version: 1.0
0ac2f3
Content-Type: text/plain; charset=UTF-8
0ac2f3
Content-Transfer-Encoding: 8bit
0ac2f3
0ac2f3
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1824896
0ac2f3
Upstream Status: iproute2.git commit 2c7056ac26412
0ac2f3
0ac2f3
commit 2c7056ac26412fe99443a283f0c1261cb81ccea2
0ac2f3
Author: Andrea Claudi <aclaudi@redhat.com>
0ac2f3
Date:   Mon Feb 17 14:46:18 2020 +0100
0ac2f3
0ac2f3
    nstat: print useful error messages in abort() cases
0ac2f3
0ac2f3
    When nstat temporary file is corrupted or in some other corner cases,
0ac2f3
    nstat use abort() to stop its execution. This can puzzle some users,
0ac2f3
    wondering what is the reason for the crash.
0ac2f3
0ac2f3
    This commit replaces abort() with some meaningful error messages and exit()
0ac2f3
0ac2f3
    Reported-by: Renaud Métrich <rmetrich@redhat.com>
0ac2f3
    Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
0ac2f3
    Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
0ac2f3
---
0ac2f3
 misc/nstat.c | 47 +++++++++++++++++++++++++++++++++--------------
0ac2f3
 1 file changed, 33 insertions(+), 14 deletions(-)
0ac2f3
0ac2f3
diff --git a/misc/nstat.c b/misc/nstat.c
0ac2f3
index 23113b223b22d..425e75ef461ec 100644
0ac2f3
--- a/misc/nstat.c
0ac2f3
+++ b/misc/nstat.c
0ac2f3
@@ -142,14 +142,19 @@ static void load_good_table(FILE *fp)
0ac2f3
 		}
0ac2f3
 		/* idbuf is as big as buf, so this is safe */
0ac2f3
 		nr = sscanf(buf, "%s%llu%lg", idbuf, &val, &rate;;
0ac2f3
-		if (nr < 2)
0ac2f3
-			abort();
0ac2f3
+		if (nr < 2) {
0ac2f3
+			fprintf(stderr, "%s:%d: error parsing history file\n",
0ac2f3
+				__FILE__, __LINE__);
0ac2f3
+			exit(-2);
0ac2f3
+		}
0ac2f3
 		if (nr < 3)
0ac2f3
 			rate = 0;
0ac2f3
 		if (useless_number(idbuf))
0ac2f3
 			continue;
0ac2f3
-		if ((n = malloc(sizeof(*n))) == NULL)
0ac2f3
-			abort();
0ac2f3
+		if ((n = malloc(sizeof(*n))) == NULL) {
0ac2f3
+			perror("nstat: malloc");
0ac2f3
+			exit(-1);
0ac2f3
+		}
0ac2f3
 		n->id = strdup(idbuf);
0ac2f3
 		n->val = val;
0ac2f3
 		n->rate = rate;
0ac2f3
@@ -190,8 +195,11 @@ static void load_ugly_table(FILE *fp)
0ac2f3
 		int count1, count2, skip = 0;
0ac2f3
 
0ac2f3
 		p = strchr(buf, ':');
0ac2f3
-		if (!p)
0ac2f3
-			abort();
0ac2f3
+		if (!p) {
0ac2f3
+			fprintf(stderr, "%s:%d: error parsing history file\n",
0ac2f3
+				__FILE__, __LINE__);
0ac2f3
+			exit(-2);
0ac2f3
+		}
0ac2f3
 		count1 = count_spaces(buf);
0ac2f3
 		*p = 0;
0ac2f3
 		idbuf[0] = 0;
0ac2f3
@@ -211,8 +219,10 @@ static void load_ugly_table(FILE *fp)
0ac2f3
 				strncat(idbuf, p, sizeof(idbuf) - off - 1);
0ac2f3
 			}
0ac2f3
 			n = malloc(sizeof(*n));
0ac2f3
-			if (!n)
0ac2f3
-				abort();
0ac2f3
+			if (!n) {
0ac2f3
+				perror("nstat: malloc");
0ac2f3
+				exit(-1);
0ac2f3
+			}
0ac2f3
 			n->id = strdup(idbuf);
0ac2f3
 			n->rate = 0;
0ac2f3
 			n->next = db;
0ac2f3
@@ -221,18 +231,27 @@ static void load_ugly_table(FILE *fp)
0ac2f3
 		}
0ac2f3
 		n = db;
0ac2f3
 		nread = getline(&buf, &buflen, fp);
0ac2f3
-		if (nread == -1)
0ac2f3
-			abort();
0ac2f3
+		if (nread == -1) {
0ac2f3
+			fprintf(stderr, "%s:%d: error parsing history file\n",
0ac2f3
+				__FILE__, __LINE__);
0ac2f3
+			exit(-2);
0ac2f3
+		}
0ac2f3
 		count2 = count_spaces(buf);
0ac2f3
 		if (count2 > count1)
0ac2f3
 			skip = count2 - count1;
0ac2f3
 		do {
0ac2f3
 			p = strrchr(buf, ' ');
0ac2f3
-			if (!p)
0ac2f3
-				abort();
0ac2f3
+			if (!p) {
0ac2f3
+				fprintf(stderr, "%s:%d: error parsing history file\n",
0ac2f3
+					__FILE__, __LINE__);
0ac2f3
+				exit(-2);
0ac2f3
+			}
0ac2f3
 			*p = 0;
0ac2f3
-			if (sscanf(p+1, "%llu", &n->val) != 1)
0ac2f3
-				abort();
0ac2f3
+			if (sscanf(p+1, "%llu", &n->val) != 1) {
0ac2f3
+				fprintf(stderr, "%s:%d: error parsing history file\n",
0ac2f3
+					__FILE__, __LINE__);
0ac2f3
+				exit(-2);
0ac2f3
+			}
0ac2f3
 			/* Trick to skip "dummy" trailing ICMP MIB in 2.4 */
0ac2f3
 			if (skip)
0ac2f3
 				skip--;
0ac2f3
-- 
0ac2f3
2.25.4
0ac2f3