[libcxx-commits] [libcxx] [C++ Safe Buffers][libcxx][test] Add a test for hardened span in ObjC++ (PR #205462)
via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Jun 23 18:00:01 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Ziqing Luo (ziqingluo-90)
<details>
<summary>Changes</summary>
The C++ Safe Buffers project does not officially support ObjC++, while people may still want to apply the analysis to ObjC++ code for improved confidence. However, it would be meaningless to do so if hardened containers do not trap out-of-bounds accesses. We need to add a test that shows that hardened containers do work for ObjC++.
rdar://180461846
---
Full diff: https://github.com/llvm/llvm-project/pull/205462.diff
1 Files Affected:
- (added) libcxx/test/libcxx/containers/views/views.span/span.elem/assert.op_idx.pass.mm (+48)
``````````diff
diff --git a/libcxx/test/libcxx/containers/views/views.span/span.elem/assert.op_idx.pass.mm b/libcxx/test/libcxx/containers/views/views.span/span.elem/assert.op_idx.pass.mm
new file mode 100644
index 0000000000000..4156bd5b9c2ea
--- /dev/null
+++ b/libcxx/test/libcxx/containers/views/views.span/span.elem/assert.op_idx.pass.mm
@@ -0,0 +1,48 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+// <span>
+//
+// constexpr reference operator[](size_type idx) const;
+
+// Make sure that accessing a span out-of-bounds triggers an assertion in an
+// Objective-C++ translation unit.
+
+// REQUIRES: objective-c++
+// REQUIRES: has-unix-headers
+// UNSUPPORTED: libcpp-hardening-mode=none
+// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
+// ADDITIONAL_COMPILE_FLAGS: -lobjc
+
+#include <span>
+
+#include "check_assertion.h"
+
+#import <objc/NSObject.h>
+
+ at interface Wrapper : NSObject {
+ int _array[3];
+}
+- (std::span<int>)dynamicSpan;
+- (std::span<int, 3>)staticSpan;
+ at end
+
+ at implementation Wrapper
+- (std::span<int>)dynamicSpan { return std::span<int>(_array, 3); }
+- (std::span<int, 3>)staticSpan { return std::span<int, 3>(_array, 3); }
+ at end
+
+int main(int, char**) {
+ Wrapper* wrapper = [[Wrapper alloc] init];
+
+ TEST_LIBCPP_ASSERT_FAILURE([wrapper dynamicSpan][3], "span<T>::operator[](index): index out of range");
+ TEST_LIBCPP_ASSERT_FAILURE([wrapper staticSpan][3], "span<T, N>::operator[](index): index out of range");
+
+ return 0;
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/205462
More information about the libcxx-commits
mailing list