summaryrefslogtreecommitdiff
path: root/absl/types
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2020-03-23 13:16:18 -0700
committerGravatar Xiaoyi Zhang <zhangxy@google.com>2020-03-23 16:24:45 -0400
commit518f17501e6156f7921fbb9b68a1e420bcb10bc5 (patch)
tree615cb6be894145feaa79ff3e341f2d483022f336 /absl/types
parent092ed9793a1ad0e7e418f32c057bf3159a71cd04 (diff)
Export of internal Abseil changes
-- 79913a12f0cad4baf948430315aabf53f03b6475 by Abseil Team <absl-team@google.com>: Don't inline (Un)LockSlow. PiperOrigin-RevId: 302502344 -- 6b340e80f0690655f24799c8de6707b3a95b8579 by Derek Mauro <dmauro@google.com>: Add hardening assertions to absl::optional's dereference operators PiperOrigin-RevId: 302492862 -- a9951bf4852d8c1aec472cb4b539830411270e4c by Derek Mauro <dmauro@google.com>: Correctly add hardware AES compiler flags under Linux X86-64 Fixes #643 PiperOrigin-RevId: 302490673 -- 314c3621ee4d57b6bc8d64338a1f1d48a69741d1 by Derek Mauro <dmauro@google.com>: Upgrade to hardening assertions in absl::Span::remove_prefix and absl::Span::remove_suffix PiperOrigin-RevId: 302481191 -- a142b8c6c62705c5f0d4fe3113150f0c0b7822b9 by Derek Mauro <dmauro@google.com>: Update docker containers to Bazel 2.2.0, GCC 9.3, and new Clang snapshot PiperOrigin-RevId: 302454042 -- afedeb70a2adc87010030c9ba6f06fe35ec26407 by Derek Mauro <dmauro@google.com>: Add hardening assertions for the preconditions of absl::FixedArray PiperOrigin-RevId: 302441767 -- 44442bfbc0a9a742df32f07cee86a47712efb8b4 by Derek Mauro <dmauro@google.com>: Fix new Clang warning about SpinLock doing operations on enums of different types PiperOrigin-RevId: 302430387 -- 69eaff7f97231779f696321c2ba8b88debf6dd9e by Derek Mauro <dmauro@google.com>: Convert precondition assertions to ABSL_HARDENING_ASSERT for absl::InlinedVector PiperOrigin-RevId: 302427894 -- 26b6db906a0942fd18583dc2cdd1bab32919d964 by Gennadiy Rozental <rogeeff@google.com>: Internal change PiperOrigin-RevId: 302425283 -- e62e81422979e922505d2cd9000e1de58123c088 by Derek Mauro <dmauro@google.com>: Add an option to build Abseil in hardened mode In hardened mode, the ABSL_HARDENING_ASSERT() macro is active even when NDEBUG is defined. This allows Abseil to perform runtime checks even in release mode. This should be used to implement things like bounds checks that could otherwise lead to security vulnerabilities. Use the new assertion in absl::string_view and absl::Span to test it. PiperOrigin-RevId: 302119187 GitOrigin-RevId: 79913a12f0cad4baf948430315aabf53f03b6475 Change-Id: I0cc3341fd333a1df313167bab72dc5a759c4a048
Diffstat (limited to 'absl/types')
-rw-r--r--absl/types/optional.h12
-rw-r--r--absl/types/optional_test.cc6
-rw-r--r--absl/types/span.h14
-rw-r--r--absl/types/span_test.cc10
4 files changed, 24 insertions, 18 deletions
diff --git a/absl/types/optional.h b/absl/types/optional.h
index afda69a0..61540cfd 100644
--- a/absl/types/optional.h
+++ b/absl/types/optional.h
@@ -412,11 +412,11 @@ class optional : private optional_internal::optional_data<T>,
//
// If you need myOpt->foo in constexpr, use (*myOpt).foo instead.
const T* operator->() const {
- assert(this->engaged_);
+ ABSL_HARDENING_ASSERT(this->engaged_);
return std::addressof(this->data_);
}
T* operator->() {
- assert(this->engaged_);
+ ABSL_HARDENING_ASSERT(this->engaged_);
return std::addressof(this->data_);
}
@@ -425,17 +425,17 @@ class optional : private optional_internal::optional_data<T>,
// Accesses the underlying `T` value of an `optional`. If the `optional` is
// empty, behavior is undefined.
constexpr const T& operator*() const& {
- return ABSL_ASSERT(this->engaged_), reference();
+ return ABSL_HARDENING_ASSERT(this->engaged_), reference();
}
T& operator*() & {
- assert(this->engaged_);
+ ABSL_HARDENING_ASSERT(this->engaged_);
return reference();
}
constexpr const T&& operator*() const && {
- return absl::move(reference());
+ return ABSL_HARDENING_ASSERT(this->engaged_), absl::move(reference());
}
T&& operator*() && {
- assert(this->engaged_);
+ ABSL_HARDENING_ASSERT(this->engaged_);
return std::move(reference());
}
diff --git a/absl/types/optional_test.cc b/absl/types/optional_test.cc
index 47d5c8a2..874334e6 100644
--- a/absl/types/optional_test.cc
+++ b/absl/types/optional_test.cc
@@ -1057,8 +1057,7 @@ TEST(optionalTest, Value) {
// test constexpr value()
constexpr absl::optional<int> o1(1);
static_assert(1 == o1.value(), ""); // const &
-#if !defined(ABSL_SKIP_OVERLOAD_TEST_DUE_TO_MSVC_BUG) && \
- !defined(ABSL_SKIP_OVERLOAD_TEST_DUE_TO_GCC_BUG)
+#if !defined(_MSC_VER) && !defined(ABSL_SKIP_OVERLOAD_TEST_DUE_TO_GCC_BUG)
using COI = const absl::optional<int>;
static_assert(2 == COI(2).value(), ""); // const &&
#endif
@@ -1098,8 +1097,7 @@ TEST(optionalTest, DerefOperator) {
constexpr absl::optional<int> opt1(1);
static_assert(*opt1 == 1, "");
-#if !defined(ABSL_SKIP_OVERLOAD_TEST_DUE_TO_MSVC_BUG) && \
- !defined(ABSL_SKIP_OVERLOAD_TEST_DUE_TO_GCC_BUG)
+#if !defined(_MSC_VER) && !defined(ABSL_SKIP_OVERLOAD_TEST_DUE_TO_GCC_BUG)
using COI = const absl::optional<int>;
static_assert(*COI(2) == 2, "");
#endif
diff --git a/absl/types/span.h b/absl/types/span.h
index 21cda34b..734db695 100644
--- a/absl/types/span.h
+++ b/absl/types/span.h
@@ -276,7 +276,7 @@ class Span {
// Returns a reference to the i'th element of this span.
constexpr reference operator[](size_type i) const noexcept {
// MSVC 2015 accepts this as constexpr, but not ptr_[i]
- return ABSL_ASSERT(i < size()), *(data() + i);
+ return ABSL_HARDENING_ASSERT(i < size()), *(data() + i);
}
// Span::at()
@@ -295,7 +295,7 @@ class Span {
// Returns a reference to the first element of this span. The span must not
// be empty.
constexpr reference front() const noexcept {
- return ABSL_ASSERT(size() > 0), *data();
+ return ABSL_HARDENING_ASSERT(size() > 0), *data();
}
// Span::back()
@@ -303,7 +303,7 @@ class Span {
// Returns a reference to the last element of this span. The span must not
// be empty.
constexpr reference back() const noexcept {
- return ABSL_ASSERT(size() > 0), *(data() + size() - 1);
+ return ABSL_HARDENING_ASSERT(size() > 0), *(data() + size() - 1);
}
// Span::begin()
@@ -368,7 +368,7 @@ class Span {
//
// Removes the first `n` elements from the span.
void remove_prefix(size_type n) noexcept {
- assert(size() >= n);
+ ABSL_HARDENING_ASSERT(size() >= n);
ptr_ += n;
len_ -= n;
}
@@ -377,7 +377,7 @@ class Span {
//
// Removes the last `n` elements from the span.
void remove_suffix(size_type n) noexcept {
- assert(size() >= n);
+ ABSL_HARDENING_ASSERT(size() >= n);
len_ -= n;
}
@@ -665,7 +665,7 @@ constexpr Span<T> MakeSpan(T* ptr, size_t size) noexcept {
template <int&... ExplicitArgumentBarrier, typename T>
Span<T> MakeSpan(T* begin, T* end) noexcept {
- return ABSL_ASSERT(begin <= end), Span<T>(begin, end - begin);
+ return ABSL_HARDENING_ASSERT(begin <= end), Span<T>(begin, end - begin);
}
template <int&... ExplicitArgumentBarrier, typename C>
@@ -710,7 +710,7 @@ constexpr Span<const T> MakeConstSpan(T* ptr, size_t size) noexcept {
template <int&... ExplicitArgumentBarrier, typename T>
Span<const T> MakeConstSpan(T* begin, T* end) noexcept {
- return ABSL_ASSERT(begin <= end), Span<const T>(begin, end - begin);
+ return ABSL_HARDENING_ASSERT(begin <= end), Span<const T>(begin, end - begin);
}
template <int&... ExplicitArgumentBarrier, typename C>
diff --git a/absl/types/span_test.cc b/absl/types/span_test.cc
index 6ac234d3..2584339b 100644
--- a/absl/types/span_test.cc
+++ b/absl/types/span_test.cc
@@ -27,6 +27,7 @@
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/base/internal/exception_testing.h"
+#include "absl/base/options.h"
#include "absl/container/fixed_array.h"
#include "absl/container/inlined_vector.h"
#include "absl/hash/hash_testing.h"
@@ -233,7 +234,7 @@ TEST(IntSpan, ElementAccess) {
EXPECT_EQ(s.front(), s[0]);
EXPECT_EQ(s.back(), s[9]);
-#ifndef NDEBUG
+#if !defined(NDEBUG) || ABSL_OPTION_HARDENED
EXPECT_DEATH_IF_SUPPORTED(s[-1], "");
EXPECT_DEATH_IF_SUPPORTED(s[10], "");
#endif
@@ -273,6 +274,13 @@ TEST(IntSpan, RemovePrefixAndSuffix) {
EXPECT_EQ(s.size(), 0);
EXPECT_EQ(v, MakeRamp(20, 1));
+
+#if !defined(NDEBUG) || ABSL_OPTION_HARDENED
+ absl::Span<int> prefix_death(v);
+ EXPECT_DEATH_IF_SUPPORTED(prefix_death.remove_prefix(21), "");
+ absl::Span<int> suffix_death(v);
+ EXPECT_DEATH_IF_SUPPORTED(suffix_death.remove_suffix(21), "");
+#endif
}
TEST(IntSpan, Subspan) {