mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-05 17:30:27 +00:00
standardize strtol behavior when result is out of bounds
This commit is contained in:
parent
aad841610e
commit
9a2dd849c1
5 changed files with 50 additions and 12 deletions
|
@ -17,6 +17,7 @@
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
#include "libc/fmt/conv.h"
|
#include "libc/fmt/conv.h"
|
||||||
|
#include "libc/errno.h"
|
||||||
#include "libc/limits.h"
|
#include "libc/limits.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,5 +26,15 @@
|
||||||
* @param optional_base is recommended as 0 for flexidecimal
|
* @param optional_base is recommended as 0 for flexidecimal
|
||||||
*/
|
*/
|
||||||
long strtol(const char *s, char **opt_out_end, int optional_base) {
|
long strtol(const char *s, char **opt_out_end, int optional_base) {
|
||||||
return strtoimax(s, opt_out_end, optional_base);
|
intmax_t res;
|
||||||
|
res = strtoimax(s, opt_out_end, optional_base);
|
||||||
|
if (res < LONG_MIN) {
|
||||||
|
errno = ERANGE;
|
||||||
|
return LONG_MIN;
|
||||||
|
}
|
||||||
|
if (res > LONG_MAX) {
|
||||||
|
errno = ERANGE;
|
||||||
|
return LONG_MAX;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,12 +17,19 @@
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
#include "libc/fmt/conv.h"
|
#include "libc/fmt/conv.h"
|
||||||
|
#include "libc/errno.h"
|
||||||
#include "libc/limits.h"
|
#include "libc/limits.h"
|
||||||
|
|
||||||
long long strtoll(const char *s, char **endptr, int optional_base) {
|
long long strtoll(const char *s, char **endptr, int optional_base) {
|
||||||
long long res;
|
intmax_t res;
|
||||||
res = strtoimax(s, endptr, optional_base);
|
res = strtoimax(s, endptr, optional_base);
|
||||||
if (res < LONG_LONG_MIN) return LONG_LONG_MIN;
|
if (res < LONG_LONG_MIN) {
|
||||||
if (res > LONG_LONG_MAX) return LONG_LONG_MAX;
|
errno = ERANGE;
|
||||||
|
return LONG_LONG_MIN;
|
||||||
|
}
|
||||||
|
if (res > LONG_LONG_MAX) {
|
||||||
|
errno = ERANGE;
|
||||||
|
return LONG_LONG_MAX;
|
||||||
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,12 +17,19 @@
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
#include "libc/fmt/conv.h"
|
#include "libc/fmt/conv.h"
|
||||||
|
#include "libc/errno.h"
|
||||||
#include "libc/limits.h"
|
#include "libc/limits.h"
|
||||||
|
|
||||||
unsigned long strtoul(const char *s, char **endptr, int optional_base) {
|
unsigned long strtoul(const char *s, char **endptr, int optional_base) {
|
||||||
unsigned long long res;
|
intmax_t res;
|
||||||
res = strtoimax(s, endptr, optional_base);
|
res = strtoimax(s, endptr, optional_base);
|
||||||
if (res < ULONG_MIN) return ULONG_MIN;
|
if (res < ULONG_MIN) {
|
||||||
if (res > ULONG_MAX) return ULONG_MAX;
|
errno = ERANGE;
|
||||||
|
return ULONG_MIN;
|
||||||
|
}
|
||||||
|
if (res > ULONG_MAX) {
|
||||||
|
errno = ERANGE;
|
||||||
|
return ULONG_MAX;
|
||||||
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,12 +17,19 @@
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
#include "libc/fmt/conv.h"
|
#include "libc/fmt/conv.h"
|
||||||
|
#include "libc/errno.h"
|
||||||
#include "libc/limits.h"
|
#include "libc/limits.h"
|
||||||
|
|
||||||
unsigned long long strtoull(const char *s, char **endptr, int optional_base) {
|
unsigned long long strtoull(const char *s, char **endptr, int optional_base) {
|
||||||
unsigned long long res;
|
intmax_t res;
|
||||||
res = strtoimax(s, endptr, optional_base);
|
res = strtoimax(s, endptr, optional_base);
|
||||||
if (res < ULONG_LONG_MIN) return ULONG_LONG_MIN;
|
if (res < ULONG_LONG_MIN) {
|
||||||
if (res > ULONG_LONG_MAX) return ULONG_LONG_MAX;
|
errno = ERANGE;
|
||||||
|
return ULONG_LONG_MIN;
|
||||||
|
}
|
||||||
|
if (res > ULONG_LONG_MAX) {
|
||||||
|
errno = ERANGE;
|
||||||
|
return ULONG_LONG_MAX;
|
||||||
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
#include "libc/bits/bits.h"
|
#include "libc/bits/bits.h"
|
||||||
#include "libc/fmt/conv.h"
|
#include "libc/fmt/conv.h"
|
||||||
#include "libc/testlib/testlib.h"
|
#include "libc/testlib/testlib.h"
|
||||||
|
#include "libc/errno.h"
|
||||||
|
|
||||||
TEST(strtoimax, testZero) {
|
TEST(strtoimax, testZero) {
|
||||||
EXPECT_EQ(0, strtoimax("0", NULL, 0));
|
EXPECT_EQ(0, strtoimax("0", NULL, 0));
|
||||||
|
@ -57,6 +58,11 @@ TEST(strtoimax, testTwosBane) {
|
||||||
strtoimax("0x80000000000000000000000000000000", NULL, 0));
|
strtoimax("0x80000000000000000000000000000000", NULL, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(strtol, neghex) {
|
TEST(strtol, testOutsideLimit) {
|
||||||
ASSERT_EQ(-16, strtol("0xfffffffffffffff0", NULL, 0));
|
errno = 0;
|
||||||
|
EXPECT_EQ(0x7fffffffffffffff, strtol("0x8000000000000000", NULL, 0));
|
||||||
|
EXPECT_EQ(ERANGE, errno);
|
||||||
|
errno = 0;
|
||||||
|
EXPECT_EQ(0x8000000000000000, strtol("-0x8000000000000001", NULL, 0));
|
||||||
|
EXPECT_EQ(ERANGE, errno);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue