1 // Copyright (C) 2018-2019 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++11 } }
19 
20 #include <ios>
21 #include <climits>
22 #include <testsuite_hooks.h>
23 
24 // PR libstdc++/68197
25 
26 struct sbuf : std::streambuf { } sb;
27 
28 void
test01()29 test01()
30 {
31   std::ios ios(&sb);
32   long& i1 = ios.iword(-1);
33   VERIFY( i1 == 0 );
34   VERIFY( ios.bad() );
35   ios.clear();
36   i1 = 1;
37   VERIFY( ios.iword(-1) == 0 );
38   VERIFY( ios.bad() );
39   ios.clear();
40   long& i2 = ios.iword(INT_MIN);
41   VERIFY( i2 == 0 );
42   VERIFY( ios.bad() );
43   ios.clear();
44   i2 = 2;
45   VERIFY( ios.iword(INT_MIN) == 0 );
46   VERIFY( ios.bad() );
47   ios.clear();
48 
49   bool caught = false;
50   ios.exceptions(std::ios::badbit);
51   try {
52     ios.iword(-1);
53   } catch (const std::exception&) {
54     caught = true;
55   }
56   VERIFY( caught );
57 }
58 
59 void
test02()60 test02()
61 {
62   std::ios ios(&sb);
63   void*& p1 = ios.pword(-1);
64   VERIFY( p1 == nullptr );
65   VERIFY( ios.bad() );
66   ios.clear();
67   p1 = &p1;
68   VERIFY( ios.pword(-1) == nullptr );
69   VERIFY( ios.bad() );
70   ios.clear();
71   void*& p2 = ios.pword(INT_MIN);
72   VERIFY( p2 == nullptr );
73   VERIFY( ios.bad() );
74   ios.clear();
75   p2 = &p2;
76   VERIFY( ios.pword(INT_MIN) == nullptr );
77   VERIFY( ios.bad() );
78   ios.clear();
79 
80   bool caught = false;
81   ios.exceptions(std::ios::badbit);
82   try {
83     ios.pword(-1);
84   } catch (const std::exception&) {
85     caught = true;
86   }
87   VERIFY( caught );
88 }
89 
90 int
main()91 main()
92 {
93   test01();
94   test02();
95 }
96