Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/llvm/llvm-project.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVolodymyr Sapsai <vsapsai@apple.com>2017-11-22 21:52:36 +0300
committerVolodymyr Sapsai <vsapsai@apple.com>2017-11-22 21:52:36 +0300
commit2eb7f433a8d1fb1b0cd89db525195d2ff279497d (patch)
tree4e1782ea2fb629fc25444fbfb457284c80ec5990 /libcxx/test/std
parentb02295641d14a0d1746bd8b3cbedfc0c2649bb28 (diff)
[libcxx] Make std::basic_istream::getline 0-terminate input array in case of error.
It covers the cases when the sentry object returns false and when an exception was thrown. Corresponding standard paragraph is C++14 [istream.unformatted]p21: In any case, if n is greater than zero, it then stores a null character (using charT()) into the next successive location of the array. Patch by Reimar Döffinger. llvm-svn: 318862
Diffstat (limited to 'libcxx/test/std')
-rw-r--r--libcxx/test/std/input.output/iostream.format/input.streams/istream.unformatted/getline_pointer_size.pass.cpp54
-rw-r--r--libcxx/test/std/input.output/iostream.format/input.streams/istream.unformatted/getline_pointer_size_chart.pass.cpp54
2 files changed, 108 insertions, 0 deletions
diff --git a/libcxx/test/std/input.output/iostream.format/input.streams/istream.unformatted/getline_pointer_size.pass.cpp b/libcxx/test/std/input.output/iostream.format/input.streams/istream.unformatted/getline_pointer_size.pass.cpp
index 465824a659f2..2fce548da098 100644
--- a/libcxx/test/std/input.output/iostream.format/input.streams/istream.unformatted/getline_pointer_size.pass.cpp
+++ b/libcxx/test/std/input.output/iostream.format/input.streams/istream.unformatted/getline_pointer_size.pass.cpp
@@ -14,6 +14,8 @@
#include <istream>
#include <cassert>
+#include "test_macros.h"
+
template <class CharT>
struct testbuf
: public std::basic_streambuf<CharT>
@@ -59,7 +61,33 @@ int main()
assert(!is.fail());
assert(std::string(s) == " ");
assert(is.gcount() == 1);
+ // Check that even in error case the buffer is properly 0-terminated.
+ is.getline(s, 5);
+ assert( is.eof());
+ assert( is.fail());
+ assert(std::string(s) == "");
+ assert(is.gcount() == 0);
+ }
+#ifndef TEST_HAS_NO_EXCEPTIONS
+ {
+ testbuf<char> sb(" ");
+ std::istream is(&sb);
+ char s[5] = "test";
+ is.exceptions(std::istream::eofbit | std::istream::badbit);
+ try
+ {
+ is.getline(s, 5);
+ assert(false);
+ }
+ catch (std::ios_base::failure&)
+ {
+ }
+ assert( is.eof());
+ assert( is.fail());
+ assert(std::string(s) == " ");
+ assert(is.gcount() == 1);
}
+#endif
{
testbuf<wchar_t> sb(L" \n \n ");
std::wistream is(&sb);
@@ -79,5 +107,31 @@ int main()
assert(!is.fail());
assert(std::wstring(s) == L" ");
assert(is.gcount() == 1);
+ // Check that even in error case the buffer is properly 0-terminated.
+ is.getline(s, 5);
+ assert( is.eof());
+ assert( is.fail());
+ assert(std::wstring(s) == L"");
+ assert(is.gcount() == 0);
+ }
+#ifndef TEST_HAS_NO_EXCEPTIONS
+ {
+ testbuf<wchar_t> sb(L" ");
+ std::wistream is(&sb);
+ wchar_t s[5] = L"test";
+ is.exceptions(std::wistream::eofbit | std::wistream::badbit);
+ try
+ {
+ is.getline(s, 5);
+ assert(false);
+ }
+ catch (std::ios_base::failure&)
+ {
+ }
+ assert( is.eof());
+ assert( is.fail());
+ assert(std::wstring(s) == L" ");
+ assert(is.gcount() == 1);
}
+#endif
}
diff --git a/libcxx/test/std/input.output/iostream.format/input.streams/istream.unformatted/getline_pointer_size_chart.pass.cpp b/libcxx/test/std/input.output/iostream.format/input.streams/istream.unformatted/getline_pointer_size_chart.pass.cpp
index 7362959966a6..4b9d554d2bb5 100644
--- a/libcxx/test/std/input.output/iostream.format/input.streams/istream.unformatted/getline_pointer_size_chart.pass.cpp
+++ b/libcxx/test/std/input.output/iostream.format/input.streams/istream.unformatted/getline_pointer_size_chart.pass.cpp
@@ -14,6 +14,8 @@
#include <istream>
#include <cassert>
+#include "test_macros.h"
+
template <class CharT>
struct testbuf
: public std::basic_streambuf<CharT>
@@ -59,7 +61,33 @@ int main()
assert(!is.fail());
assert(std::string(s) == " ");
assert(is.gcount() == 1);
+ // Check that even in error case the buffer is properly 0-terminated.
+ is.getline(s, 5, '*');
+ assert( is.eof());
+ assert( is.fail());
+ assert(std::string(s) == "");
+ assert(is.gcount() == 0);
+ }
+#ifndef TEST_HAS_NO_EXCEPTIONS
+ {
+ testbuf<char> sb(" ");
+ std::istream is(&sb);
+ char s[5] = "test";
+ is.exceptions(std::istream::eofbit | std::istream::badbit);
+ try
+ {
+ is.getline(s, 5, '*');
+ assert(false);
+ }
+ catch (std::ios_base::failure&)
+ {
+ }
+ assert( is.eof());
+ assert( is.fail());
+ assert(std::string(s) == " ");
+ assert(is.gcount() == 1);
}
+#endif
{
testbuf<wchar_t> sb(L" * * ");
std::wistream is(&sb);
@@ -79,5 +107,31 @@ int main()
assert(!is.fail());
assert(std::wstring(s) == L" ");
assert(is.gcount() == 1);
+ // Check that even in error case the buffer is properly 0-terminated.
+ is.getline(s, 5, L'*');
+ assert( is.eof());
+ assert( is.fail());
+ assert(std::wstring(s) == L"");
+ assert(is.gcount() == 0);
+ }
+#ifndef TEST_HAS_NO_EXCEPTIONS
+ {
+ testbuf<wchar_t> sb(L" ");
+ std::wistream is(&sb);
+ wchar_t s[5] = L"test";
+ is.exceptions(std::wistream::eofbit | std::wistream::badbit);
+ try
+ {
+ is.getline(s, 5, L'*');
+ assert(false);
+ }
+ catch (std::ios_base::failure&)
+ {
+ }
+ assert( is.eof());
+ assert( is.fail());
+ assert(std::wstring(s) == L" ");
+ assert(is.gcount() == 1);
}
+#endif
}