naccyde / rpms / iproute

Forked from rpms/iproute 10 months ago
Clone

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

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