Skip to content

test-parse-util fails on 32 bit - safe_atou does not ERANGE on negative numbers #1829

@martinpitt

Description

@martinpitt

on i386 (32 bit x86), this test fails:

$ ./test-parse-util 
Assertion 'parse_range("111--123", &lower, &upper) == -ERANGE' failed at src/test/test-parse-util.c:258, function test_parse_range(). Aborting.

Stepping through with gdb shows that the "-123" is not handled correctly in safe_atou:

303    int safe_atou(const char *s, unsigned *ret_u) {
[...]
311         l = strtoul(s, &x, 0);
(gdb) p s
$8 = 0x5658928f "-123"
(gdb) n
313         if (!x || x == s || *x || errno)
(gdb) p x
$9 = 0x56589293 ""
(gdb) p l
$10 = 4294967173
(gdb) p errno
$11 = 0

Thus this expects an ERANGE from strtoul(), but that instead converts it to a positive number. But this actually appears correct: the manpage says:

"The strtoul() function returns either the result of the conversion or, if there was a leading minus sign, the negation of the result of the conversion represented as an unsigned value, unless the original (nonnegated) value would overflow; in the latter case, strtoul() returns ULONG_MAX and sets errno to ERANGE."

So it would seem we need to check for a leading minus sign ourselves?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions