Index: lib/sanitizer_common/sanitizer_posix.cc =================================================================== --- lib/sanitizer_common/sanitizer_posix.cc +++ lib/sanitizer_common/sanitizer_posix.cc @@ -160,7 +160,7 @@ 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); Index: lib/sanitizer_common/sanitizer_rtems.cc =================================================================== --- lib/sanitizer_common/sanitizer_rtems.cc +++ lib/sanitizer_common/sanitizer_rtems.cc @@ -193,7 +193,7 @@ 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); Index: lib/sanitizer_common/tests/sanitizer_libc_test.cc =================================================================== --- lib/sanitizer_common/tests/sanitizer_libc_test.cc +++ lib/sanitizer_common/tests/sanitizer_libc_test.cc @@ -108,6 +108,12 @@ 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);