[libcxx-commits] [libcxx] [libc++] Fix filebuf resetting its underlying buffer upon close() (PR #168947)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Fri Nov 21 03:22:47 PST 2025


================
@@ -0,0 +1,132 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// This test requires the fix to std::filebuf::close() (which is defined in the
+// built library) from https://github.com/llvm/llvm-project/pull/168947.
+// UNSUPPORTED: using-built-library-before-llvm-22
+
+// <fstream>
+
+// basic_filebuf<charT,traits>* close();
+
+//
+// Ensure that basic_filebuf::close() does not get rid of the underlying buffer set
+// via pubsetbuf(). Otherwise, reopening the stream will result in not reusing the
+// same buffer, which might be conforming but is definitely surprising. The standard
+// is not very clear on whether that is actually conforming.
+//
+
+#include <cassert>
+#include <fstream>
+#include <string>
+
+#include "platform_support.h"
+#include "test_macros.h"
+
+struct overflow_detecting_filebuf : std::filebuf {
+  explicit overflow_detecting_filebuf(bool* overflow_monitor) : did_overflow_(overflow_monitor) {
+    assert(overflow_monitor != nullptr && "must provide an overflow monitor");
+  }
+
+  using Traits = std::filebuf::traits_type;
+  virtual std::filebuf::int_type overflow(std::filebuf::int_type ch = Traits::eof()) {
+    *did_overflow_ = true;
+    return std::filebuf::overflow(ch);
+  }
+
+private:
+  bool* did_overflow_;
+};
+
+int main(int, char**) {
+  {
----------------
philnik777 wrote:

Why the extra scope?

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


More information about the libcxx-commits mailing list