[libc-commits] [libc] d215b74 - [libc] Make a stream unbuffered when setvbuf is given a size of zero (#236517)
via libc-commits
libc-commits at lists.llvm.org
Sun Nov 8 18:13:03 PST 2026
Author: Mike Lothian
Date: 2026-11-08T18:12:58-08:00
New Revision: d215b7427e5482dbaef1c35ccf5c364e802f7f77
URL: https://github.com/llvm/llvm-project/commit/d215b7427e5482dbaef1c35ccf5c364e802f7f77
DIFF: https://github.com/llvm/llvm-project/commit/d215b7427e5482dbaef1c35ccf5c364e802f7f77.diff
LOG: [libc] Make a stream unbuffered when setvbuf is given a size of zero (#236517)
`setvbuf(f, nullptr, _IOFBF, 0)` and `setlinebuf(f)`, which is
`setvbuf(f, nullptr, _IOLBF, 0)`, take the branch that stores the
caller's buffer. That buffer is null, but the requested mode is kept, so
the stream is left fully or line buffered with no buffer. A buffer of no
size holds nothing, so this sets the mode to `_IONBF` to match.
Where it came from: with my libc `setlinebuf(stdout)` followed by
another write crashed `bluetoothctl --help` when stdout was a pipe,
through a null buffer in a line buffered stream. I could not reproduce
that crash in a unit test on current main, so the tests added here pass
with and without the change. They pin the behaviour a program can see: a
zero sized stream shows its writes without a flush, and a sized one
still waits for `fflush`. Nothing public reports the buffering mode, so
I can't test the mode directly.
AI disclosure: this patch was written with Claude Code. I reviewed the
change and ran the new tests.
Assisted-by: Claude
Added:
Modified:
libc/src/__support/File/file.cpp
libc/test/src/stdio/CMakeLists.txt
libc/test/src/stdio/setvbuf_test.cpp
Removed:
################################################################################
diff --git a/libc/src/__support/File/file.cpp b/libc/src/__support/File/file.cpp
index f8ff4a6e52f2ca..db998a3e5f12b1 100644
--- a/libc/src/__support/File/file.cpp
+++ b/libc/src/__support/File/file.cpp
@@ -546,6 +546,11 @@ int File::set_buffer(void *buffer, size_t size, int buffer_mode) {
return EINVAL;
}
+ // A buffer of no size cannot hold anything, so the stream is unbuffered
+ // whatever mode was asked for.
+ if (size == 0)
+ buffer_mode = _IONBF;
+
if (buffer == nullptr && size != 0 && buffer_mode != _IONBF) {
// We exclude the case of buffer_mode == _IONBF in this branch
// because we don't need to allocate buffer in such a case.
diff --git a/libc/test/src/stdio/CMakeLists.txt b/libc/test/src/stdio/CMakeLists.txt
index c1c3bcd776c3a2..663b87c5e690a9 100644
--- a/libc/test/src/stdio/CMakeLists.txt
+++ b/libc/test/src/stdio/CMakeLists.txt
@@ -111,7 +111,9 @@ add_libc_test(
setvbuf_test.cpp
DEPENDS
libc.include.stdio
+ libc.src.stdio.clearerr
libc.src.stdio.fclose
+ libc.src.stdio.fflush
libc.src.stdio.fopen
libc.src.stdio.fread
libc.src.stdio.fwrite
diff --git a/libc/test/src/stdio/setvbuf_test.cpp b/libc/test/src/stdio/setvbuf_test.cpp
index a0936ba79ef73d..fb66d4decd2bb8 100644
--- a/libc/test/src/stdio/setvbuf_test.cpp
+++ b/libc/test/src/stdio/setvbuf_test.cpp
@@ -1,12 +1,19 @@
-//===-- Unittests for setvbuf ---------------------------------------------===//
+//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for setvbuf.
+///
+//===----------------------------------------------------------------------===//
+#include "src/stdio/clearerr.h"
#include "src/stdio/fclose.h"
+#include "src/stdio/fflush.h"
#include "src/stdio/fopen.h"
#include "src/stdio/fread.h"
#include "src/stdio/fwrite.h"
@@ -95,6 +102,78 @@ TEST_F(LlvmLibcSetvbufTest, SetLBFBuffer) {
ASSERT_EQ(0, LIBC_NAMESPACE::fclose(fr));
}
+TEST_F(LlvmLibcSetvbufTest, NullBufferOfNoSizeIsUnbuffered) {
+ // A buffer of no size cannot hold anything, so the stream is unbuffered
+ // whatever mode was asked for, and what is written is readable through
+ // another handle without a flush.
+ constexpr char FILENAME[] = "testdata/setvbuf_null_buffer_no_size.test";
+ constexpr char CONTENT[] = "abcdef";
+ constexpr size_t CONTENT_SIZE = sizeof(CONTENT) - 1;
+
+ ::FILE *fw = LIBC_NAMESPACE::fopen(FILENAME, "w");
+ ASSERT_FALSE(fw == nullptr);
+ ASSERT_EQ(LIBC_NAMESPACE::setvbuf(fw, nullptr, _IOFBF, 0), 0);
+
+ ::FILE *fr = LIBC_NAMESPACE::fopen(FILENAME, "r");
+ ASSERT_FALSE(fr == nullptr);
+
+ ASSERT_EQ(CONTENT_SIZE, LIBC_NAMESPACE::fwrite(CONTENT, 1, CONTENT_SIZE, fw));
+ char data[CONTENT_SIZE + 1] = {};
+ ASSERT_EQ(CONTENT_SIZE, LIBC_NAMESPACE::fread(data, 1, CONTENT_SIZE, fr));
+ ASSERT_STREQ(CONTENT, data);
+
+ ASSERT_EQ(0, LIBC_NAMESPACE::fclose(fw));
+ ASSERT_EQ(0, LIBC_NAMESPACE::fclose(fr));
+}
+
+TEST_F(LlvmLibcSetvbufTest, LineBufferedNullBufferOfNoSizeIsUnbuffered) {
+ constexpr char FILENAME[] = "testdata/setvbuf_null_buffer_no_size_lbf.test";
+ constexpr char CONTENT[] = "abcdef";
+ constexpr size_t CONTENT_SIZE = sizeof(CONTENT) - 1;
+
+ ::FILE *fw = LIBC_NAMESPACE::fopen(FILENAME, "w");
+ ASSERT_FALSE(fw == nullptr);
+ ASSERT_EQ(LIBC_NAMESPACE::setvbuf(fw, nullptr, _IOLBF, 0), 0);
+
+ ::FILE *fr = LIBC_NAMESPACE::fopen(FILENAME, "r");
+ ASSERT_FALSE(fr == nullptr);
+
+ // No newline is written, so a line buffered stream would still be holding
+ // the data.
+ ASSERT_EQ(CONTENT_SIZE, LIBC_NAMESPACE::fwrite(CONTENT, 1, CONTENT_SIZE, fw));
+ char data[CONTENT_SIZE + 1] = {};
+ ASSERT_EQ(CONTENT_SIZE, LIBC_NAMESPACE::fread(data, 1, CONTENT_SIZE, fr));
+ ASSERT_STREQ(CONTENT, data);
+
+ ASSERT_EQ(0, LIBC_NAMESPACE::fclose(fw));
+ ASSERT_EQ(0, LIBC_NAMESPACE::fclose(fr));
+}
+
+TEST_F(LlvmLibcSetvbufTest, NullBufferWithASizeStaysBuffered) {
+ constexpr char FILENAME[] = "testdata/setvbuf_null_buffer_sized.test";
+ constexpr char CONTENT[] = "abcdef";
+ constexpr size_t CONTENT_SIZE = sizeof(CONTENT) - 1;
+
+ ::FILE *fw = LIBC_NAMESPACE::fopen(FILENAME, "w");
+ ASSERT_FALSE(fw == nullptr);
+ ASSERT_EQ(LIBC_NAMESPACE::setvbuf(fw, nullptr, _IOFBF, 16), 0);
+
+ ::FILE *fr = LIBC_NAMESPACE::fopen(FILENAME, "r");
+ ASSERT_FALSE(fr == nullptr);
+
+ ASSERT_EQ(CONTENT_SIZE, LIBC_NAMESPACE::fwrite(CONTENT, 1, CONTENT_SIZE, fw));
+ char data[CONTENT_SIZE + 1] = {};
+ ASSERT_EQ(size_t(0), LIBC_NAMESPACE::fread(data, 1, CONTENT_SIZE, fr));
+
+ ASSERT_EQ(0, LIBC_NAMESPACE::fflush(fw));
+ LIBC_NAMESPACE::clearerr(fr);
+ ASSERT_EQ(CONTENT_SIZE, LIBC_NAMESPACE::fread(data, 1, CONTENT_SIZE, fr));
+ ASSERT_STREQ(CONTENT, data);
+
+ ASSERT_EQ(0, LIBC_NAMESPACE::fclose(fw));
+ ASSERT_EQ(0, LIBC_NAMESPACE::fclose(fr));
+}
+
TEST(LlvmLibcSetbufTest, InvalidBufferMode) {
constexpr char FILENAME[] = "testdata/setvbuf_invalid_bufmode.test";
::FILE *f = LIBC_NAMESPACE::fopen(FILENAME, "w");
More information about the libc-commits
mailing list