Skip to content

Coupling::stretch_with_identities: validate coupling_positions bounds - #272

Merged
jhauschild merged 1 commit into
mainfrom
swi_error_messages
Sep 14, 2026
Merged

jhauschild merged 1 commit into
mainfrom
swi_error_messages

Conversation

@LudwigZw

@LudwigZw LudwigZw commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Summary

Coupling::stretch_with_identities indexed all_sites with values taken
directly from the caller-supplied coupling_positions, using unchecked
operator[]. Out-of-range, negative, or (in the degenerate zero-tensor
case) empty coupling_positions caused a segfault instead of a Python-side
exception. This adds explicit validation so misuse fails with a clear
ValueError instead.

What the two new checks do

  • Range check: every coupling_positions[i] must satisfy
    0 <= coupling_positions[i] < len(all_sites), since each one indexes into
    all_sites. Violating this now raises:
    `coupling_positions[i]` = <value> is out of range for `all_sites` of length <n>
  • Empty check: coupling_positions (and thus factorization) must be
    non-empty before taking .front()/.back(), which are undefined
    behavior on an empty vector. Violating this now raises:
    `coupling_positions` is empty; a `Coupling` with no factorization tensors cannot be stretched

Test plan

  • Reproduced the segfault locally with out-of-range and negative
    coupling_positions before the fix
  • Confirmed both cases raise ValueError cleanly after the fix
  • Existing stretch_with_identities tests in
    tests/python_tests/models/test_couplings.py still pass

Raise a descriptive std::invalid_argument instead of segfaulting on
out-of-range or empty coupling_positions.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@jhauschild
jhauschild merged commit d0b5e74 into main Sep 14, 2026
4 checks passed
@jhauschild
jhauschild deleted the swi_error_messages branch September 14, 2026 08:55
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