Index: lib/sanitizer_common/sanitizer_common_interceptors.inc =================================================================== --- lib/sanitizer_common/sanitizer_common_interceptors.inc +++ lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -7264,6 +7264,26 @@ #define INIT_NETENT #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); + 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); + } + 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(); @@ -7517,6 +7537,7 @@ INIT_TTYENT; INIT_PROTOENT; INIT_NETENT; + INIT_STRTONUM; INIT___PRINTF_CHK; } Index: lib/sanitizer_common/sanitizer_platform_interceptors.h =================================================================== --- lib/sanitizer_common/sanitizer_platform_interceptors.h +++ lib/sanitizer_common/sanitizer_platform_interceptors.h @@ -516,5 +516,6 @@ #define SANITIZER_INTERCEPT_TTYENT SI_NETBSD #define SANITIZER_INTERCEPT_PROTOENT SI_NETBSD #define SANITIZER_INTERCEPT_NETENT 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,39 @@ +// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s + +#define _OPENBSD_SOURCE + +#include +#include +#include + +int main(void) { + long long l; + const char *errstr; + + l = strtonum("100", 1, 100, &errstr); + if (errstr) + errx(1, "strtonum: %s", errstr); + printf("%lld\n", l); + + l = strtonum("200", 1, 100, &errstr); + if (!errstr) + errx(1, "strtonum"); + printf("%s\n", errstr); + + l = strtonum("300", 1000, 1001, &errstr); + if (!errstr) + errx(1, "strtonum"); + printf("%s\n", errstr); + + l = strtonum("abc", 1000, 1001, &errstr); + if (!errstr) + errx(1, "strtonum"); + printf("%s\n", errstr); + + // CHECK: 100 + // CHECK: too large + // CHECK: too small + // CHECK: invalid + + return 0; +}