Index: lib/sanitizer_common/sanitizer_common_interceptors.inc =================================================================== --- lib/sanitizer_common/sanitizer_common_interceptors.inc +++ lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -7724,6 +7724,50 @@ #define INIT_MODCTL #endif +#if SANITIZER_INTERCEPT_STRTONUM +INTERCEPTOR(long long, strtonum, const char *nptr, long long minval, + long long maxval, const char **errstr) { + void *ctx; + INTMAX_T res = 0; + COMMON_INTERCEPTOR_ENTER(ctx, strtonum, nptr, minval, maxval, errstr); + const char *rerrstr; + if (!errstr) + errstr = &rerrstr; + + if (minval > maxval) { + *errstr = "invalid"; + } else { + int 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; + } + + 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 +#define INIT_STRTONUM +#endif + static void InitializeCommonInterceptors() { static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1]; interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap(); @@ -7988,6 +8032,7 @@ INIT_SYSCTLGETMIBINFO; INIT_NL_LANGINFO; INIT_MODCTL; + INIT_STRTONUM; INIT___PRINTF_CHK; } Index: lib/sanitizer_common/sanitizer_errno.cc =================================================================== --- lib/sanitizer_common/sanitizer_errno.cc +++ lib/sanitizer_common/sanitizer_errno.cc @@ -24,6 +24,7 @@ COMPILER_CHECK(errno_ENOMEM == ENOMEM); COMPILER_CHECK(errno_EBUSY == EBUSY); COMPILER_CHECK(errno_EINVAL == EINVAL); +COMPILER_CHECK(errno_ERANGE == ERANGE); // EOWNERDEAD is not present in some older platforms. #if defined(EOWNERDEAD) 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: lib/sanitizer_common/sanitizer_platform_interceptors.h =================================================================== --- lib/sanitizer_common/sanitizer_platform_interceptors.h +++ lib/sanitizer_common/sanitizer_platform_interceptors.h @@ -528,5 +528,6 @@ #define SANITIZER_INTERCEPT_SYSCTLGETMIBINFO SI_NETBSD #define SANITIZER_INTERCEPT_NL_LANGINFO (SI_NETBSD || SI_FREEBSD) #define SANITIZER_INTERCEPT_MODCTL SI_NETBSD +#define SANITIZER_INTERCEPT_STRTONUM SI_NETBSD #endif // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H 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; +}