Skip to content

feat(safety): overflow-checked signed integer arithmetic (#263) - #289

Merged
godofecht merged 2 commits into
mainfrom
misra-phase1-overflow-checks
Aug 10, 2026
Merged

godofecht merged 2 commits into
mainfrom
misra-phase1-overflow-checks

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

Summary

  • Emit FLOW_CHECKED_ADD/SUB/MUL macros for signed integer +,-,* operations (MISRA Rule 12.1 / CERT INT32-C)
  • Uses __builtin_{add,sub,mul}_overflow on Clang/GCC with portable fallback for other compilers
  • Calls flow_overflow_handler (abort + stderr, overridable via FLOW_OVERFLOW_HANDLER) on overflow
  • Float and unsigned arithmetic excluded: float overflow is IEEE (Inf/NaN, not UB), unsigned wraparound is well-defined
  • Fix _infer_expr_type to unwrap memref_ prefix so memref_f32 array accesses resolve to f32, not the i32 fallback

Test plan

  • 3 new unit tests in test_arith_safety.py (overflow handler emitted, float excluded, unsigned excluded)
  • All 10 arith safety unit tests pass
  • Ruff lint clean
  • Overflow example (2147483647 + 1) triggers handler at runtime
  • Normal arithmetic (100 + 200 * 2 - 50) produces correct result
  • CI tier-2 + lint + python tests pass

Closes #263

Generated with Devin

MISRA Phase 1: emit FLOW_CHECKED_ADD/SUB/MUL macros for signed integer
+,-,* operations. Uses __builtin_{add,sub,mul}_overflow on Clang/GCC
with a portable fallback for other compilers. Calls flow_overflow_handler
(abort + stderr message, overridable via FLOW_OVERFLOW_HANDLER) on trap.

Float and unsigned arithmetic are excluded: float overflow is IEEE
(Inf/NaN, not UB), unsigned wraparound is well-defined modular arithmetic.

Also fixes _infer_expr_type to unwrap memref_ prefix so memref_f32 array
accesses resolve to f32, not the i32 fallback.

Closes #263

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@cursor

cursor Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Bugbot is not enabled for your account, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

Overflow-checked +,-,* broke existing tests and the flowc self-host
roundtrip because the macros were always emitted. Now they only appear
under FLOW_PROFILE=safety/flight or FLOW_OVERFLOW_CHECK=1, matching
the -Werror pattern from MISRA Phase 0.

- Add overflow_check parameter to CGenerator and flow_to_c
- Auto-enable from FLOW_PROFILE env var
- Move FLOW_CHECKED_ADD/SUB/MUL macros and flow_overflow_handler
  inside the overflow_checks_enabled guard
- Add test_default_profile_no_overflow_check
- Update existing overflow tests to use _c_overflow helper

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@godofecht
godofecht merged commit 0dd4fa4 into main Aug 10, 2026
14 checks passed
@godofecht
godofecht deleted the misra-phase1-overflow-checks branch August 10, 2026 15:34
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.

[MISRA-CERT-01] Integer overflow not checked in generated C (MISRA Rule 12.1, CERT INT32-C)

1 participant