Questionable behavior of strtoi(3bsd) / strtou(3bsd)

Thorsten Glaser tg at mirbsd.de
Sun Jan 7 22:01:57 UTC 2024


Alejandro Colomar dixit:

>> I didn't design the functions, so not sure whether that was an
>> intentional choice or not, from the code PoV I can see the structure
>> making sense (in first making sure the string parsed is correct, before
>> evaluating what got parsed from it),

These functions are… rare, too.

But for this…

>That's not correct.  strtol(3) and relatives have (ignoring EINVAL) 3
>stages, not 2.  Firstly we make sure we parsed a number, secondly we

… compare prior art: OpenBSD strtonum(3).

tg at tglase-bsd:~ $ ./a.out
a.out: strtonum: errstr=<invalid>: Invalid argument
tg at tglase-bsd:~ $ cat x.c
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>

int
main(void)
{
        const char *errstr = NULL;
        long long ret;
        int eno;

        errno = 0;
        ret = strtonum("123456xyzzy", 1, 32767, &errstr);
        err(0, "strtonum: errstr=<%s>", errstr == NULL ? "nil" : errstr);
        /* NOTREACHED */
}

Indeed it checks for a valid number first, only then it checks the range
as that needs a valid number in the first place.

bye,
//mirabilos
-- 
> Hi, does anyone sell openbsd stickers by themselves and not packaged
> with other products?
No, the only way I've seen them sold is for $40 with a free OpenBSD CD.
	-- Haroon Khalid and Steve Shockley in gmane.os.openbsd.misc


More information about the libbsd mailing list