[libc-commits] [libc] 5bcda1d - [libc] fix line buffered empty file writes

Michael Jones via libc-commits libc-commits at lists.llvm.org
Thu Jun 16 09:55:02 PDT 2022


Author: Michael Jones
Date: 2022-06-16T09:54:57-07:00
New Revision: 5bcda1d3a93797aabe00d9a6ddc2ecc6ab5e7c15

URL: https://github.com/llvm/llvm-project/commit/5bcda1d3a93797aabe00d9a6ddc2ecc6ab5e7c15
DIFF: https://github.com/llvm/llvm-project/commit/5bcda1d3a93797aabe00d9a6ddc2ecc6ab5e7c15.diff

LOG: [libc] fix line buffered empty file writes

Previously, any line buffered write of size 0 would cause an error.
The variable used to track the index of the last newline started at
the size of the write - 1, which underflowed. Now it's handled properly,
and a test has been added to prevent regressions.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D127914

Added: 
    

Modified: 
    libc/src/__support/File/file.cpp
    libc/test/src/__support/File/file_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/File/file.cpp b/libc/src/__support/File/file.cpp
index 249f44549d4d6..135bc406871e9 100644
--- a/libc/src/__support/File/file.cpp
+++ b/libc/src/__support/File/file.cpp
@@ -130,9 +130,9 @@ size_t File::write_unlocked_fbf(const void *data, size_t len) {
 size_t File::write_unlocked_lbf(const void *data, size_t len) {
   constexpr char NEWLINE_CHAR = '\n';
   size_t last_newline = len;
-  for (size_t i = len - 1; i > 0; --i) {
-    if (static_cast<const char *>(data)[i] == NEWLINE_CHAR) {
-      last_newline = i;
+  for (size_t i = len; i > 1; --i) {
+    if (static_cast<const char *>(data)[i - 1] == NEWLINE_CHAR) {
+      last_newline = i - 1;
       break;
     }
   }

diff  --git a/libc/test/src/__support/File/file_test.cpp b/libc/test/src/__support/File/file_test.cpp
index e41295bf07977..271f2dce4ef55 100644
--- a/libc/test/src/__support/File/file_test.cpp
+++ b/libc/test/src/__support/File/file_test.cpp
@@ -458,3 +458,30 @@ TEST(LlvmLibcFileTest, ZeroLengthBuffer) {
   ASSERT_EQ(f_lbf->close(), 0);
   ASSERT_EQ(f_nbf->close(), 0);
 }
+
+TEST(LlvmLibcFileTest, WriteNothing) {
+  const char WRITE_DATA[] = "";
+  constexpr size_t WRITE_SIZE = 0;
+  constexpr size_t FILE_BUFFER_SIZE = 5;
+  char file_buffer_fbf[FILE_BUFFER_SIZE];
+  char file_buffer_lbf[FILE_BUFFER_SIZE];
+  char file_buffer_nbf[FILE_BUFFER_SIZE];
+  StringFile *f_fbf =
+      new_string_file(file_buffer_fbf, FILE_BUFFER_SIZE, _IOFBF, false, "w");
+  StringFile *f_lbf =
+      new_string_file(file_buffer_lbf, FILE_BUFFER_SIZE, _IOLBF, false, "w");
+  StringFile *f_nbf =
+      new_string_file(file_buffer_nbf, FILE_BUFFER_SIZE, _IONBF, false, "w");
+
+  ASSERT_EQ(WRITE_SIZE, f_fbf->write(WRITE_DATA, WRITE_SIZE));
+  ASSERT_EQ(WRITE_SIZE, f_lbf->write(WRITE_DATA, WRITE_SIZE));
+  ASSERT_EQ(WRITE_SIZE, f_nbf->write(WRITE_DATA, WRITE_SIZE));
+
+  ASSERT_FALSE(f_fbf->error_unlocked());
+  ASSERT_FALSE(f_lbf->error_unlocked());
+  ASSERT_FALSE(f_nbf->error_unlocked());
+
+  ASSERT_EQ(f_fbf->close(), 0);
+  ASSERT_EQ(f_lbf->close(), 0);
+  ASSERT_EQ(f_nbf->close(), 0);
+}


        


More information about the libc-commits mailing list