hsutter / cppfront

A personal experimental C++ Syntax 2 -> Syntax 1 compiler
Other
5.46k stars 238 forks source link

Add null violation checks for `std::unique_ptr`, `std::shared_ptr`, `std::optional` and `std::expected` to prevent UB #945

Closed bluetarpmedia closed 8 months ago

bluetarpmedia commented 8 months ago

assert_not_null now verifies std::unique_ptr, std::shared_ptr, std::optional and std::expected in addition to the original null pointer check.

These states are considered to be "null" and will result in a violation error if dereferenced or accessed:

Null violation errors will be reported in the following cases:

test: () = {
    ex: std::expected<int, bool> = std::unexpected(false);
    op: std::optional<int> = std::nullopt;

    up:= unique.new<int>(1);
    sp:= shared.new<int>(2);
    up.reset();
    sp.reset();

    v1 := ex*;   // Null violation error
    v2 := op*;   // Null violation error
    v3 := up*;   // Null violation error
    v4 := sp*;   // Null violation error
}

Add new regression tests:

hsutter commented 8 months ago

Thanks!

JohelEGP commented 8 months ago

The test output isn't platform-independent. See #556:

  • Update tests that finish through std::terminate.
    • Add std::set_terminate(std::abort); to avoid platform-dependent output on termination.