diff options
author | Volodymyr Sapsai <vsapsai@apple.com> | 2017-11-22 18:52:36 +0000 |
---|---|---|
committer | Volodymyr Sapsai <vsapsai@apple.com> | 2017-11-22 18:52:36 +0000 |
commit | 2744cdf509ba305dfbd1bccd89fc72372522f9ce (patch) | |
tree | 4972bd8f5bb096282828de5023e9a5df0ccbb4b5 /test | |
parent | f27631034af5302e7124461296b4b647bd8fc88b (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.
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@318862 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test')
2 files changed, 108 insertions, 0 deletions
diff --git a/test/std/input.output/iostream.format/input.streams/istream.unformatted/getline_pointer_size.pass.cpp b/test/std/input.output/iostream.format/input.streams/istream.unformatted/getline_pointer_size.pass.cpp index 465824a65..2fce548da 100644 --- a/test/std/input.output/iostream.format/input.streams/istream.unformatted/getline_pointer_size.pass.cpp +++ b/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/test/std/input.output/iostream.format/input.streams/istream.unformatted/getline_pointer_size_chart.pass.cpp b/test/std/input.output/iostream.format/input.streams/istream.unformatted/getline_pointer_size_chart.pass.cpp index 736295996..4b9d554d2 100644 --- a/test/std/input.output/iostream.format/input.streams/istream.unformatted/getline_pointer_size_chart.pass.cpp +++ b/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 } |