[compiler-rt] r334881 - [sanitizer_common] Use O_TRUNC for WrOnly access mode.
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 15 20:32:59 PDT 2018
Author: maskray
Date: Fri Jun 15 20:32:59 2018
New Revision: 334881
URL: http://llvm.org/viewvc/llvm-project?rev=334881&view=rev
Log:
[sanitizer_common] Use O_TRUNC for WrOnly access mode.
Summary: Otherwise if the file existed and was larger than the write size before the OpenFile call, the file will not be truncated and contain garbage in trailing bytes.
Reviewers: glider, kcc, vitalybuka
Subscribers: kubamracek, delcypher, llvm-commits, #sanitizers
Differential Revision: https://reviews.llvm.org/D48250
Modified:
compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc
compiler-rt/trunk/lib/sanitizer_common/sanitizer_rtems.cc
compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_libc_test.cc
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc?rev=334881&r1=334880&r2=334881&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc Fri Jun 15 20:32:59 2018
@@ -160,7 +160,7 @@ fd_t OpenFile(const char *filename, File
int flags;
switch (mode) {
case RdOnly: flags = O_RDONLY; break;
- case WrOnly: flags = O_WRONLY | O_CREAT; break;
+ case WrOnly: flags = O_WRONLY | O_CREAT | O_TRUNC; break;
case RdWr: flags = O_RDWR | O_CREAT; break;
}
fd_t res = internal_open(filename, flags, 0660);
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_rtems.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_rtems.cc?rev=334881&r1=334880&r2=334881&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_rtems.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_rtems.cc Fri Jun 15 20:32:59 2018
@@ -193,7 +193,7 @@ fd_t OpenFile(const char *filename, File
int flags;
switch (mode) {
case RdOnly: flags = O_RDONLY; break;
- case WrOnly: flags = O_WRONLY | O_CREAT; break;
+ case WrOnly: flags = O_WRONLY | O_CREAT | O_TRUNC; break;
case RdWr: flags = O_RDWR | O_CREAT; break;
}
fd_t res = open(filename, flags, 0660);
Modified: compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_libc_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_libc_test.cc?rev=334881&r1=334880&r2=334881&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_libc_test.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_libc_test.cc Fri Jun 15 20:32:59 2018
@@ -108,6 +108,12 @@ TEST(SanitizerCommon, FileOps) {
temp_file_name(tmpfile, sizeof(tmpfile), "sanitizer_common.fileops.tmp.");
fd_t fd = OpenFile(tmpfile, WrOnly);
ASSERT_NE(fd, kInvalidFd);
+ ASSERT_FALSE(internal_iserror(internal_write(fd, "A", 1)));
+ CloseFile(fd);
+
+ fd = OpenFile(tmpfile, WrOnly);
+ ASSERT_NE(fd, kInvalidFd);
+ EXPECT_EQ(internal_lseek(fd, 0, SEEK_END), 0u);
uptr bytes_written = 0;
EXPECT_TRUE(WriteToFile(fd, str1, len1, &bytes_written));
EXPECT_EQ(len1, bytes_written);
More information about the llvm-commits
mailing list