Skip to content

[MISRA-CERT-03] Shift operation UB not checked (MISRA Rule 12.2, CERT INT34-C) #265

Closed

Description

@godofecht

Description

The C generator emits raw << and >> with no check on shift amount or sign. Shifting by >= width is UB. Left-shifting a negative value is UB.

Reproduction:

function main() -> i32 {
    let z: i32 = 1 << 32    // shift >= width, UB
    let n: i32 = -1 << 1    // left shift of negative, UB
    return z + n
}

Generated C:

int32_t z = (1 << 32);    // UB
int32_t n = (-1 << 1);    // UB

Standards

  • MISRA C:2024 Rule 12.2 (Required): The right operand of a shift operator shall be in range [0, width-1] and the left operand shall not be negative for left shift.
  • CERT C INT34-C: Do not shift a negative number of bits or more bits than exist in the operand.

Fix

Emit: (((unsigned)(shift) < (sizeof(lhs_type) * 8) && !(shift < 0)) ? (lhs << shift) : (flow_shift_ub_handler(), 0)). Compile-time: reject literal out-of-range shifts.

Audit ID: MISRA-CERT-03

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