|
3 | 3 |
|
4 | 4 | #define _SILENCE_CXX17_STRSTREAM_DEPRECATION_WARNING
|
5 | 5 |
|
| 6 | +#include <array> |
6 | 7 | #include <cassert>
|
7 | 8 | #include <fstream>
|
8 | 9 | #include <ios>
|
|
11 | 12 | #include <ostream>
|
12 | 13 | #include <sstream>
|
13 | 14 | #include <streambuf>
|
| 15 | +#include <string> |
14 | 16 | #include <strstream>
|
15 | 17 | #include <type_traits>
|
16 | 18 | #include <utility>
|
@@ -148,6 +150,255 @@ void test_istream_exceptions() {
|
148 | 150 | }
|
149 | 151 | }
|
150 | 152 |
|
| 153 | +template <class CharT> |
| 154 | +CharT meow_array; |
| 155 | +template <> |
| 156 | +constexpr array<char, 5> meow_array<char> = {"meow"}; |
| 157 | +template <> |
| 158 | +constexpr array<wchar_t, 5> meow_array<wchar_t> = {L"meow"}; |
| 159 | + |
| 160 | +// testing GH-5070: basic_istream::get[line](char_type* s, std::streamsize n, char_type delim) |
| 161 | +// do not null-terminate the output buffer correctly |
| 162 | +template <class CharT> |
| 163 | +void test_gh5070_istream_get_null_termination_under_exceptions() { |
| 164 | + throwing_buffer<CharT> buffer; |
| 165 | + const basic_string<CharT> stream_content(1U, meow_array<CharT>[2]); |
| 166 | + |
| 167 | + { // get, exception during input extraction, no exception rethrow |
| 168 | + basic_istream<CharT> is(buffer.to_buf()); |
| 169 | + auto buf = meow_array<CharT>; |
| 170 | + assert(!is.bad()); |
| 171 | + is.get(buf.data(), static_cast<streamsize>(buf.size())); |
| 172 | + assert(is.bad()); |
| 173 | + assert(buf[0] == CharT()); |
| 174 | + } |
| 175 | + |
| 176 | + { // get, exception during input extraction, exception rethrow enabled |
| 177 | + basic_istream<CharT> is(buffer.to_buf()); |
| 178 | + auto buf = meow_array<CharT>; |
| 179 | + is.exceptions(ios_base::badbit); |
| 180 | + assert(!is.bad()); |
| 181 | + try { |
| 182 | + is.get(buf.data(), static_cast<streamsize>(buf.size())); |
| 183 | + assert(false); |
| 184 | + } catch (const ios_base::failure&) { |
| 185 | + assert(false); |
| 186 | + } catch (const test_exception&) { |
| 187 | + // Expected case |
| 188 | + } |
| 189 | + assert(is.bad()); |
| 190 | + assert(buf[0] == CharT()); |
| 191 | + } |
| 192 | + |
| 193 | + { // get, empty output buffer, no exception raised |
| 194 | + basic_istream<CharT> is(buffer.to_buf()); |
| 195 | + auto buf = meow_array<CharT>; |
| 196 | + assert(!is.bad()); |
| 197 | + is.get(buf.data(), 0); |
| 198 | + assert(is.fail()); |
| 199 | + assert(buf[0] == meow_array<CharT>[0]); |
| 200 | + } |
| 201 | + |
| 202 | + { // get, empty output buffer, exception raised on failbit |
| 203 | + basic_istream<CharT> is(buffer.to_buf()); |
| 204 | + auto buf = meow_array<CharT>; |
| 205 | + is.exceptions(ios_base::failbit); |
| 206 | + assert(!is.bad()); |
| 207 | + try { |
| 208 | + is.get(buf.data(), 0); |
| 209 | + assert(false); |
| 210 | + } catch (const ios_base::failure&) { |
| 211 | + // Expected case |
| 212 | + } |
| 213 | + assert(is.fail()); |
| 214 | + assert(buf[0] == meow_array<CharT>[0]); |
| 215 | + } |
| 216 | + |
| 217 | + { // get, sentry construction fails, no exception raised |
| 218 | + basic_stringbuf<CharT> strbuf{stream_content}; |
| 219 | + basic_istream<CharT> is(&strbuf); |
| 220 | + assert(!is.bad()); |
| 221 | + |
| 222 | + // tests null termination on eof and |
| 223 | + // sets eofbit, preparing sentry failure |
| 224 | + auto buf1 = meow_array<CharT>; |
| 225 | + is.get(buf1.data(), static_cast<streamsize>(buf1.size())); |
| 226 | + assert(is.eof()); |
| 227 | + assert(!is.fail()); |
| 228 | + assert(buf1[0] == meow_array<CharT>[2]); |
| 229 | + assert(buf1[1] == CharT()); |
| 230 | + |
| 231 | + // actually tests sentry construction failure |
| 232 | + auto buf2 = meow_array<CharT>; |
| 233 | + is.get(buf2.data(), static_cast<streamsize>(buf2.size())); |
| 234 | + assert(is.fail()); |
| 235 | + assert(buf2[0] == CharT()); |
| 236 | + } |
| 237 | + |
| 238 | + { // get, sentry construction fails, exception raised on failbit |
| 239 | + basic_stringbuf<CharT> strbuf{stream_content}; |
| 240 | + basic_istream<CharT> is(&strbuf); |
| 241 | + is.exceptions(ios_base::failbit); |
| 242 | + assert(!is.bad()); |
| 243 | + |
| 244 | + // tests null termination on eof and |
| 245 | + // sets eofbit, preparing sentry failure |
| 246 | + auto buf1 = meow_array<CharT>; |
| 247 | + is.get(buf1.data(), static_cast<streamsize>(buf1.size())); |
| 248 | + assert(is.eof()); |
| 249 | + assert(!is.fail()); |
| 250 | + assert(buf1[0] == meow_array<CharT>[2]); |
| 251 | + assert(buf1[1] == CharT()); |
| 252 | + |
| 253 | + // actually tests sentry construction failure |
| 254 | + auto buf2 = meow_array<CharT>; |
| 255 | + try { |
| 256 | + is.get(buf2.data(), static_cast<streamsize>(buf2.size())); |
| 257 | + assert(false); |
| 258 | + } catch (const ios_base::failure&) { |
| 259 | + // Expected case |
| 260 | + } |
| 261 | + assert(is.fail()); |
| 262 | + assert(buf2[0] == CharT()); |
| 263 | + } |
| 264 | + |
| 265 | + { // get, exception raised on eofbit |
| 266 | + basic_stringbuf<CharT> strbuf{stream_content}; |
| 267 | + basic_istream<CharT> is(&strbuf); |
| 268 | + is.exceptions(ios_base::eofbit); |
| 269 | + assert(!is.bad()); |
| 270 | + |
| 271 | + auto buf = meow_array<CharT>; |
| 272 | + try { |
| 273 | + is.get(buf.data(), static_cast<streamsize>(buf.size())); |
| 274 | + assert(false); |
| 275 | + } catch (const ios_base::failure&) { |
| 276 | + // Expected case |
| 277 | + } |
| 278 | + assert(is.eof()); |
| 279 | + assert(!is.fail()); |
| 280 | + assert(buf[0] == meow_array<CharT>[2]); |
| 281 | + assert(buf[1] == CharT()); |
| 282 | + } |
| 283 | + |
| 284 | + { // getline, exception during input extraction, no exception rethrow |
| 285 | + basic_istream<CharT> is(buffer.to_buf()); |
| 286 | + auto buf = meow_array<CharT>; |
| 287 | + assert(!is.bad()); |
| 288 | + is.getline(buf.data(), static_cast<streamsize>(buf.size())); |
| 289 | + assert(is.bad()); |
| 290 | + assert(buf[0] == CharT()); |
| 291 | + } |
| 292 | + |
| 293 | + { // getline, exception during input extraction, exception rethrow enabled |
| 294 | + basic_istream<CharT> is(buffer.to_buf()); |
| 295 | + auto buf = meow_array<CharT>; |
| 296 | + is.exceptions(ios_base::badbit); |
| 297 | + assert(!is.bad()); |
| 298 | + try { |
| 299 | + is.getline(buf.data(), static_cast<streamsize>(buf.size())); |
| 300 | + assert(false); |
| 301 | + } catch (const ios_base::failure&) { |
| 302 | + assert(false); |
| 303 | + } catch (const test_exception&) { |
| 304 | + // Expected case |
| 305 | + } |
| 306 | + assert(is.bad()); |
| 307 | + assert(buf[0] == CharT()); |
| 308 | + } |
| 309 | + |
| 310 | + { // getline, empty output buffer, no exception raised |
| 311 | + basic_istream<CharT> is(buffer.to_buf()); |
| 312 | + auto buf = meow_array<CharT>; |
| 313 | + assert(!is.bad()); |
| 314 | + is.getline(buf.data(), 0); |
| 315 | + assert(is.fail()); |
| 316 | + assert(buf[0] == meow_array<CharT>[0]); |
| 317 | + } |
| 318 | + |
| 319 | + { // getline, empty output buffer, exception raised on failbit |
| 320 | + basic_istream<CharT> is(buffer.to_buf()); |
| 321 | + auto buf = meow_array<CharT>; |
| 322 | + is.exceptions(ios_base::failbit); |
| 323 | + assert(!is.bad()); |
| 324 | + try { |
| 325 | + is.getline(buf.data(), 0); |
| 326 | + assert(false); |
| 327 | + } catch (const ios_base::failure&) { |
| 328 | + // Expected case |
| 329 | + } |
| 330 | + assert(is.fail()); |
| 331 | + assert(buf[0] == meow_array<CharT>[0]); |
| 332 | + } |
| 333 | + |
| 334 | + { // getline, sentry construction fails, no exception raised |
| 335 | + basic_stringbuf<CharT> strbuf{stream_content}; |
| 336 | + basic_istream<CharT> is(&strbuf); |
| 337 | + assert(!is.bad()); |
| 338 | + |
| 339 | + // tests null termination on eof and |
| 340 | + // sets eofbit, preparing sentry failure |
| 341 | + auto buf1 = meow_array<CharT>; |
| 342 | + is.getline(buf1.data(), static_cast<streamsize>(buf1.size())); |
| 343 | + assert(is.eof()); |
| 344 | + assert(!is.fail()); |
| 345 | + assert(buf1[0] == meow_array<CharT>[2]); |
| 346 | + assert(buf1[1] == CharT()); |
| 347 | + |
| 348 | + // actually tests sentry construction failure |
| 349 | + auto buf2 = meow_array<CharT>; |
| 350 | + is.getline(buf2.data(), static_cast<streamsize>(buf2.size())); |
| 351 | + assert(is.fail()); |
| 352 | + assert(buf2[0] == CharT()); |
| 353 | + } |
| 354 | + |
| 355 | + { // getline, sentry construction fails, exception raised on failbit |
| 356 | + basic_stringbuf<CharT> strbuf{stream_content}; |
| 357 | + basic_istream<CharT> is(&strbuf); |
| 358 | + is.exceptions(ios_base::failbit); |
| 359 | + assert(!is.bad()); |
| 360 | + |
| 361 | + // tests null termination on eof and |
| 362 | + // sets eofbit, preparing sentry failure |
| 363 | + auto buf1 = meow_array<CharT>; |
| 364 | + is.getline(buf1.data(), static_cast<streamsize>(buf1.size())); |
| 365 | + assert(is.eof()); |
| 366 | + assert(!is.fail()); |
| 367 | + assert(buf1[0] == meow_array<CharT>[2]); |
| 368 | + assert(buf1[1] == CharT()); |
| 369 | + |
| 370 | + // actually tests sentry construction failure |
| 371 | + auto buf2 = meow_array<CharT>; |
| 372 | + try { |
| 373 | + is.getline(buf2.data(), static_cast<streamsize>(buf2.size())); |
| 374 | + assert(false); |
| 375 | + } catch (const ios_base::failure&) { |
| 376 | + // Expected case |
| 377 | + } |
| 378 | + assert(is.fail()); |
| 379 | + assert(buf2[0] == CharT()); |
| 380 | + } |
| 381 | + |
| 382 | + { // getline, exception raised on eofbit |
| 383 | + basic_stringbuf<CharT> strbuf{stream_content}; |
| 384 | + basic_istream<CharT> is(&strbuf); |
| 385 | + is.exceptions(ios_base::eofbit); |
| 386 | + assert(!is.bad()); |
| 387 | + |
| 388 | + auto buf = meow_array<CharT>; |
| 389 | + try { |
| 390 | + is.getline(buf.data(), static_cast<streamsize>(buf.size())); |
| 391 | + assert(false); |
| 392 | + } catch (const ios_base::failure&) { |
| 393 | + // Expected case |
| 394 | + } |
| 395 | + assert(is.eof()); |
| 396 | + assert(!is.fail()); |
| 397 | + assert(buf[0] == meow_array<CharT>[2]); |
| 398 | + assert(buf[1] == CharT()); |
| 399 | + } |
| 400 | +} |
| 401 | + |
151 | 402 | template <class CharT>
|
152 | 403 | void test_ostream_exceptions() {
|
153 | 404 | throwing_buffer<CharT> buffer;
|
@@ -481,6 +732,9 @@ int main() {
|
481 | 732 | test_istream_exceptions<char>();
|
482 | 733 | test_istream_exceptions<wchar_t>();
|
483 | 734 |
|
| 735 | + test_gh5070_istream_get_null_termination_under_exceptions<char>(); |
| 736 | + test_gh5070_istream_get_null_termination_under_exceptions<wchar_t>(); |
| 737 | + |
484 | 738 | test_ostream_exceptions<char>();
|
485 | 739 | test_ostream_exceptions<wchar_t>();
|
486 | 740 | }
|
0 commit comments