[libcxx-commits] [libcxx] [libc++][hardening] Categorize more assertions. (PR #75918)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Thu Dec 21 12:42:17 PST 2023


================
@@ -73,7 +73,8 @@ class directory_iterator {
   _LIBCPP_HIDE_FROM_ABI ~directory_iterator() = default;
 
   _LIBCPP_HIDE_FROM_ABI const directory_entry& operator*() const {
-    _LIBCPP_ASSERT_UNCATEGORIZED(__imp_, "The end iterator cannot be dereferenced");
+    // Note: this check duplicates a check in `__dereference()`.
----------------
ldionne wrote:

Per our discussion just now, these are the different ways we can think of handling the situation of "redundant" checks:

```c++
// Option #1: leave it as-is
void f(std::optional<T> foo) {
    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(foo.has_value(), "oops");
    use(*foo);
}

// Option #2: remove it, it's implicitly checked, we "know" it
void f(std::optional<T> foo) {
    use(*foo);
}

// Option #3: Use a comment
void f(std::optional<T> foo) {
    // implicit precondition: foo.has_value(), already checked in operator* below
    use(*foo);
}

// Option #4: Macro orthogonal to the assertion category
void f(std::optional<T> foo) {
    _LIBCPP_REDUNDANT_ASSERTION(_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(foo.has_value(), "oops"));
    use(*foo);
}

// Option #5: Add a new category
void f(std::optional<T> foo) {
    // REDUNDANT|EARLY|EXTRA|...
    _LIBCPP_ASSERT_EARLY(foo.has_value(), "oops");
    use(*foo);
}
```

https://github.com/llvm/llvm-project/pull/75918


More information about the libcxx-commits mailing list