mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
Fix strtol(3) handling negative numbers incorrectly.
This commit is contained in:
parent
00a3579940
commit
1b3f4b36d8
1 changed files with 4 additions and 3 deletions
|
@ -144,7 +144,7 @@ STRTOL_INT STRTOL(const STRTOL_CHAR* restrict str,
|
||||||
// Handle a leading sign character.
|
// Handle a leading sign character.
|
||||||
if ( c == STRTOL_L('-') )
|
if ( c == STRTOL_L('-') )
|
||||||
str++, negative = true;
|
str++, negative = true;
|
||||||
if ( c == STRTOL_L('+') )
|
else if ( c == STRTOL_L('+') )
|
||||||
str++, negative = false;
|
str++, negative = false;
|
||||||
|
|
||||||
// Autodetect base 8 or base 16.
|
// Autodetect base 8 or base 16.
|
||||||
|
@ -194,7 +194,8 @@ STRTOL_INT STRTOL(const STRTOL_CHAR* restrict str,
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
STRTOL_INT new_result = result * (STRTOL_INT) base;
|
STRTOL_INT new_result = result * (STRTOL_INT) base;
|
||||||
assert(result <= new_result);
|
assert( negative || result <= new_result);
|
||||||
|
assert(!negative || result >= new_result);
|
||||||
result = new_result;
|
result = new_result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,7 +206,7 @@ STRTOL_INT STRTOL(const STRTOL_CHAR* restrict str,
|
||||||
|
|
||||||
// Attempt to subtract the latest digit to the accumulator (negative).
|
// Attempt to subtract the latest digit to the accumulator (negative).
|
||||||
else if ( (!STRTOL_INT_IS_UNSIGNED && negative) &&
|
else if ( (!STRTOL_INT_IS_UNSIGNED && negative) &&
|
||||||
(STRTOL_INT) val <= (STRTOL_INT) (result - STRTOL_INT_MIN) )
|
(STRTOL_UNSIGNED_INT) val < ((STRTOL_UNSIGNED_INT) result - (STRTOL_UNSIGNED_INT) STRTOL_INT_MIN) )
|
||||||
result -= (STRTOL_INT) val;
|
result -= (STRTOL_INT) val;
|
||||||
|
|
||||||
// Handle the case where the addition/subtract would overflow/underflow.
|
// Handle the case where the addition/subtract would overflow/underflow.
|
||||||
|
|
Loading…
Add table
Reference in a new issue