Skip to content

Fix integer overflow in insertPitchPeriod/enlargeOutputBufferIfNeeded - #4

Open
austek wants to merge 1 commit into
espeak-ng:masterfrom
ZirekHQ:fix/insert-pitch-period-overflow-espeak
Open

Fix integer overflow in insertPitchPeriod/enlargeOutputBufferIfNeeded#4
austek wants to merge 1 commit into
espeak-ng:masterfrom
ZirekHQ:fix/insert-pitch-period-overflow-espeak

Conversation

@austek

@austek austek commented Aug 23, 2026

Copy link
Copy Markdown

Summary

period + newSamples is computed correctly in long in insertPitchPeriod, but was truncated to int when passed into enlargeOutputBufferIfNeeded's int parameter. Inside that function, the int addition numOutputSamples + numSamples and the buffer growth calculation can also signed-overflow, leading to a too-small allocation followed by writes sized for the original, larger sample count.

Guards both: insertPitchPeriod now checks the long sum against INT_MAX before truncating, and enlargeOutputBufferIfNeeded checks its own addition for overflow before applying it, returning 0 through the existing failure path in both cases instead of invoking undefined behavior.

Same bug, same fix as waywardgeek/sonic#62 — this repo's insertPitchPeriod/enlargeOutputBufferIfNeeded are byte-identical to what that file had upstream, so the fix ports directly.

Test plan

  • cd tests && make runtests && ./runtests — passes, new tests/overflow_test.c included
  • Same build under -fsanitize=address,undefined — no finding from this path; only the separate, already-known findSincCoefficient left-shift issue remains

period + newSamples is computed correctly in long in insertPitchPeriod,
but was truncated to int when passed into enlargeOutputBufferIfNeeded's
int parameter. Inside that function, the int addition
numOutputSamples + numSamples and the buffer growth calculation can
also signed-overflow, both leading to a too-small allocation followed
by writes sized for the original, larger sample count.

Guard both: insertPitchPeriod now checks the long sum against INT_MAX
before truncating, and enlargeOutputBufferIfNeeded checks its own
addition for overflow before applying it, returning 0 through the
existing failure path in both cases instead of invoking undefined
behavior.

Adds tests/overflow_test.c, a white-box test that calls both functions
directly (they are no longer static, for this reason) with values
chosen to hit the overflow guards.

Ported from waywardgeek/sonic#62, which fixes the same bug (also
present here, byte-identical insertPitchPeriod/enlargeOutputBufferIfNeeded)
upstream.
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