Skip to content

[MISRA-CERT-02] Division by zero not checked (MISRA Rule 12.5, CERT INT33-C) #264

Closed

Description

@godofecht

Description

The C generator emits raw / and % with no zero-divisor check. Integer division by zero is undefined behavior in C.

Reproduction:

function main() -> i32 {
    let w: i32 = 10 / 0
    return w
}

Generated C:

int32_t w = (10 / 0);  // UB, traps on most platforms

src/flow/c_generator.py line 3137: no special handling for / or %.

Standards

  • MISRA C:2024 Rule 12.5 (Required): The right operand of a division or modulo operator shall not be zero.
  • CERT C INT33-C: Ensure that division and modulo operations do not result in divide-by-zero errors.

Fix

Emit a runtime check: ((rhs != 0) ? (lhs / rhs) : (flow_div_by_zero_handler(), 0)). Compile-time: reject literal 0 divisors. The handler should be configurable (abort by default, custom hook for safety profiles).

Audit ID: MISRA-CERT-02

Severity: Critical | Category: C generator - arithmetic safety

Activity

  1. added
    MISRA
    CERT
    on Aug 9, 2026
  2. added a commit that references this issue on Aug 10, 2026
  3. godofecht commented on Aug 10, 2026

    @godofecht
    ContributorAuthor

    Closed by #287 (commit 2b6d0c6). MISRA Phase 0 landed: -std=c11 -Wall -Wextra defaults, --profile safety → -Werror, UBSan/ASan/TSan support, div0/shift guards in type checker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    CERTMISRAcompilersecurity

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions