diff options
author | Volodymyr Sapsai <vsapsai@apple.com> | 2017-11-22 21:52:36 +0300 |
---|---|---|
committer | Volodymyr Sapsai <vsapsai@apple.com> | 2017-11-22 21:52:36 +0300 |
commit | 2eb7f433a8d1fb1b0cd89db525195d2ff279497d (patch) | |
tree | 4e1782ea2fb629fc25444fbfb457284c80ec5990 /libcxx/test/std | |
parent | b02295641d14a0d1746bd8b3cbedfc0c2649bb28 (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')
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 } |