Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b19461b1 authored by Leon Scroggins III's avatar Leon Scroggins III
Browse files

Prevent calling new ftl::Optional

This class inherits from std::optional, which does not have a virtual
destructor. As such, deleting an object of ftl::Optional using a pointer
to its base class has undefined behavior. Prevent this by removing new
from ftl::Optional. This is generally not the right way to use it
anyway, and in fact this builds as is.

Delete new[] while we're at it. This should be enough of a signal not to
try to heap allocate these. It's still possible for a programmer to
circumvent these deletions, but they prevent the straightforward
(broken) use case.

Bug: 261035092
Test: make
Change-Id: Iafdaf98ed88920162af2b445caf0e4a69be51ab5
parent 95538a1c
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -95,6 +95,14 @@ struct Optional final : std::optional<T> {
    if (has_value()) return std::invoke(std::forward<F>(f), std::move(value()));
    return R();
  }

  // Delete new for this class. Its base doesn't have a virtual destructor, and
  // if it got deleted via base class pointer, it would cause undefined
  // behavior. There's not a good reason to allocate this object on the heap
  // anyway.
  static void* operator new(size_t) = delete;
  static void* operator new[](size_t) = delete;

};

template <typename T, typename U>