[libcxx-commits] [libcxx] [libc++][hardening] Categorize more assertions. (PR #75918)
Konstantin Varlamov via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Jan 4 19:14:17 PST 2024
================
@@ -65,8 +65,9 @@ template < class _InputIter,
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 void advance(_InputIter& __i, _Distance __orig_n) {
typedef typename iterator_traits<_InputIter>::difference_type _Difference;
_Difference __n = static_cast<_Difference>(std::__convert_to_integral(__orig_n));
- _LIBCPP_ASSERT_UNCATEGORIZED(__n >= 0 || __has_bidirectional_iterator_category<_InputIter>::value,
- "Attempt to advance(it, n) with negative n on a non-bidirectional iterator");
+ // Calling `advance` with a negative value on a non-bidirectional iterator in the current implementation.
+ _LIBCPP_ASSERT_PEDANTIC(__n >= 0 || __has_bidirectional_iterator_category<_InputIter>::value,
----------------
var-const wrote:
Similar to the other comment thread -- this is more-or-less the case for which I intended to use `pedantic`. Sorry, rereading this, I see that the comment misses an important part (fixed now) -- it should say _is a no-op in the current implementation_. Basically, `pedantic` is intended for situations where in our current implementation we either don't do anything or return some sort of a default value. We still want to flag this because it could still lead to problems in user code (probably more likely for the case of returning a default value, since that could be used as an index, etc. -- harder to imagine user code breaking because the function they called turned out to be a no-op, though I suppose it's still possible), although I suspect in most cases the effect would be "benign". What I want to capture, though, is that a) we don't know what the effects are, if any; and b) there is no immediate severe effect (memory corruption, program crash).
`pedantic` is enabled in the extensive mode and above, so despite the name everyone using something other than the deliberately minimalistic `fast` mode would get these assertions. Perhaps the name could be improved?
https://github.com/llvm/llvm-project/pull/75918
More information about the libcxx-commits
mailing list