← Back to Pull requests

Fix integer overflow in quirc_resize() dimension calculation #158

Closed
unknown closed this pull request without merging on Oct 28, 2026 17:03 UTC
View on GitHub
unknown unknown commented on Jul 15, 2026 05:10 UTC
Author

Problem

In quirc_resize(), w/h (and q->w/q->h) are int, so:

size_t olddim = q->w * q->h;
size_t newdim = w * h;

evaluates q->w q->h and w h in signed int arithmetic first, then converts the (possibly overflowed) result to size_t. For large-but-otherwise-valid dimensions whose product exceeds INT_MAX (2147483647), this overflows and produces a wrapped/garbage value, which then feeds the memcpy() length bound (min = (olddim < newdim ? olddim : newdim)) a few lines below.

Reproduction

Built an unmodified copy of lib/quirc.c and called, in sequence:

quirc_resize(q, 50000, 50000);  /* 50000*50000 = 2,500,000,000 > INT_MAX, succeeds */
quirc_resize(q, 60000, 60000);  /* 60000*60000 = 3,600,000,000 > INT_MAX */

Both individual dimension products overflow int while each requested allocation (~2.4GB / ~3.4GB) is realistically satisfiable, so neither call is rejected by calloc()'s own overflow-safe size check. The second call segfaults:

Thread 1 received signal SIGSEGV, Segmentation fault.
0x00007ff9f184974e in msvcrt!memmove ()
#0  0x00007ff9f184974e in msvcrt!memmove ()
#1  0x000000000040182b in quirc_resize (q=0xb29770, w=60000, h=60000) at lib/quirc.c:84
#2  ... at main () ...
84		(void)memcpy(image, q->image, min);

confirmed with gdb: the crash is inside the memcpy() at quirc.c:84, because olddim/newdim wrapped to a value wildly inconsistent with the real, correctly-sized buffers.

Fix

Cast each operand to size_t before multiplying, so the multiplication itself happens in size_t arithmetic (both w/h are already validated non-negative a few lines above, so the cast is safe):

size_t olddim = (size_t)q->w * (size_t)q->h;
size_t newdim = (size_t)w * (size_t)h;

This mirrors the approach taken in #103 for the similar overflow in otsu().

Testing

  • Re-ran the two-call repro above against the patched code: both calls now return successfully (no crash).

  • Regression-checked ordinary (non-overflowing) resize sequences (grow/shrink/zero-size/negative-rejection/1920x1080) to confirm identical behavior to before the change -- the fix only changes behavior once w*h would have overflowed int.

  • gcc -Wall -Wextra on the changed file introduces no new warnings compared to the pre-patch build.
  • Diff: 291 lines (+218 / -73) across 3 files Reviewer: kaworu Review duration: 151913m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.40) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 291 lines diff across 3 files
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.340
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #158"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 39.9% (MEDIUM) · Higher = larger blast radius
    39.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 17.2% · Safe to merge
    17.2%
    Repository