[libcxx-commits] [libcxx] [libcxx] Fix xsgetn in basic_filebuf (PR #167779)
Michael Jones via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Nov 12 14:51:10 PST 2025
https://github.com/michaelrj-google created https://github.com/llvm/llvm-project/pull/167779
The optimized version of xsgetn for basic_filebuf added in #165223 has
an issue where if the reads come from both the buffer and the
filesystem it returns the wrong number of characters. This patch should
address the issue.
>From 916bdde163142a9d88d4ff352bc8503ca535d794 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Wed, 12 Nov 2025 22:46:29 +0000
Subject: [PATCH] [libcxx] Fix xsgetn in basic_filebuf
The optimized version of xsgetn for basic_filebuf added in #165223 has
an issue where if the reads come from both the buffer and the
filesystem it returns the wrong number of characters. This patch should
address the issue.
---
libcxx/include/fstream | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/libcxx/include/fstream b/libcxx/include/fstream
index b07ca636094af..ef670728adec4 100644
--- a/libcxx/include/fstream
+++ b/libcxx/include/fstream
@@ -310,13 +310,19 @@ protected:
_LIBCPP_HIDE_FROM_ABI_VIRTUAL streamsize xsgetn(char_type* __str, streamsize __len) override {
if (__always_noconv_) {
- const streamsize __n = std::min(this->egptr() - this->gptr(), __len);
+ const streamsize __n = std::min( this->egptr() - this->gptr(), __len);
if (__n != 0) {
traits_type::copy(__str, this->gptr(), __n);
this->__gbump_ptrdiff(__n);
}
- if (__len - __n >= this->egptr() - this->eback())
- return std::fread(__str + __n, sizeof(char_type), __len - __n, __file_);
+ const streamsize __remainder = __len - __n;
+ const streamsize __buffer_space = this->egptr() - this->eback();
+
+ if (__remainder >= __buffer_space)
+ return std::fread(__str + __n, sizeof(char_type), __remainder, __file_) + __n;
+ else if( __remainder > 0)
+ return basic_streambuf<_CharT, _Traits>::xsgetn(__str + __n, __remainder) + __n;
+ return __n;
}
return basic_streambuf<_CharT, _Traits>::xsgetn(__str, __len);
}
More information about the libcxx-commits
mailing list