diff mbox series

[iproute2-next] nstat: print useful error messages in abort() cases

Message ID 0dd54edffe6edd9f0a15dbe9590c251782b743a4.1581012315.git.aclaudi@redhat.com
State Changes Requested
Delegated to: David Ahern
Headers show
Series [iproute2-next] nstat: print useful error messages in abort() cases | expand

Commit Message

Andrea Claudi Feb. 6, 2020, 6:08 p.m. UTC
When nstat temporary file is corrupted or in some other corner cases,
nstat uses abort() to stop its execution. This can puzzle some users,
wondering what is the reason of 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>
---
 misc/nstat.c | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)

Comments

David Ahern Feb. 10, 2020, 5:27 a.m. UTC | #1
On 2/6/20 11:08 AM, Andrea Claudi wrote:
> @@ -221,8 +231,11 @@ static void load_ugly_table(FILE *fp)
>  		}
>  		n = db;
>  		nread = getline(&buf, &buflen, fp);
> -		if (nread == -1)
> -			abort();
> +		if (nread == -1) {
> +			fprintf(stderr, "%s:%d: error parsing history file\n",
> +				__FILE__, __LINE__);
> +			exit(-2);
> +		}
>  		count2 = count_spaces(buf);
>  		if (count2 > count1)
>  			skip = count2 - count1;
> 

I see 2 more aborts after this one; seems like you should cover those as
well.

Also, given that it is a straightforward replace of abort with error
message + exit, this should probably go to master.
diff mbox series

Patch

diff --git a/misc/nstat.c b/misc/nstat.c
index 23113b223b22d..d8e3e274442f6 100644
--- a/misc/nstat.c
+++ b/misc/nstat.c
@@ -142,14 +142,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;
@@ -190,8 +195,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;
@@ -211,8 +219,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;
@@ -221,8 +231,11 @@  static void load_ugly_table(FILE *fp)
 		}
 		n = db;
 		nread = getline(&buf, &buflen, fp);
-		if (nread == -1)
-			abort();
+		if (nread == -1) {
+			fprintf(stderr, "%s:%d: error parsing history file\n",
+				__FILE__, __LINE__);
+			exit(-2);
+		}
 		count2 = count_spaces(buf);
 		if (count2 > count1)
 			skip = count2 - count1;