Skip to content

common: convert O(1) debug asserts to unconditional throws - #4493

Open
jhale wants to merge 2 commits into
jhale/common-exception-reclassifyfrom
jhale/common-o1-checks
Open

common: convert O(1) debug asserts to unconditional throws#4493
jhale wants to merge 2 commits into
jhale/common-exception-reclassifyfrom
jhale/common-o1-checks

Conversation

@jhale

@jhale jhale commented Sep 13, 2026

Copy link
Copy Markdown
Member

local_range, MPI::distribute_data and sort_by_perm validated cheap,
O(1) preconditions (positive size/shape1, size relationships) only via
assert, which compiles out under NDEBUG. Bad arguments then caused a
division by zero or an out-of-bounds read in Release builds instead of
a diagnostic. Per AGENTS.md, an O(1) check should be an unconditional
throw rather than assert-only, so convert these to
std::invalid_argument and add Catch2 coverage.

MPI::index_owner and math::det/inv/pinv were also surveyed but are
called per-element inside hot loops (mesh partitioning, per-quadrature
-point Jacobian computation), so they are intentionally left as
assert-only per the "no exceptions in hot loops" rule. IndexMap.cpp is
excluded from this pass as it is being hardened separately.

AI assistance: I used Claude to draft this PR. I reviewed, edited,
tested, and take responsibility for the final contribution.


Stack created with GitHub Stacks CLIGive Feedback 💬

local_range, MPI::distribute_data and sort_by_perm validated cheap,
O(1) preconditions (positive size/shape1, size relationships) only via
assert, which compiles out under NDEBUG. Bad arguments then caused a
division by zero or an out-of-bounds read in Release builds instead of
a diagnostic. Per AGENTS.md, an O(1) check should be an unconditional
throw rather than assert-only, so convert these to
std::invalid_argument and add Catch2 coverage.

MPI::index_owner and math::det/inv/pinv were also surveyed but are
called per-element inside hot loops (mesh partitioning, per-quadrature
-point Jacobian computation), so they are intentionally left as
assert-only per the "no exceptions in hot loops" rule. IndexMap.cpp is
excluded from this pass as it is being hardened separately.

AI assistance: I used Claude to draft this PR. I reviewed, edited,
tested, and take responsibility for the final contribution.
@jhale
jhale added this pull request to stack #4494 September 13, 2026 10:16
Comment thread cpp/dolfinx/common/local_range.h
Comment thread cpp/test/common/utils.cpp Outdated

using namespace dolfinx;

TEST_CASE("local_range bad arguments", "[local_range]")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly over testing, will remove.

@jhale
jhale removed this pull request from stack #4494 September 13, 2026 12:37
@jhale
jhale added this pull request to stack #4498 September 13, 2026 12:38
@jhale
jhale removed this pull request from stack #4498 September 13, 2026 12:38
@jhale
jhale added this pull request to stack #4499 September 13, 2026 12:38
The added CHECK_THROWS_AS cases only verified that a one-line
if (cond) throw fires, which doesn't exercise any meaningful logic
and adds little confidence beyond what's already clear on inspection.

AI assistance: I used Claude to draft this PR. I reviewed, edited,
tested, and take responsibility for the final contribution.
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