[libc-commits] [libc] [libc] Support dynamically-grown lines in FlatFileDatabase (PR #223811)

Alexey Samsonov via libc-commits libc-commits at lists.llvm.org
Tue Sep 15 15:50:26 PDT 2026


================
@@ -98,13 +119,67 @@ template <typename EntryType> class FlatFileDatabase {
       --bytes_read;
 
     buf[bytes_read] = '\0';
-    return ReadLineResult{bytes_read, truncated, eof};
+    return ReadLineResult{bytes_read, raw_bytes_consumed, truncated, eof};
+  }
+
+  // Reads a single line into a caller-owned buffer, growing it as needed so
+  // that arbitrarily long records can be read. Otherwise behaves as read_line;
+  // the result is never truncated.
+  LIBC_INLINE static ErrorOr<ReadLineResult>
+  read_line_growing(File *f, DynamicBuffer &buf) {
+    if (!f)
+      return Error(EINVAL);
+
+    File::FileLock lock(f);
+    size_t bytes_read = 0;
+
+    while (true) {
+      // One byte for the character about to be read, one for the terminator.
+      if (bytes_read > cpp::numeric_limits<size_t>::max() - 2 ||
+          (bytes_read + 2 > buf.capacity() && !buf.reserve(bytes_read + 2))) {
+        char c = '\0';
+        while (true) {
+          FileIOResult drain = f->read_unlocked(&c, 1);
+          if (drain.has_error() || drain.value != 1 || c == '\n')
----------------
vonosmas wrote:

Not sure why we don't return Error(drain.error) in this case.

https://github.com/llvm/llvm-project/pull/223811


More information about the libc-commits mailing list