Fix various undefined behavior found by UBSan.
* Fix non-null violation in strstream.cpp
Overflow was calling memcpy with a null parameter and a size of 0.
* Fix std/atomics/atomics.flag/ tests:
a.test_and_set() was reading from an uninitialized atomic, but wasn't
using the value. The tests now clear the flag before performing the
first test_and_set. This allows UBSAN to test that clear doesn't read
an invalid value.
* Fix std/experimental/algorithms/alg.random.sample/sample.pass.cpp
The tests were dereferencing a past-the-end pointer to an array so that
they could do pointer arithmetic with it. Instead of dereference the iterator
I changed the tests to use the special 'base()' test iterator method.
* Add -fno-sanitize=float-divide-by-zero to suppress division by zero UBSAN diagnostics.
The tests that cause float division by zero are explicitly aware that they
are doing that. Since this is well defined for IEEE floats suppress the warnings
for now.
llvm-svn: 273107
Cr-Mirrored-From: sso://chromium.googlesource.com/_direct/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: 990952b6644e79da08c6925bddedff7c3b3ebe8a
diff --git a/src/strstream.cpp b/src/strstream.cpp
index ea72813..83702fc 100644
--- a/src/strstream.cpp
+++ b/src/strstream.cpp
@@ -11,6 +11,7 @@
#include "algorithm"
#include "climits"
#include "cstring"
+#include "__debug"
_LIBCPP_BEGIN_NAMESPACE_STD
@@ -167,7 +168,10 @@
buf = new char[new_size];
if (buf == nullptr)
return int_type(EOF);
- memcpy(buf, eback(), static_cast<size_t>(old_size));
+ if (old_size != 0) {
+ _LIBCPP_ASSERT(eback(), "overflow copying from NULL");
+ memcpy(buf, eback(), static_cast<size_t>(old_size));
+ }
ptrdiff_t ninp = gptr() - eback();
ptrdiff_t einp = egptr() - eback();
ptrdiff_t nout = pptr() - pbase();