Skip to content

Conversation

@ldionne
Copy link
Member

@ldionne ldionne commented Nov 20, 2025

It's a bit awkward to pass the char* and the size to the function, which then only decides whether it should be a no-op with it. Instead, only call the function when the pointer is null and n is 0, which matches more closely what the standard says with respect to calling pubsetbuf(0, 0).

It's a bit awkward to pass the char* and the size to the function,
which then only decides whether it should be a no-op with it. Instead,
only call the function when the pointer is null and n is 0, which
matches more closely what the standard says with respect to calling
pubsetbuf(0, 0).
@ldionne ldionne requested a review from a team as a code owner November 20, 2025 21:28
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Nov 20, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 20, 2025

@llvm/pr-subscribers-libcxx

Author: Louis Dionne (ldionne)

Changes

It's a bit awkward to pass the char* and the size to the function, which then only decides whether it should be a no-op with it. Instead, only call the function when the pointer is null and n is 0, which matches more closely what the standard says with respect to calling pubsetbuf(0, 0).


Full diff: https://github.com/llvm/llvm-project/pull/168950.diff

1 Files Affected:

  • (modified) libcxx/include/fstream (+9-9)
diff --git a/libcxx/include/fstream b/libcxx/include/fstream
index 90e35740c17cf..2f0e8688e7d83 100644
--- a/libcxx/include/fstream
+++ b/libcxx/include/fstream
@@ -419,14 +419,12 @@ private:
   // If the file is already open, switch to unbuffered mode. Otherwise, record
   // the request to use unbuffered mode so that we use that mode when we
   // eventually open the file.
-  _LIBCPP_HIDE_FROM_ABI void __request_unbuffered_mode(char_type* __s, streamsize __n) {
-    if (__cm_ == __no_io_operations && __s == nullptr && __n == 0) {
-      if (__file_) {
-        std::setbuf(__file_, nullptr);
-        __cm_ = 0;
-      } else {
-        __cm_ = __no_io_operations | __use_unbuffered_io;
-      }
+  _LIBCPP_HIDE_FROM_ABI void __request_unbuffered_mode() {
+    if (__file_) {
+      std::setbuf(__file_, nullptr);
+      __cm_ = 0;
+    } else {
+      __cm_ = __no_io_operations | __use_unbuffered_io;
     }
   }
 
@@ -934,7 +932,9 @@ template <class _CharT, class _Traits>
 basic_streambuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n) {
   this->setg(nullptr, nullptr, nullptr);
   this->setp(nullptr, nullptr);
-  __request_unbuffered_mode(__s, __n);
+  // Calling setbuf(nullptr, 0) before any i/o operation switches the stream to unbuffered mode
+  if (__cm_ == __no_io_operations && __s == nullptr && __n == 0)
+    __request_unbuffered_mode();
   if (__owns_eb_)
     delete[] __extbuf_;
   if (__owns_ib_)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants