Add death test for bad pdfium::span<> lifetimes
Ensure Asan catches dangling references in spans.
Change-Id: Id592abbea3f6cfa145194fe9f1951d6b0320237a
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/99050
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcrt/pdfium_span_unittest.cpp b/core/fxcrt/pdfium_span_unittest.cpp
index a142cbb..a57233e 100644
--- a/core/fxcrt/pdfium_span_unittest.cpp
+++ b/core/fxcrt/pdfium_span_unittest.cpp
@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <vector>
+
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/base/span.h"
@@ -49,3 +51,21 @@
pdfium::span<int> empty_span;
EXPECT_DEATH(empty_span.back() += 1, ".*");
}
+
+#if defined(ADDRESS_SANITIZER)
+namespace {
+
+void CreateDanglingSpan() {
+ pdfium::span<int> data_span;
+ {
+ std::vector<int> data(4);
+ data_span = pdfium::make_span(data);
+ }
+}
+
+} // namespace
+
+TEST(PdfiumSpanDeathTest, DanglingReference) {
+ EXPECT_DEATH(CreateDanglingSpan(), ".*");
+}
+#endif // defined(ADDRESS_SANITIZER)