[sanitizer] Fix PR17138.

strerror_r on OSX returns a positive error code when the errno value is
unknown. Buffer contents are initialized in any case.

llvm-svn: 190295
This commit is contained in:
Evgeniy Stepanov 2013-09-09 08:58:54 +00:00
parent a4a59d7468
commit cf02f171a9
2 changed files with 17 additions and 2 deletions

View File

@ -0,0 +1,13 @@
// RUN: %clangxx_asan -O0 %s -o %t && %t
// Regression test for PR17138.
#include <assert.h>
#include <string.h>
int main() {
char buf[1024];
char *res = (char *)strerror_r(300, buf, sizeof(buf));
assert(res != 0);
return 0;
}

View File

@ -1807,12 +1807,14 @@ INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
// writes message to buf.
// * GNU version returns message pointer, which points to either buf or some
// static storage.
if (!res) {
SIZE_T posix_res = (SIZE_T)res;
if (posix_res < 1024 || posix_res > (SIZE_T) - 1024) {
// POSIX version. Spec is not clear on whether buf is NULL-terminated.
// At least on OSX, buf contents are valid even when the call fails.
SIZE_T sz = internal_strnlen(buf, buflen);
if (sz < buflen) ++sz;
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
} else if ((SIZE_T)res < (SIZE_T) - 1024) {
} else {
// GNU version.
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
}