Re: [patch] have rtprio check that arguments are numeric; change atoi to strtol

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Giorgos Keramidas
Date: Sunday, January 2, 2011 - 12:29 pm

On Sun, 2 Jan 2011 12:18:45 +0200, Kostik Belousov <kostikbel@gmail.com> wrote:

It's quite surprising how easy it is to use strtol() in an allegedly
"safe" manner, but miss some of the edge cases. We should probably check
for errno too, e.g.:

    #include <errno.h>
    #include <string.h>
    #include <stdlib.h>

    pid_t proc;
    long x;
    char *endp;

    errno = 0;
    x = strtol(argv[1], &endp, 0);
    if (errno != 0 || (endp != NULL && endp != str && *endp != '\0' &&
        (isdigit(*endp) == 0 || isspace(*endp) == 0)))
            error();

Then if we want to avoid overflows of pid_t, we might have to check
against PID_MAX or at least INT32_MAX.  The sizeof(pid_t) is __int32_t
on all FreeBSD architectures, so it may be useful to check for:

    if (x >= INT32_MAX)
            error();
    proc = (pid_t)x;

But this is probably being too paranoid now.

_______________________________________________
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org"
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Re: [patch] have rtprio check that arguments are numeric; ..., Giorgos Keramidas, (Sun Jan 2, 12:29 pm)
Re: [patch] have rtprio check that arguments are numeric; ..., Giorgos Keramidas, (Tue Jan 4, 3:36 am)
Re: [patch] have rtprio check that arguments are numeric; ..., Giorgos Keramidas, (Tue Jan 4, 3:40 am)
Re: [patch] have rtprio check that arguments are numeric; ..., Giorgos Keramidas, (Tue Jan 4, 5:26 am)
Re: [patch] have rtprio check that arguments are numeric; ..., Giorgos Keramidas, (Tue Jan 4, 11:12 am)