From d8ec91e809e92bd8f45c5dee612546343b683b64 Mon Sep 17 00:00:00 2001 From: Christopher Brannon Date: Sun, 2 May 2021 11:46:19 -0700 Subject: [PATCH] Fix buffer size calculation when initializing output. With the previous calculation, it was possible for the buffer to be a byte or two too small to hold all of the data. --- src/libespeak-ng/speech.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libespeak-ng/speech.c b/src/libespeak-ng/speech.c index c92c2cbf..13aca0fb 100644 --- a/src/libespeak-ng/speech.c +++ b/src/libespeak-ng/speech.c @@ -280,7 +280,9 @@ ESPEAK_NG_API espeak_ng_STATUS espeak_ng_InitializeOutput(espeak_ng_OUTPUT_MODE buffer_length = min_buffer_length; // allocate 2 bytes per sample - outbuf_size = (buffer_length * samplerate)/500; + // Always round up to the nearest sample and the nearest byte. + int millisamples = buffer_length * samplerate; + outbuf_size = (millisamples + 1000 - millisamples % 1000) / 500; out_start = (unsigned char *)realloc(outbuf, outbuf_size); if (out_start == NULL) return ENOMEM;