| 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 { { ! lp64 } && { ! simulator } } } } |
| 19 | // { dg-timeout-factor 2 } |
| 20 | |
| 21 | #include <istream> |
| 22 | #include <streambuf> |
| 23 | #include <limits> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | typedef char C; |
| 27 | |
| 28 | struct buff : std::basic_streambuf<C> |
| 29 | { |
| 30 | typedef std::streamsize streamsize; |
| 31 | typedef std::numeric_limits<streamsize> limits; |
| 32 | |
| 33 | buff() : count(0), buf() { } |
| 34 | |
| 35 | int_type underflow() |
| 36 | { |
| 37 | // Number of characters left until we overflow the counter |
| 38 | const streamsize headroom = limits::max() - count; |
| 39 | |
| 40 | if (headroom == 0) |
| 41 | return traits_type::eof(); |
| 42 | |
| 43 | if (bufsz < headroom) |
| 44 | count += bufsz; |
| 45 | else |
| 46 | count = limits::max(); |
| 47 | |
| 48 | this->setg(buf + 1, buf + 1, buf + bufsz); |
| 49 | |
| 50 | return buf[0]; |
| 51 | } |
| 52 | |
| 53 | int_type overflow(int_type c) |
| 54 | { |
| 55 | if (traits_type::eq_int_type(c , traits_type::eof())) |
| 56 | return c; |
| 57 | this->setp(buf, buf + bufsz - 1); |
| 58 | return traits_type::not_eof(c); |
| 59 | } |
| 60 | |
| 61 | streamsize count; |
| 62 | |
| 63 | static const streamsize bufsz = (2048 << limits::digits10) + 1; |
| 64 | char_type buf[bufsz]; |
| 65 | }; |
| 66 | |
| 67 | void |
| 68 | test01() |
| 69 | { |
| 70 | // Not possible to overflow 64-bit streamsize in reasonable time. |
| 71 | if (std::numeric_limits<std::streamsize>::digits > 32) |
| 72 | return; |
| 73 | |
| 74 | std::basic_istream<C> in(new buff); |
| 75 | buff out; |
| 76 | in.get(out); |
| 77 | VERIFY( in.gcount() == std::numeric_limits<std::streamsize>::max() ); |
| 78 | |
| 79 | delete in.rdbuf(new buff); |
| 80 | |
| 81 | in.get(); |
| 82 | VERIFY( in.gcount() == 1 ); |
| 83 | |
| 84 | in.get(out, 'a'); |
| 85 | VERIFY( in.gcount() == std::numeric_limits<std::streamsize>::max() ); |
| 86 | } |
| 87 | |
| 88 | int |
| 89 | main() |
| 90 | { |
| 91 | test01(); |
| 92 | } |