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