[libc] Fix copy/paste error in file.cpp (#150802)
Fix using wrong variable due to copy/paste error.
---------
Co-authored-by: codefaber <codefaber>
GitOrigin-RevId: fd7f69bfe7b694d22cd74058faa90b8a93a6d949
diff --git a/src/__support/File/file.cpp b/src/__support/File/file.cpp
index 303852d..4217e73 100644
--- a/src/__support/File/file.cpp
+++ b/src/__support/File/file.cpp
@@ -123,7 +123,7 @@
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/test/src/__support/File/file_test.cpp b/test/src/__support/File/file_test.cpp
index b3c9f2b..04ab78b 100644
--- a/test/src/__support/File/file_test.cpp
+++ b/test/src/__support/File/file_test.cpp
@@ -493,3 +493,21 @@
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());
+}