Index: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc =================================================================== --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -7728,6 +7728,30 @@ #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; + COMMON_INTERCEPTOR_ENTER(ctx, strtonum, nptr, minval, maxval, errstr); + + // TODO(kamil): Implement strtoll as a common inteceptor + char *real_endptr; + long long ret = (long long)REAL(strtoimax)(nptr, &real_endptr, 10); + StrtolFixAndCheck(ctx, nptr, nullptr, real_endptr, 10); + + 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); + } + return ret; +} +#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(); @@ -7992,6 +8016,7 @@ INIT_SYSCTLGETMIBINFO; INIT_NL_LANGINFO; INIT_MODCTL; + INIT_STRTONUM; INIT___PRINTF_CHK; } Index: compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h =================================================================== --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h @@ -529,5 +529,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: compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/strtonum.cc =================================================================== --- compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/strtonum.cc +++ compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/strtonum.cc @@ -0,0 +1,52 @@ +// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s + +#define _OPENBSD_SOURCE + +#include +#include +#include + +int main(void) { + const char *errstr; + + printf("strtonum\n"); + + long long l = strtonum("100", 1, 100, &errstr); + assert(!errstr); + printf("%lld\n", l); + + l = strtonum("200", 1, 100, &errstr); + assert(errstr); + printf("%s\n", errstr); + + l = strtonum("300", 1000, 1001, &errstr); + assert(errstr); + printf("%s\n", errstr); + + l = strtonum("abc", 1000, 1001, &errstr); + assert(errstr); + printf("%s\n", errstr); + + l = strtonum("1000", 1001, 1000, &errstr); + assert(errstr); + printf("%s\n", errstr); + + l = strtonum("1000abc", 1000, 1001, &errstr); + assert(errstr); + printf("%s\n", errstr); + + l = strtonum("1000.0", 1000, 1001, &errstr); + assert(errstr); + printf("%s\n", errstr); + + // CHECK: strtonum + // CHECK: 100 + // CHECK: too large + // CHECK: too small + // CHECK: invalid + // CHECK: invalid + // CHECK: invalid + // CHECK: invalid + + return 0; +}