Skip to content

Commit 062fe21

Browse files
committed
[libc++] Fixes istream::sync.
This fixes two issues. The return value ---------------- Based on the wording [istream.unformatted]/37 Effects: Behaves as an unformatted input function (as described above), except that it does not count the number of characters extracted and does not affect the value returned by subsequent calls to gcount(). After constructing a sentry object, if rdbuf() is a null pointer, returns -1. [istream.unformatted]/1 ... It then creates an object of class sentry with the default argument noskipws (second) argument true. If the sentry object returns true, when converted to a value of type bool, the function endeavors to obtain the requested input. ... It could be argued the current behaviour is correct, however constructing a istream rdbuf() == nullptr creates a sentry that returns false; its state is always bad in this case. As mentioned in the bug report, after this change the 3 major implementations behave the same. The setting of the state ------------------------ When pubsync returned -1 it updated the local __state variable and returned. This early return caused the state up the istream not to be updated to the new state. Fixes: #51497 Fixes: #51499
1 parent 8cf6bcf commit 062fe21

File tree

2 files changed

+79
-5
lines changed
  • libcxx

2 files changed

+79
-5
lines changed

libcxx/include/istream

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,17 +1009,18 @@ basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::unget() {
10091009
template <class _CharT, class _Traits>
10101010
int basic_istream<_CharT, _Traits>::sync() {
10111011
ios_base::iostate __state = ios_base::goodbit;
1012-
int __r = 0;
10131012
sentry __sen(*this, true);
1013+
if (this->rdbuf() == nullptr)
1014+
return -1;
1015+
1016+
int __r = 0;
10141017
if (__sen) {
10151018
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
10161019
try {
10171020
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1018-
if (this->rdbuf() == nullptr)
1019-
return -1;
10201021
if (this->rdbuf()->pubsync() == -1) {
10211022
__state |= ios_base::badbit;
1022-
return -1;
1023+
__r = -1;
10231024
}
10241025
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
10251026
} catch (...) {

libcxx/test/std/input.output/iostream.format/input.streams/istream.unformatted/sync.pass.cpp

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
// int sync();
1212

13+
// The fix for bug 51497 and bug 51499 require and updated dylib due to
14+
// explicit instantiations. That means Apple backdeployment targets remain
15+
// broken.
16+
// UNSUPPORTED: stdlib=apple-libc++ && target={{.+}}-apple-macosx1{{0|1|2}}
17+
1318
#include <istream>
1419
#include <cassert>
1520

@@ -48,6 +53,18 @@ struct testbuf
4853
}
4954
};
5055

56+
template <class CharT>
57+
struct testbuf_pubsync_error
58+
: public std::basic_streambuf<CharT>
59+
{
60+
public:
61+
62+
testbuf_pubsync_error() {}
63+
protected:
64+
virtual int sync() { return -1; }
65+
};
66+
67+
5168
#ifndef TEST_HAS_NO_EXCEPTIONS
5269
struct testbuf_exception { };
5370

@@ -85,21 +102,62 @@ struct throwing_testbuf
85102

86103
int main(int, char**)
87104
{
105+
{
106+
std::istream is(nullptr);
107+
assert(is.sync() == -1);
108+
}
88109
{
89110
testbuf<char> sb(" 123456789");
90111
std::istream is(&sb);
91112
assert(is.sync() == 0);
92113
assert(sync_called == 1);
93114
}
115+
{
116+
testbuf_pubsync_error<char> sb;
117+
std::istream is(&sb);
118+
is.exceptions(std::ios_base::failbit | std::ios_base::eofbit);
119+
assert(is.sync() == -1);
120+
assert( is.bad());
121+
assert(!is.eof());
122+
assert( is.fail());
123+
}
94124
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
125+
{
126+
std::wistream is(nullptr);
127+
assert(is.sync() == -1);
128+
}
95129
{
96130
testbuf<wchar_t> sb(L" 123456789");
97131
std::wistream is(&sb);
98132
assert(is.sync() == 0);
99133
assert(sync_called == 2);
100134
}
135+
{
136+
testbuf_pubsync_error<wchar_t> sb;
137+
std::wistream is(&sb);
138+
is.exceptions(std::ios_base::failbit | std::ios_base::eofbit);
139+
assert(is.sync() == -1);
140+
assert( is.bad());
141+
assert(!is.eof());
142+
assert( is.fail());
143+
}
101144
#endif
102145
#ifndef TEST_HAS_NO_EXCEPTIONS
146+
{
147+
testbuf_pubsync_error<char> sb;
148+
std::istream is(&sb);
149+
is.exceptions(std::ios_base::badbit);
150+
bool threw = false;
151+
try {
152+
is.sync();
153+
} catch (std::ios_base::failure const&) {
154+
threw = true;
155+
}
156+
assert( is.bad());
157+
assert(!is.eof());
158+
assert( is.fail());
159+
assert(threw);
160+
}
103161
{
104162
throwing_testbuf<char> sb(" 123456789");
105163
std::basic_istream<char> is(&sb);
@@ -116,6 +174,21 @@ int main(int, char**)
116174
assert(threw);
117175
}
118176
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
177+
{
178+
testbuf_pubsync_error<wchar_t> sb;
179+
std::wistream is(&sb);
180+
is.exceptions(std::ios_base::badbit);
181+
bool threw = false;
182+
try {
183+
is.sync();
184+
} catch (std::ios_base::failure const&) {
185+
threw = true;
186+
}
187+
assert( is.bad());
188+
assert(!is.eof());
189+
assert( is.fail());
190+
assert(threw);
191+
}
119192
{
120193
throwing_testbuf<wchar_t> sb(L" 123456789");
121194
std::basic_istream<wchar_t> is(&sb);
@@ -131,7 +204,7 @@ int main(int, char**)
131204
assert( is.fail());
132205
assert(threw);
133206
}
134-
#endif
207+
#endif // TEST_HAS_NO_WIDE_CHARACTERS
135208
#endif // TEST_HAS_NO_EXCEPTIONS
136209

137210
return 0;

0 commit comments

Comments
 (0)