From owner-svn-src-head@FreeBSD.ORG Tue Jan 4 14:13:10 2011 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 52807106566C; Tue, 4 Jan 2011 14:13:10 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 278838FC08; Tue, 4 Jan 2011 14:13:10 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id p04EDAHf038362; Tue, 4 Jan 2011 14:13:10 GMT (envelope-from kib@svn.freebsd.org) Received: (from kib@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id p04EDA4f038360; Tue, 4 Jan 2011 14:13:10 GMT (envelope-from kib@svn.freebsd.org) Message-Id: <201101041413.p04EDA4f038360@svn.freebsd.org> From: Konstantin Belousov Date: Tue, 4 Jan 2011 14:13:10 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r216955 - head/usr.sbin/rtprio X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.5 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: Tue, 04 Jan 2011 14:13:10 -0000 Author: kib Date: Tue Jan 4 14:13:09 2011 New Revision: 216955 URL: http://svn.freebsd.org/changeset/base/216955 Log: Make the parsing of the integer arguments for rtprio(1)/idprio(1) stricter. Style. Based on submission by: Eitan Adler , keramida Reviewed by: jhb, keramida MFC after: 1 week Modified: head/usr.sbin/rtprio/rtprio.c Modified: head/usr.sbin/rtprio/rtprio.c ============================================================================== --- head/usr.sbin/rtprio/rtprio.c Tue Jan 4 13:16:28 2011 (r216954) +++ head/usr.sbin/rtprio/rtprio.c Tue Jan 4 14:13:09 2011 (r216955) @@ -37,31 +37,31 @@ __FBSDID("$FreeBSD$"); #include #include -#include #include #include +#include #include #include #include #include -static void usage(); +static int parseint(const char *, const char *); +static void usage(void); int -main(argc, argv) - int argc; - char **argv; +main(int argc, char *argv[]) { - char *p; - int proc = 0; struct rtprio rtp; + char *p; + pid_t proc; /* find basename */ if ((p = rindex(argv[0], '/')) == NULL) p = argv[0]; else ++p; + proc = 0; if (!strcmp(p, "rtprio")) rtp.type = RTP_PRIO_REALTIME; @@ -70,12 +70,12 @@ main(argc, argv) switch (argc) { case 2: - proc = abs(atoi(argv[1])); /* Should check if numeric - * arg! */ + proc = parseint(argv[1], "pid"); + proc = abs(proc); /* FALLTHROUGH */ case 1: if (rtprio(RTP_LOOKUP, proc, &rtp) != 0) - err(1, "%s", argv[0]); + err(1, "RTP_LOOKUP"); printf("%s: ", p); switch (rtp.type) { case RTP_PRIO_REALTIME: @@ -103,19 +103,17 @@ main(argc, argv) usage(); break; } - } else { - rtp.prio = atoi(argv[1]); - } + } else + rtp.prio = parseint(argv[1], "priority"); } else { usage(); break; } if (argv[2][0] == '-') - proc = -atoi(argv[2]); - + proc = parseint(argv[2] + 1, "pid"); if (rtprio(RTP_SET, proc, &rtp) != 0) - err(1, "%s", argv[0]); + err(1, "RTP_SET"); if (proc == 0) { execvp(argv[2], &argv[2]); @@ -123,12 +121,28 @@ main(argc, argv) } exit(0); } - exit (1); + exit(1); +} + +static int +parseint(const char *str, const char *errname) +{ + char *endp; + long res; + + errno = 0; + res = strtol(str, &endp, 10); + if (errno != 0 || endp == str || *endp != '\0') + err(1, "%s must be a number", errname); + if (res >= INT_MAX) + err(1, "Integer overflow parsing %s", errname); + return (res); } static void -usage() +usage(void) { + (void) fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n", "usage: [id|rt]prio", " [id|rt]prio [-]pid",