tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

int vs. long in test(1)


sh(1)'s test (-current/amd64) doesn't deal with big numbers (> MAXINT)
correctly. Direct quote from src/bin/test/test.c:

static int
getn(const char *s)
        char *p;
        long r;

        errno = 0;
        r = strtol(s, &p, 10);

        if (errno != 0)
              error("%s: out of range", s);

        while (isspace((unsigned char)*p))

        if (*p)
              error("%s: bad number", s);

        return (int) r;

strtol is used to convert the number into a long, but then it is cast
to an int.
For big numbers:
On i386, the strtol fails and an "out of range" error is returned.
On amd64, the strtol succeeds and the cast loses significant bits.

Do we want to convert the code to use strtoimax() and return a long
(with checks) or even something bigger?

Or do we want it to stay an int and add the additional checks that
strtol(3) says we should?
I.e. replace the errno != 0 with something like:
        if ((errno == ERANGE && (r == LONG_MAX || r == LONG_MIN)) ||
            (lval > r || r < INT_MIN))
                error("%s: out of range", s);


Home | Main Index | Thread Index | Old Index