Skip to content

Guard sbuf size calculations against int overflow#33

Open
billdenney wants to merge 3 commits into
mainfrom
fix/mem-sbuf-int-overflow
Open

Guard sbuf size calculations against int overflow#33
billdenney wants to merge 3 commits into
mainfrom
fix/mem-sbuf-int-overflow

Conversation

@billdenney
Copy link
Copy Markdown
Contributor

sAppendN/sAppend/addLine (s and lProp paths) compute the new allocation size as
int mx = sbb->o + 2 + n + SBUF_MXBUF;
where n is a user-controlled length. When n is large enough the addition overflows int to a negative value; R_Realloc then interprets that as a huge unsigned size and crashes (or fails) instead of returning an informative R error.

Add a check n > INT_MAX - <existing summands> immediately before each allocation expression, raising string buffer size overflow: input too large (and line array size overflow: too many lines for the lProp path) before the unsigned wrap can happen.

Mirror of the fix in rxode2 inst/include/sbuf.c. monolix2rx ships its own copy of the same buffer routines so it is fixed independently.

billdenney and others added 3 commits May 1, 2026 20:42
`sAppendN`/`sAppend`/`addLine` (s and lProp paths) compute the new
allocation size as
    int mx = sbb->o + 2 + n + SBUF_MXBUF;
where `n` is a user-controlled length.  When `n` is large enough the
addition overflows `int` to a negative value; `R_Realloc` then
interprets that as a huge unsigned size and crashes (or fails) instead
of returning an informative R error.

Add a check `n > INT_MAX - <existing summands>` immediately before each
allocation expression, raising `string buffer size overflow: input too
large` (and `line array size overflow: too many lines` for the lProp
path) before the unsigned wrap can happen.

Mirror of the fix in rxode2 inst/include/sbuf.c.  monolix2rx ships its
own copy of the same buffer routines so it is fixed independently.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Skipped by default because triggering the overflow needs a ~2.147 GB
identifier string (~2.5 GB peak RAM during the test).  Mirrors the
test added to rxode2 and nonmem2rx for the same fix.  Manual-run
instructions are in the test body.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented May 3, 2026

Codecov Report

❌ Patch coverage is 25.00000% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.44%. Comparing base (ca1ffea) to head (a9fc6bb).

Files with missing lines Patch % Lines
src/sbuf.c 25.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #33      +/-   ##
==========================================
- Coverage   89.44%   89.44%   -0.01%     
==========================================
  Files          60       60              
  Lines        6360     6368       +8     
==========================================
+ Hits         5689     5696       +7     
- Misses        671      672       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

1 participant