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,91 @@ #define INIT_MODCTL #endif +#if SANITIZER_INTERCEPT_STRTOI +INTERCEPTOR(INTMAX_T, strtoi, const char *nptr, char **endptr, int base, + INTMAX_T low, INTMAX_T high, int *rstatus) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, strtoi, nptr, endptr, base, low, high, rstatus); + int rrstatus; + if (!rstatus) + rstatus = &rrstatus; + + int saved_errno = errno; + errno = 0; + + char *real_endptr; + INTMAX_T ret = REAL(strtoimax)(nptr, &real_endptr, base); + StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base); + + *rstatus = errno; + errno = saved_errno; + + if (!*rstatus) { + if (nptr == *endptr) + *rstatus = errno_ECANCELED; + else if (**endptr != '\0') + *rstatus = errno_ENOTSUP; + } + + if (high < ret || low > ret) { + if (!*rstatus) + *rstatus = errno_ERANGE; + if (high < ret) + ret = high; + if (low > ret) + ret = low; + } + + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus)); + + return ret; +} + +INTERCEPTOR(UINTMAX_T, strtou, const char *nptr, char **endptr, int base, + UINTMAX_T low, UINTMAX_T high, int *rstatus) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, strtou, nptr, endptr, base, low, high, rstatus); + int rrstatus; + if (!rstatus) + rstatus = &rrstatus; + + int saved_errno = errno; + errno = 0; + + char *real_endptr; + UINTMAX_T ret = REAL(strtoumax)(nptr, &real_endptr, base); + StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base); + + *rstatus = errno; + errno = saved_errno; + + if (!*rstatus) { + if (nptr == *endptr) + *rstatus = errno_ECANCELED; + else if (**endptr != '\0') + *rstatus = errno_ENOTSUP; + } + + if (high < ret || low > ret) { + if (!*rstatus) + *rstatus = errno_ERANGE; + if (high < ret) + ret = high; + if (low > ret) + ret = low; + } + + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus)); + + return ret; +} +#define INIT_STRTOI \ + COMMON_INTERCEPT_FUNCTION(strtoi); \ + COMMON_INTERCEPT_FUNCTION(strtou) +#else +#define INIT_STRTOI +#endif + static void InitializeCommonInterceptors() { static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1]; interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap(); @@ -7988,6 +8073,7 @@ INIT_SYSCTLGETMIBINFO; INIT_NL_LANGINFO; INIT_MODCTL; + INIT_STRTOI; 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) @@ -32,4 +33,16 @@ extern const int errno_EOWNERDEAD = -1; #endif +#if defined(ECANCELED) +extern const int errno_ECANCELED = ECANCELED; +#else +extern const int errno_ECANCELED = -1; +#endif + +#if defined(ENOTSUP) +extern const int errno_ENOTSUP = ENOTSUP; +#else +extern const int errno_ENOTSUP = -1; +#endif + } // namespace __sanitizer Index: lib/sanitizer_common/sanitizer_errno_codes.h =================================================================== --- lib/sanitizer_common/sanitizer_errno_codes.h +++ lib/sanitizer_common/sanitizer_errno_codes.h @@ -25,9 +25,12 @@ #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; +extern const int errno_ECANCELED; +extern const int errno_ENOTSUP; } // namespace __sanitizer 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_STRTOI SI_NETBSD #endif // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H Index: test/sanitizer_common/TestCases/NetBSD/strtoi.cc =================================================================== --- /dev/null +++ test/sanitizer_common/TestCases/NetBSD/strtoi.cc @@ -0,0 +1,85 @@ +// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s + +#include +#include + +void test_strtoi1() { + const char *cent = "100"; + char *p; + int status; + intmax_t i = strtoi(cent, &p, 0, 1, 1000, &status); + printf("strtoi: conversion of '%s' to a number %s, using %jd, p=%#" PRIx8 + "\n", + cent, status ? "failed" : "successful", i, *p); +} + +void test_strtoi2() { + const char *cent = "100"; + char *p; + int status; + intmax_t i = strtoi(cent, &p, 0, 1, 10, &status); + printf("strtoi: conversion of '%s' to a number %s, using %jd, p=%#" PRIx8 + "\n", + cent, status ? "failed" : "successful", i, *p); +} + +void test_strtoi3() { + const char *cent = "100xyz"; + char *p; + int status; + intmax_t i = strtoi(cent, &p, 0, 1, 10, &status); + printf("strtoi: conversion of '%s' to a number %s, using %jd, p=%#" PRIx8 + "\n", + cent, status ? "failed" : "successful", i, *p); +} + +void test_strtou1() { + const char *cent = "100"; + char *p; + int status; + uintmax_t i = strtou(cent, &p, 0, 1, 1000, &status); + printf("strtou: conversion of '%s' to a number %s, using %ju, p=%#" PRIx8 + "\n", + cent, status ? "failed" : "successful", i, *p); +} + +void test_strtou2() { + const char *cent = "100"; + char *p; + int status; + uintmax_t i = strtou(cent, &p, 0, 1, 10, &status); + printf("strtou: conversion of '%s' to a number %s, using %ju, p=%#" PRIx8 + "\n", + cent, status ? "failed" : "successful", i, *p); +} + +void test_strtou3() { + const char *cent = "100xyz"; + char *p; + int status; + uintmax_t i = strtou(cent, &p, 0, 1, 10, &status); + printf("strtou: conversion of '%s' to a number %s, using %ju, p=%#" PRIx8 + "\n", + cent, status ? "failed" : "successful", i, *p); +} + +int main(void) { + printf("strtoi\n"); + + test_strtoi1(); + test_strtoi2(); + test_strtoi3(); + test_strtou1(); + test_strtou2(); + test_strtou3(); + + // CHECK: strtoi + // CHECK: strtoi: conversion of '100' to a number successful, using 100, p=0 + // CHECK: strtoi: conversion of '100' to a number failed, using 10, p=0 + // CHECK: strtoi: conversion of '100xyz' to a number failed, using 10, p=0x78 + // CHECK: strtou: conversion of '100' to a number successful, using 100, p=0 + // CHECK: strtou: conversion of '100' to a number failed, using 10, p=0 + // CHECK: strtou: conversion of '100xyz' to a number failed, using 10, p=0x78 + + return 0; +}