[libcxx-commits] [libcxx] [libc++][format] Don't leave the output buffer full after a bulk write (PR #226791)

via libcxx-commits libcxx-commits at lists.llvm.org
Sun Sep 27 09:55:19 PDT 2026


================
@@ -250,6 +250,10 @@ class __output_buffer {
       __first += __chunk;
       __n -= __chunk;
     } while (__n);
+
+    // push_back needs a free code unit, which the last chunk may have used.
+    if (__size_ == __capacity_)
+      __prepare_write(0);
----------------
16bit-ykiko wrote:

It doesn't allocate more. For `__allocating_buffer` (format, vformat, `__iterator_buffer`) the `__prepare_write(__n)` in the loop already makes room for `__n + 1`, so the buffer can't be full after the loop and the new branch is never taken. `__direct_iterator_buffer` never fills. Only `__container_inserter_buffer` gets there, and its `__prepare_write` inserts the 256 code units into the container, which the next write or `__out_it()` would do anyway.

I counted `operator new` calls with and without the patch for `format`, `format_to` into string/vector/deque/list and into a plain output iterator, `format_to_n` and `formatted_size`, for lengths 0–2100. The counts are identical, except in the cases where the unpatched code overflows.


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


More information about the libcxx-commits mailing list