diff --git a/libc/src/__support/File/file.cpp b/libc/src/__support/File/file.cpp index 303852dbbb717..4217e73828388 100644 --- a/libc/src/__support/File/file.cpp +++ b/libc/src/__support/File/file.cpp @@ -123,7 +123,7 @@ FileIOResult File::write_unlocked_fbf(const uint8_t *data, size_t len) { FileIOResult result = platform_write(this, remainder.data(), remainder.size()); - size_t bytes_written = buf_result.value; + size_t bytes_written = result.value; // If less bytes were written than expected, then an error occurred. Return // the number of bytes that have been written from |data|. diff --git a/libc/test/src/__support/File/file_test.cpp b/libc/test/src/__support/File/file_test.cpp index b3c9f2ba49bce..04ab78b07d04c 100644 --- a/libc/test/src/__support/File/file_test.cpp +++ b/libc/test/src/__support/File/file_test.cpp @@ -493,3 +493,21 @@ TEST(LlvmLibcFileTest, WriteNothing) { ASSERT_EQ(f_lbf->close(), 0); ASSERT_EQ(f_nbf->close(), 0); } + +TEST(LlvmLibcFileTest, WriteSplit) { + constexpr size_t FILE_BUFFER_SIZE = 8; + char file_buffer[FILE_BUFFER_SIZE]; + StringFile *f = + new_string_file(file_buffer, FILE_BUFFER_SIZE, _IOFBF, false, "w"); + + static constexpr size_t AVAIL = 12; + f->seek(-AVAIL, SEEK_END); + + const char data[] = "hello"; + ASSERT_EQ(sizeof(data) - 1, f->write(data, sizeof(data) - 1).value); + + const char data2[] = " extra data"; + static constexpr size_t WR_EXPECTED = AVAIL - (sizeof(data) - 1); + ASSERT_EQ(WR_EXPECTED, f->write(data2, sizeof(data2) - 1).value); + EXPECT_TRUE(f->error()); +}