Index: lib/sanitizer_common/sanitizer_common_interceptors.inc =================================================================== --- lib/sanitizer_common/sanitizer_common_interceptors.inc +++ lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -7728,16 +7728,43 @@ INTERCEPTOR(long long, strtonum, const char *nptr, long long minval, long long maxval, const char **errstr) { void *ctx; + INTMAX_T res; + int saved_errno; COMMON_INTERCEPTOR_ENTER(ctx, strtonum, nptr, minval, maxval, errstr); - if (nptr) - COMMON_INTERCEPTOR_READ_RANGE(ctx, nptr, REAL(strlen)(nptr) + 1); - long long ret = REAL(strtonum)(nptr, minval, maxval, errstr); - if (errstr) { - COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errstr, sizeof(const char *)); - if (*errstr) - COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *errstr, REAL(strlen)(*errstr) + 1); + const char *rerrstr; + if (!errstr) + errstr = &rerrstr; + + if (minval > maxval) { + *errstr = "invalid"; + goto skip; } - return ret; + + saved_errno = errno; + errno = 0; + + char *real_endptr; + res = REAL(strtoimax)(nptr, &real_endptr, 10); + StrtolFixAndCheck(ctx, nptr, nullptr, real_endptr, 10); + if (nptr == real_endptr || *real_endptr != '\0') + *errstr = "invalid"; + else if ((res == INTMAX_MAX && errno == errno_ERANGE) || (maxval < res)) + *errstr = "too large"; + else if ((res == INTMAX_MIN && errno == errno_ERANGE) || (minval > res)) + *errstr = "too small"; + else + *errstr = nullptr; + + errno = saved_errno; + + skip: + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errstr, sizeof(*errstr)); + if (*errstr) { + res = 0; + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *errstr, REAL(strlen)(*errstr) + 1); + } + + return res; } #define INIT_STRTONUM COMMON_INTERCEPT_FUNCTION(strtonum) #else Index: lib/sanitizer_common/sanitizer_errno_codes.h =================================================================== --- lib/sanitizer_common/sanitizer_errno_codes.h +++ lib/sanitizer_common/sanitizer_errno_codes.h @@ -25,6 +25,7 @@ #define errno_ENOMEM 12 #define errno_EBUSY 16 #define errno_EINVAL 22 +#define errno_ERANGE 34 // Those might not present or their value differ on different platforms. extern const int errno_EOWNERDEAD; Index: lib/sanitizer_common/sanitizer_internal_defs.h =================================================================== --- lib/sanitizer_common/sanitizer_internal_defs.h +++ lib/sanitizer_common/sanitizer_internal_defs.h @@ -363,6 +363,10 @@ #define INT64_MAX (__INT64_C(9223372036854775807)) #undef UINT64_MAX #define UINT64_MAX (__UINT64_C(18446744073709551615)) +#undef INTMAX_MIN +#define INTMAX_MIN INT64_MIN +#undef INTMAX_MAX +#define INTMAX_MAX INT64_MAX #undef UINTPTR_MAX #if SANITIZER_WORDSIZE == 64 # define UINTPTR_MAX (18446744073709551615UL) Index: test/sanitizer_common/TestCases/NetBSD/strtonum.cc =================================================================== --- /dev/null +++ test/sanitizer_common/TestCases/NetBSD/strtonum.cc @@ -0,0 +1,58 @@ +// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s + +#define _OPENBSD_SOURCE + +#include +#include + +int main(void) { + const char *errstr; + + printf("strtonum\n"); + + long long l = strtonum("100", 1, 100, &errstr); + if (errstr) + abort(); + printf("%lld\n", l); + + l = strtonum("200", 1, 100, &errstr); + if (!errstr) + abort(); + printf("%s\n", errstr); + + l = strtonum("300", 1000, 1001, &errstr); + if (!errstr) + abort(); + printf("%s\n", errstr); + + l = strtonum("abc", 1000, 1001, &errstr); + if (!errstr) + abort(); + printf("%s\n", errstr); + + l = strtonum("1000", 1001, 1000, &errstr); + if (!errstr) + abort(); + printf("%s\n", errstr); + + l = strtonum("1000abc", 1000, 1001, &errstr); + if (!errstr) + abort(); + printf("%s\n", errstr); + + l = strtonum("1000.0", 1000, 1001, &errstr); + if (!errstr) + abort(); + printf("%s\n", errstr); + + // CHECK: strtonum + // CHECK: 100 + // CHECK: too large + // CHECK: too small + // CHECK: invalid + // CHECK: invalid + // CHECK: invalid + // CHECK: invalid + + return 0; +}