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