naccyde / rpms / iproute

Forked from rpms/iproute 8 months ago
Clone

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

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