tech-userlevel archive

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

Re: int vs. long in test(1)

On Jun 18, 2008, at 9:26 PM, Thomas Klausner wrote:


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);

1003.1 claims all sh arithmetic requires "signed long" (as defined in C) as a minimum. You can go beyond that but for amd64 it's clear this is currently broken then.

However, it would be nice to be consistent across all platforms too. We could simply allow 64bit sizes here always.


Home | Main Index | Thread Index | Old Index