[libcxx-commits] [libcxx] [libc++] Fix hardening checks in std::string operator[] (PR #122414)
Peng Liu via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Jan 9 20:19:56 PST 2025
https://github.com/winner245 updated https://github.com/llvm/llvm-project/pull/122414
>From 0d909b9d1d14b30922eb497a655075dd4eb2c9a8 Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Wed, 8 Jan 2025 23:33:30 -0500
Subject: [PATCH] Fix out-of-bounds check in std::string operator[]
---
libcxx/include/string | 4 +-
.../basic.string/assert.index.oob.pass.cpp | 56 +++++++++++++++++++
2 files changed, 58 insertions(+), 2 deletions(-)
create mode 100644 libcxx/test/libcxx/containers/strings/basic.string/assert.index.oob.pass.cpp
diff --git a/libcxx/include/string b/libcxx/include/string
index 39982d5670bdbb..41b7d55854e7fe 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -1342,7 +1342,7 @@ public:
}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference operator[](size_type __pos) const _NOEXCEPT {
- _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__pos <= size(), "string index out of bounds");
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__pos < size(), "string index out of bounds");
if (__builtin_constant_p(__pos) && !__fits_in_sso(__pos)) {
return *(__get_long_pointer() + __pos);
}
@@ -1350,7 +1350,7 @@ public:
}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator[](size_type __pos) _NOEXCEPT {
- _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__pos <= size(), "string index out of bounds");
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__pos < size(), "string index out of bounds");
if (__builtin_constant_p(__pos) && !__fits_in_sso(__pos)) {
return *(__get_long_pointer() + __pos);
}
diff --git a/libcxx/test/libcxx/containers/strings/basic.string/assert.index.oob.pass.cpp b/libcxx/test/libcxx/containers/strings/basic.string/assert.index.oob.pass.cpp
new file mode 100644
index 00000000000000..93e2c4d417695c
--- /dev/null
+++ b/libcxx/test/libcxx/containers/strings/basic.string/assert.index.oob.pass.cpp
@@ -0,0 +1,56 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <string>
+
+// Index string out of bounds.
+
+// REQUIRES: has-unix-headers
+// UNSUPPORTED: c++03
+// UNSUPPORTED: libcpp-hardening-mode=none
+// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
+
+#include <string>
+#include <cassert>
+
+#include "check_assertion.h"
+#include "min_allocator.h"
+
+int main(int, char**) {
+ // Test the const overloads.
+ {
+ using C = std::basic_string<char, std::char_traits<char>, safe_allocator<char> >;
+ const C c;
+ TEST_LIBCPP_ASSERT_FAILURE(c[0], "string index out of bounds");
+ TEST_LIBCPP_ASSERT_FAILURE(c[1], "string index out of bounds");
+ }
+ {
+ using C = std::basic_string<char, std::char_traits<char>, safe_allocator<char> >;
+ const C c = "abc";
+ TEST_LIBCPP_ASSERT_FAILURE(c[3], "string index out of bounds");
+ TEST_LIBCPP_ASSERT_FAILURE(c[4], "string index out of bounds");
+ TEST_LIBCPP_ASSERT_FAILURE(c[100], "string index out of bounds");
+ }
+
+ // Test the nonconst overloads.
+ {
+ using C = std::basic_string<char, std::char_traits<char>, safe_allocator<char> >;
+ C c;
+ TEST_LIBCPP_ASSERT_FAILURE(c[0], "string index out of bounds");
+ TEST_LIBCPP_ASSERT_FAILURE(c[1], "string index out of bounds");
+ }
+ {
+ using C = std::basic_string<char, std::char_traits<char>, safe_allocator<char> >;
+ C c = "abc";
+ TEST_LIBCPP_ASSERT_FAILURE(c[3], "string index out of bounds");
+ TEST_LIBCPP_ASSERT_FAILURE(c[4], "string index out of bounds");
+ TEST_LIBCPP_ASSERT_FAILURE(c[100], "string index out of bounds");
+ }
+
+ return 0;
+}
More information about the libcxx-commits
mailing list