From owner-svn-src-head@FreeBSD.ORG Sun Dec 16 23:47:00 2012 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 1252F4F4; Sun, 16 Dec 2012 23:47:00 +0000 (UTC) (envelope-from hrs@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) by mx1.freebsd.org (Postfix) with ESMTP id EC94B8FC0A; Sun, 16 Dec 2012 23:46:59 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.5/8.14.5) with ESMTP id qBGNkxPr075047; Sun, 16 Dec 2012 23:46:59 GMT (envelope-from hrs@svn.freebsd.org) Received: (from hrs@localhost) by svn.freebsd.org (8.14.5/8.14.5/Submit) id qBGNkxZS075046; Sun, 16 Dec 2012 23:46:59 GMT (envelope-from hrs@svn.freebsd.org) Message-Id: <201212162346.qBGNkxZS075046@svn.freebsd.org> From: Hiroki Sato Date: Sun, 16 Dec 2012 23:46:59 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r244325 - head/sbin/route X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 16 Dec 2012 23:47:00 -0000 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);