-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[libc++] Add unsafe-buffer-usage attributes to span, vector, string and string_view #119603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
#ifndef _LIBCPP___ITERATOR_WRAP_ITER_H | ||
#define _LIBCPP___ITERATOR_WRAP_ITER_H | ||
|
||
#include <__assert> | ||
#include <__compare/ordering.h> | ||
#include <__compare/three_way_comparable.h> | ||
#include <__config> | ||
|
@@ -57,7 +58,10 @@ class __wrap_iter { | |
int> = 0> | ||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter(const __wrap_iter<_OtherIter>& __u) _NOEXCEPT | ||
: __i_(__u.__i_) {} | ||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator*() const _NOEXCEPT { return *__i_; } | ||
_LIBCPP_VALID_ELEMENT_ACCESS_PRECONDITION _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This warns when hardening is disabled, but whether the checking is happening is dependent also on the checked iterator ABI flag. In this case, since the checked iterator ABI flag is off (we're using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use case is when you've enabled hardening in libc++ (so you get bounds checking in span::operator[], array::operator[], etc.), but your vendor configuration -- e.g., Darwin user space -- does not enable hardening for iterators (so you can't safely use span.begin(), array.begin(), etc.), because enabling hardening for iterators would break ABI. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this generate -Wunsafe-buffer-usage even if hardening is not enabled then too? The warning isn't tied to hardening. These iterators are never safe buffer usage. |
||
operator*() const _NOEXCEPT { | ||
return *__i_; | ||
} | ||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pointer operator->() const _NOEXCEPT { | ||
return std::__to_address(__i_); | ||
} | ||
|
@@ -96,7 +100,8 @@ class __wrap_iter { | |
*this += -__n; | ||
return *this; | ||
} | ||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator[](difference_type __n) const _NOEXCEPT { | ||
_LIBCPP_VALID_ELEMENT_ACCESS_PRECONDITION _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference | ||
operator[](difference_type __n) const _NOEXCEPT { | ||
return __i_[__n]; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be really nice if these attributes could automatically be added via the
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS
assertion macro instead, since we pretty much want to have this attribute on each function that has such preconditions.