aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/27_io/basic_istream
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2020-08-06 16:16:33 +0100
committerJonathan Wakely <jwakely@redhat.com>2020-08-06 18:26:45 +0100
commit6251ea15f55ec57d6325c2e37e88b22315aba658 (patch)
tree0f44c42a6daea85c951e58e781fb2a6c7f4bf37b /libstdc++-v3/testsuite/27_io/basic_istream
parent9c376d1c166e7c8b10bba6f1675d2471ffe8447f (diff)
downloadgcc-6251ea15f55ec57d6325c2e37e88b22315aba658.zip
gcc-6251ea15f55ec57d6325c2e37e88b22315aba658.tar.gz
gcc-6251ea15f55ec57d6325c2e37e88b22315aba658.tar.bz2
libstdc++: Adjust overflow prevention to operator>>
This adjusts the overflow prevention added to operator>> so that we can distinguish "unknown size" from "zero size", and avoid writing anything at all in to zero sized buffers. This also removes the incorrect comment saying extraction stops at a null byte. libstdc++-v3/ChangeLog: * include/std/istream (operator>>(istream&, char*)): Add attributes to get warnings for pointers that are null or known to point to the end of a buffer. Request upper bound from __builtin_object_size check and handle zero-sized buffer case. (operator>>(istream&, signed char)) (operator>>(istream&, unsigned char*)): Add attributes. * testsuite/27_io/basic_istream/extractors_character/char/overflow.cc: Check extracting into the middle of a buffer. * testsuite/27_io/basic_istream/extractors_character/wchar_t/overflow.cc: New test.
Diffstat (limited to 'libstdc++-v3/testsuite/27_io/basic_istream')
-rw-r--r--libstdc++-v3/testsuite/27_io/basic_istream/extractors_character/char/overflow.cc21
-rw-r--r--libstdc++-v3/testsuite/27_io/basic_istream/extractors_character/wchar_t/overflow.cc57
2 files changed, 75 insertions, 3 deletions
diff --git a/libstdc++-v3/testsuite/27_io/basic_istream/extractors_character/char/overflow.cc b/libstdc++-v3/testsuite/27_io/basic_istream/extractors_character/char/overflow.cc
index 1141a41..abbba8b 100644
--- a/libstdc++-v3/testsuite/27_io/basic_istream/extractors_character/char/overflow.cc
+++ b/libstdc++-v3/testsuite/27_io/basic_istream/extractors_character/char/overflow.cc
@@ -15,14 +15,14 @@
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
-// { dg-options "-O2 -std=gnu++98" }
+// { dg-options "-O2" }
// { dg-do run }
// This test checks that operator>> will avoid a buffer overflow when
// reading into a buffer with a size that is known at compile time.
// Since C++20 this is guaranteed (see LWG 2499), for previous standards
-// we try to check the buffer size as an extension (which depends on -O2).
+// checking the buffer size is an extension and depends on optimisation.
#include <sstream>
#include <testsuite_hooks.h>
@@ -30,11 +30,24 @@
void
test01()
{
- std::istringstream in("foolish child");
+ std::istringstream in("foolishly");
char pc[5];
in >> pc;
VERIFY( in.good() );
VERIFY( std::string(pc) == "fool" );
+
+#if __cplusplus <= 201703L
+ char* p = pc + 1;
+ in >> p;
+ VERIFY( in.good() );
+ VERIFY( std::string(pc) == "fish" );
+
+ p = pc + 4;
+ *p = '#';
+ in >> p;
+ VERIFY( in.fail() ); // if no characters are extracted, failbit is set
+ VERIFY( *p == '\0' );
+#endif
}
void
@@ -61,4 +74,6 @@ int
main()
{
test01();
+ test02();
+ test03();
}
diff --git a/libstdc++-v3/testsuite/27_io/basic_istream/extractors_character/wchar_t/overflow.cc b/libstdc++-v3/testsuite/27_io/basic_istream/extractors_character/wchar_t/overflow.cc
new file mode 100644
index 0000000..6a23f13
--- /dev/null
+++ b/libstdc++-v3/testsuite/27_io/basic_istream/extractors_character/wchar_t/overflow.cc
@@ -0,0 +1,57 @@
+// Copyright (C) 2020 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-O2" }
+// { dg-do run }
+
+// This test checks that operator>> will avoid a buffer overflow when
+// reading into a buffer with a size that is known at compile time.
+
+// Since C++20 this is guaranteed (see LWG 2499), for previous standards
+// checking the buffer size is an extension and depends on optimisation.
+
+#include <sstream>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ std::wistringstream in(L"foolishly");
+ wchar_t pc[5];
+ in >> pc;
+ VERIFY( in.good() );
+ VERIFY( std::wstring(pc) == L"fool" );
+
+#if __cplusplus <= 201703L
+ wchar_t* p = pc + 1;
+ in >> p;
+ VERIFY( in.good() );
+ VERIFY( std::wstring(pc) == L"fish" );
+
+ p = pc + 4;
+ *p = L'#';
+ in >> p;
+ VERIFY( in.fail() ); // if no characters are extracted, failbit is set
+ VERIFY( *p == L'\0' );
+#endif
+}
+
+int
+main()
+{
+ test01();
+}