[libc++] Implement P0487R1 - Fixing operator>>(basic_istream&, CharT*)

Summary:
Avoid buffer overflow by replacing the pointer interface with an array reference interface in C++2a.
Tentatively ready on Batavia2018.

 https://wg21.link/lwg2499
 https://wg21.link/p0487

Reviewers: mclow.lists, ldionne, EricWF

Reviewed By: ldionne

Subscribers: libcxx-commits, cfe-commits, christof

Differential Revision: https://reviews.llvm.org/D51268

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@347377 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Zhihao Yuan
2018-11-21 03:30:10 +00:00
parent 739fdd4850
commit 580cde08e1
5 changed files with 127 additions and 9 deletions

View File

@@ -61,6 +61,17 @@ int main()
assert(std::string((char*)s) == "abc");
assert(is.width() == 0);
}
#if TEST_STD_VER > 17
{
testbuf<char> sb(" abcdefghijk ");
std::istream is(&sb);
signed char s[4];
is >> s;
assert(!is.eof());
assert(!is.fail());
assert(std::string((char*)s) == "abc");
}
#endif
{
testbuf<char> sb(" abcdefghijk");
std::istream is(&sb);
@@ -82,4 +93,15 @@ int main()
assert(std::string((char*)s) == "");
assert(is.width() == 0);
}
#if TEST_STD_VER > 17
{
testbuf<char> sb(" abcdefghijk");
std::istream is(&sb);
signed char s[1];
is >> s;
assert(!is.eof());
assert( is.fail());
assert(std::string((char*)s) == "");
}
#endif
}

View File

@@ -61,6 +61,17 @@ int main()
assert(std::string((char*)s) == "abc");
assert(is.width() == 0);
}
#if TEST_STD_VER > 17
{
testbuf<char> sb(" abcdefghijk ");
std::istream is(&sb);
unsigned char s[4];
is >> s;
assert(!is.eof());
assert(!is.fail());
assert(std::string((char*)s) == "abc");
}
#endif
{
testbuf<char> sb(" abcdefghijk");
std::istream is(&sb);
@@ -82,4 +93,15 @@ int main()
assert(std::string((char*)s) == "");
assert(is.width() == 0);
}
#if TEST_STD_VER > 17
{
testbuf<char> sb(" abcdefghijk");
std::istream is(&sb);
unsigned char s[1];
is >> s;
assert(!is.eof());
assert( is.fail());
assert(std::string((char*)s) == "");
}
#endif
}

View File

@@ -50,6 +50,17 @@ int main()
assert(!is.fail());
assert(std::string(s) == "abcdefghijk");
}
#if TEST_STD_VER > 17
{
testbuf<char> sb(" abcdefghijk ");
std::istream is(&sb);
char s[4];
is >> s;
assert(!is.eof());
assert(!is.fail());
assert(std::string(s) == "abc");
}
#endif
{
testbuf<wchar_t> sb(L" abcdefghijk ");
std::wistream is(&sb);
@@ -71,6 +82,17 @@ int main()
assert(std::wstring(s) == L"abcdefghijk");
assert(is.width() == 0);
}
#if TEST_STD_VER > 17
{
testbuf<wchar_t> sb(L" abcdefghijk");
std::wistream is(&sb);
wchar_t s[4];
is >> s;
assert(!is.eof());
assert(!is.fail());
assert(std::wstring(s) == L"abc");
}
#endif
{
testbuf<char> sb(" abcdefghijk");
std::istream is(&sb);
@@ -82,4 +104,15 @@ int main()
assert(std::string(s) == "");
assert(is.width() == 0);
}
#if TEST_STD_VER > 17
{
testbuf<char> sb(" abcdefghijk");
std::istream is(&sb);
char s[1];
is >> s;
assert(!is.eof());
assert( is.fail());
assert(std::string(s) == "");
}
#endif
}