Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 2 Jan 2011 12:18:45 +0200
From:      Kostik Belousov <kostikbel@gmail.com>
To:        Eitan Adler <lists@eitanadler.com>
Cc:        hackers@freebsd.org
Subject:   Re: [patch] have rtprio check that arguments are numeric; change atoi to strtol
Message-ID:  <20110102101845.GC90883@deviant.kiev.zoral.com.ua>
In-Reply-To: <AANLkTimdEpZ1DBoKU9g6w-j=F9KS0ONvo9racgOesdPf@mail.gmail.com>
References:  <AANLkTimiJPiHBSw5i5TVJYfh9uGOyrNJx%2BoUPeB%2Bt%2BY_@mail.gmail.com> <6BEFA669-2E1F-44E6-897A-0A51DA939A74@gmail.com> <AANLkTimmQHRoQOZBgfheds=_sv5SMzD9DrPfYk9em7j8@mail.gmail.com> <AANLkTimdEpZ1DBoKU9g6w-j=F9KS0ONvo9racgOesdPf@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help

[-- Attachment #1 --]
On Sun, Jan 02, 2011 at 02:41:10AM -0500, Eitan Adler wrote:
> > Just set the second argument to strtol to something non-NULL and then check
> > the value returned; that will help provide the error handling with
> > simplicity that you desire :).
> 
> How about this version? It also corrects a copy/paste error I have above
> 
> Index: rtprio.c
> ===================================================================
> --- rtprio.c š š(revision 216679)
> +++ rtprio.c š š(working copy)
> @@ -56,6 +56,7 @@
> š š š šchar š *p;
> š š š šint š š proc = 0;
> š š š šstruct rtprio rtp;
While there, you may change the type of proc to pid_t.
Also, move the initialization of proc out of local declaration section,
according to style(9).

> + š š š char *invalidChar;
We do not use camelCase, according to style(9).

> 
> š š š š/* find basename */
> š š š šif ((p = rindex(argv[0], '/')) == NULL)
> @@ -70,8 +71,9 @@
> 
> š š š šswitch (argc) {
> š š š šcase 2:
> - š š š š š š š proc = abs(atoi(argv[1])); š š š/* Should check if numeric
> - š š š š š š š š š š š š š š š š š š š š š š š š* arg! */
> + š š š š š š š proc = abs((int)strtol(argv[1], &invalidChar, 10));
Why is the cast needed there ?
Also, I think that doing
	proc = strtol();
	if (*invalid_char ...)
		...;
	proc = abs(proc);

> + š š š š š š š if (*invalidChar != '\0')
> + š š š š š š š š š š š errx(1,"Process should be a number");
Probably, "Pid" or "Process id" instead of "Process".

> š š š š š š š š/* FALLTHROUGH */
> š š š šcase 1:
> š š š š š š š šif (rtprio(RTP_LOOKUP, proc, &rtp) != 0)
> @@ -104,16 +106,20 @@
> š š š š š š š š š š š š š š š š š š š šbreak;
> š š š š š š š š š š š š š š š š}
> š š š š š š š š š š š š} else {
> - š š š š š š š š š š š š š š š rtp.prio = atoi(argv[1]);
> + š š š š š š š š š š š š š š š rtp.prio = (int)strtol(argv[1],
> &invalidChar, 10);
> + š š š š š š š š š š š š š š š if (*invalidChar != '\0')
> + š š š š š š š š š š š š š š š š š š š errx(1,"Priority should be a
> number", invalidChar);
I suspect that the line overflows 80 characters limit. Consider wrapping
it or unindenting.

> š š š š š š š š š š š š}
> š š š š š š š š} else {
> š š š š š š š š š š š šusage();
> š š š š š š š š š š š šbreak;
> š š š š š š š š}
> 
> - š š š š š š š if (argv[2][0] == '-')
> - š š š š š š š š š š š proc = -atoi(argv[2]);
> -
> + š š š š š š š if (argv[2][0] == '-') {
> + š š š š š š š š š š š proc = -(int)strtol(argv[2], &invalidChar, 10);
The easier solution would be
	proc = strtol(argv[2] + 1, &invalid_char, 10);
?
> + š š š š š š š š š š š if (*invalidChar != '\0')
> + š š š š š š š š š š š š š š š errx(1,"Process should be a number");
Again, Pid.
> + š š š š š š š }
> š š š š š š š šif (rtprio(RTP_SET, proc, &rtp) != 0)
> š š š š š š š š š š š šerr(1, "%s", argv[0]);
> 
The syntax of the prio commands is weird, there is an obvious corner
(or wrongly handled) case, where the command name starts with a digit.
Command name starting with dash is even harder.

[-- Attachment #2 --]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (FreeBSD)

iEYEARECAAYFAk0gUQQACgkQC3+MBN1Mb4ipKwCgpBcLWn6311tDaL+sdRjE57l5
CyQAn1ZahHOjHjBJtikaViSvgNNd9Qv4
=WbWg
-----END PGP SIGNATURE-----

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