[libc-commits] [libc] [libc] Make a stream unbuffered when setvbuf is given a size of zero (PR #236517)
Mike Lothian via libc-commits
libc-commits at lists.llvm.org
Fri Nov 6 04:56:37 PST 2026
https://github.com/FireBurn updated https://github.com/llvm/llvm-project/pull/236517
>From b8dac5569eb6985edeca1a926b03a9b7d15febef Mon Sep 17 00:00:00 2001
From: Mike Lothian <mike at fireburn.co.uk>
Date: Wed, 28 Oct 2026 17:22:50 +0000
Subject: [PATCH] [libc] Make a stream unbuffered when setvbuf is given a size
of zero
setvbuf(f, nullptr, _IOFBF, 0) and setlinebuf (which is
setvbuf(f, nullptr, _IOLBF, 0)) took the branch that stores the caller's
buffer, which is null, but kept the requested mode. The stream was then
fully or line buffered with no buffer. A buffer of no size holds nothing,
so set the mode to _IONBF to match.
Assisted-by: Claude
---
libc/src/__support/File/file.cpp | 5 ++
libc/test/src/stdio/CMakeLists.txt | 2 +
libc/test/src/stdio/setvbuf_test.cpp | 81 +++++++++++++++++++++++++++-
3 files changed, 87 insertions(+), 1 deletion(-)
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