Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 16 Dec 2012 23:46:59 +0000 (UTC)
From:      Hiroki Sato <hrs@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r244325 - head/sbin/route
Message-ID:  <201212162346.qBGNkxZS075046@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: hrs
Date: Sun Dec 16 23:46:59 2012
New Revision: 244325
URL: http://svnweb.freebsd.org/changeset/base/244325

Log:
  - Fix strtol() error handling.
  - Add a range condition of given FIB number and the related error messages.
  - Fix free() problem.
  
  Spotted by:	Artyom Mirgorodskiy
  Discussed with:	glebius

Modified:
  head/sbin/route/route.c

Modified: head/sbin/route/route.c
==============================================================================
--- head/sbin/route/route.c	Sun Dec 16 23:41:34 2012	(r244324)
+++ head/sbin/route/route.c	Sun Dec 16 23:46:59 2012	(r244325)
@@ -260,19 +260,25 @@ static int
 fiboptlist_range(const char *arg, struct fibl_head_t *flh)
 {
 	struct fibl *fl;
-	char *str, *token, *endptr;
+	char *str0, *str, *token, *endptr;
 	int fib[2], i, error;
 
-	str = strdup(arg);
+	str0 = str = strdup(arg);
 	error = 0;
 	i = 0;
 	while ((token = strsep(&str, "-")) != NULL) {
 		switch (i) {
 		case 0:
 		case 1:
+			errno = 0;
 			fib[i] = strtol(token, &endptr, 0);
-			if (*endptr != '\0' || (fib[i] == 0 &&
-			    (errno == EINVAL || errno == ERANGE)))
+			if (errno == 0) {
+				if (*endptr != '\0' ||
+				    fib[i] < 0 ||
+				    (numfibs != -1 && fib[i] > numfibs - 1)) 
+					errno = EINVAL;
+			}
+			if (errno)
 				error = 1;
 			break;
 		default:
@@ -296,7 +302,7 @@ fiboptlist_range(const char *arg, struct
 		TAILQ_INSERT_TAIL(flh, fl, fl_next);
 	}
 fiboptlist_range_ret:
-	free(str);
+	free(str0);
 	return (error);
 }
 
@@ -305,7 +311,7 @@ static int
 fiboptlist_csv(const char *arg, struct fibl_head_t *flh)
 {
 	struct fibl *fl;
-	char *str, *token, *endptr;
+	char *str0, *str, *token, *endptr;
 	int fib, error;
 
 	if (strcmp("all", arg) == 0) {
@@ -319,14 +325,14 @@ fiboptlist_csv(const char *arg, struct f
 		else
 			snprintf(str, ALLSTRLEN - 1, "%d", 0);
 	} else if (strcmp("default", arg) == 0) {
-		str = calloc(1, ALLSTRLEN);
+		str0 = str = calloc(1, ALLSTRLEN);
 		if (str == NULL) {
 			error = 1;
 			goto fiboptlist_csv_ret;
 		}
 		snprintf(str, ALLSTRLEN - 1, "%d", defaultfib);
 	} else
-		str = strdup(arg);
+		str0 = str = strdup(arg);
 
 	error = 0;
 	while ((token = strsep(&str, ",")) != NULL) {
@@ -335,9 +341,15 @@ fiboptlist_csv(const char *arg, struct f
 			if (error)
 				goto fiboptlist_csv_ret;
 		} else {
+			errno = 0;
 			fib = strtol(token, &endptr, 0);
-			if (*endptr != '\0' || (fib == 0 &&
-			    (errno == EINVAL || errno == ERANGE))) {
+			if (errno == 0) {
+				if (*endptr != '\0' ||
+				    fib < 0 ||
+				    (numfibs != -1 && fib > numfibs - 1))
+					errno = EINVAL;
+			}
+			if (errno) {
 				error = 1;
 				goto fiboptlist_csv_ret;
 			}
@@ -351,7 +363,7 @@ fiboptlist_csv(const char *arg, struct f
 		}
 	}
 fiboptlist_csv_ret:
-	free(str);
+	free(str0);
 	return (error);
 }
 
@@ -396,7 +408,7 @@ flushroutes(int argc, char *argv[])
 				usage(*argv);
 			error = fiboptlist_csv(*++argv, &fibl_head);
 			if (error)
-				usage(*argv);
+				errx(EX_USAGE, "invalid fib number: %s", *argv);
 			break;
 		default:
 			usage(*argv);
@@ -815,7 +827,8 @@ newroute(int argc, char **argv)
 					usage(NULL);
 				error = fiboptlist_csv(*++argv, &fibl_head);
 				if (error)
-					usage(NULL);
+					errx(EX_USAGE,
+					    "invalid fib number: %s", *argv);
 				break;
 			case K_IFA:
 				if (!--argc)
@@ -1383,10 +1396,16 @@ monitor(int argc, char *argv[])
 		case K_FIB:
 			if (!--argc)
 				usage(*argv);
+			errno = 0;
 			fib = strtol(*++argv, &endptr, 0);
-			if (*endptr != '\0' || (fib == 0 &&
-			    (errno == EINVAL || errno == ERANGE)))
-				usage(*argv);
+			if (errno == 0) {
+				if (*endptr != '\0' ||
+				    fib < 0 ||
+				    (numfibs != -1 && fib > numfibs - 1))
+					errno = EINVAL;
+			}
+			if (errno)
+				errx(EX_USAGE, "invalid fib number: %s", *argv);
 			break;
 		default:
 			usage(*argv);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201212162346.qBGNkxZS075046>