From ec079cb67350ae987e40ee21eb88fab415e728a0 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 1 Mar 2022 15:50:49 +0100 Subject: [PATCH] bpo-46887: Work around clang MSAN bug on stat()/fstat() Initialize the input "struct stat" buffer with zeros to prevent a false alarm on calling stat() and fstat(). --- Modules/posixmodule.c | 6 ++++++ Python/fileutils.c | 14 +++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 3431c85e2dfde9..4c88f3ee6bc8e9 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -2484,6 +2484,12 @@ posix_do_stat(PyObject *module, const char *function_name, path_t *path, STRUCT_STAT st; int result; +# if __has_feature(memory_sanitizer) + // bpo-46887: Work around clang MSAN bug: + // https://github.com/llvm/llvm-project/issues/54131 + memset(&st, 0, sizeof(st)); +# endif + #ifdef HAVE_FSTATAT int fstatat_unavailable = 0; #endif diff --git a/Python/fileutils.c b/Python/fileutils.c index 9a71b83f45578a..b11181e5120fa6 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -1166,6 +1166,13 @@ _Py_fstat_noraise(int fd, struct _Py_stat_struct *status) status->st_ino = (((uint64_t)info.nFileIndexHigh) << 32) + info.nFileIndexLow; return 0; #else + +# if __has_feature(memory_sanitizer) + // bpo-46887: Work around clang MSAN bug: + // https://github.com/llvm/llvm-project/issues/54131 + memset(status, 0, sizeof(*status)); +# endif + return fstat(fd, status); #endif } @@ -1225,6 +1232,11 @@ _Py_wstat(const wchar_t* path, struct stat *buf) errno = EINVAL; return -1; } +# if __has_feature(memory_sanitizer) + // bpo-46887: Work around clang MSAN bug: + // https://github.com/llvm/llvm-project/issues/54131 + memset(buf, 0, sizeof(*buf)); +# endif err = stat(fname, buf); PyMem_RawFree(fname); #endif @@ -2093,7 +2105,7 @@ join_relfile(wchar_t *buffer, size_t bufsize, const wchar_t *dirname, const wchar_t *relfile) { #ifdef MS_WINDOWS - if (FAILED(PathCchCombineEx(buffer, bufsize, dirname, relfile, + if (FAILED(PathCchCombineEx(buffer, bufsize, dirname, relfile, PATHCCH_ALLOW_LONG_PATHS))) { return -1; }