| 1 | // Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do run { target c++20 } } |
| 19 | |
| 20 | // LWG 2499 |
| 21 | // operator>>(basic_istream&, CharT*) makes it hard to avoid buffer overflows |
| 22 | |
| 23 | #include <sstream> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | template<typename T> |
| 27 | void |
| 28 | test(std::basic_istream<wchar_t, T>& in) |
| 29 | { |
| 30 | wchar_t wc[3]; |
| 31 | in >> wc; |
| 32 | VERIFY( in.good() ); |
| 33 | VERIFY( wc[0] == L'a' && wc[1] == L'b' && wc[2] == L'\0' ); |
| 34 | |
| 35 | wc[2] = L'#'; |
| 36 | in >> wc; |
| 37 | VERIFY( in.good() ); |
| 38 | VERIFY( wc[0] == L'c' && wc[1] == L'\0' && wc[2] == L'#' ); |
| 39 | |
| 40 | in >> wc; |
| 41 | VERIFY( in.good() ); |
| 42 | VERIFY( wc[0] == L'd' && wc[1] == L'\0' && wc[2] == L'#' ); |
| 43 | |
| 44 | wc[2] = L'#'; |
| 45 | in >> wc; |
| 46 | VERIFY( in.eof() ); |
| 47 | VERIFY( wc[0] == L'e' && wc[1] == L'\0' && wc[2] == L'#' ); |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | test01() |
| 52 | { |
| 53 | std::wistringstream in(L"abc d e"); |
| 54 | test(in); |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | test02() |
| 59 | { |
| 60 | struct WT : std::char_traits<wchar_t> { }; |
| 61 | std::basic_istringstream<wchar_t, WT> in(L"abc d e"); |
| 62 | test(in); |
| 63 | } |
| 64 | |
| 65 | int main() |
| 66 | { |
| 67 | test01(); |
| 68 | test02(); |
| 69 | } |