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/sanitizer_solaris.cc =================================================================== --- lib/sanitizer_common/sanitizer_solaris.cc +++ lib/sanitizer_common/sanitizer_solaris.cc @@ -78,7 +78,7 @@ uptr OpenFile(const char *filename, bool write) { return internal_open(filename, - write ? O_WRONLY | O_CREAT : O_RDONLY, 0660); + write ? O_WRONLY | O_CREAT | O_TRUNC : O_RDONLY, 0660); } DECLARE__REAL_AND_INTERNAL(uptr, read, fd_t fd, void *buf, uptr count) { 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);