Skip to content

Abort in IsRaw(type) for a value outside RawImageTypes, as C++ does - #10

Closed
kunpai wants to merge 1 commit into
google:masterfrom
kunpai:is-raw-type-abort-out-of-range
Closed

kunpai wants to merge 1 commit into
google:masterfrom
kunpai:is-raw-type-abort-out-of-range

Conversation

@kunpai

@kunpai kunpai commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Abort in IsRaw(type) for a value outside RawImageTypes, as C++ does

Problem

IsRaw(const RawImageTypes type) (C++, Rust) switches over the enum in C++: kNonRawImage is false, every raw type is true, and any other value reaches assert(false). Without NDEBUG, that aborts the process. The port returns false for any value RawImageType::from_u32 does not know. For example:

C++ Rust
IsRaw(static_cast<RawImageTypes>(-1)) assert(false) fails, the process aborts (SIGABRT) returns false
IsRaw(static_cast<RawImageTypes>(25)) the process aborts (SIGABRT) returns false

Question

Which behaviour do we want: C++'s or the Rust port's? Specifically, for a value that is not a RawImageTypes enumerator, should IsRaw assert (like the C++ library), or return false (like the Rust port)?

This PR implements C++'s behaviour.

Fix

rust_is_raw_type matches on RawImageType::from_u32(raw_type) and calls std::process::abort() for None:

-    catch_unwind(AssertUnwindSafe(
-        || matches!(RawImageType::from_u32(raw_type), Some(t) if t != RawImageType::NonRaw),
-    ))
+    catch_unwind(AssertUnwindSafe(|| match RawImageType::from_u32(raw_type) {
+        Some(t) => t != RawImageType::NonRaw,
+        None => std::process::abort(),
+    }))

It aborts rather than panics because the catch_unwind around it would turn a panic back into false. The examples above then do what C++ does. Replayed before and after (x86-64):

C++ Rust main Rust patched
IsRaw(-1) SIGABRT (6) false SIGABRT (6)
IsRaw(25) SIGABRT (6) false SIGABRT (6)

Notes for review:

  • This turns a false return into a process abort. That is what a C++ build without NDEBUG does; with NDEBUG the C++ assert is gone and IsRaw returns false, as the port does today. Which build the port should match is part of the question.
  • Values 20 to 24 are enumerators of the port's RawImageType but not of piex's RawImageTypes, so this PR alone still answers them (IsRaw(20) is true, C++ aborts). Use piex's RawImageTypes numbers in the cxx bridge #9 makes the bridge use piex's numbering and reads this value with from_cxx, which returns None for them. Both PRs change this function, so whichever lands second needs a rebase; matching on from_cxx(raw_type) there makes 20 to 24 abort too.
  • The port's tests pass unchanged (44 + 174).

C++'s IsRaw(RawImageTypes) reaches assert(false) for a value outside
the enum. rust_is_raw_type returned false; abort instead.
@ShaddyDC

ShaddyDC commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

We're fine with having the same prod behaviour for now. The debug assert is nice, but not worth the effort making a change for here. Thanks for the find!

@ShaddyDC ShaddyDC closed this Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants