From 355d51cce48c8a75a1f585d133bb57ee19a28efc Mon Sep 17 00:00:00 2001 From: Johan Ekenberg Date: Fri, 10 Jul 2026 11:03:49 +0200 Subject: [PATCH] Limit silence scale to [0.01, 2] in ScaleSilence n below is computed as interval_length * scale and converted to an int32_t. Converting NaN or infinity is undefined behaviour, and a large finite scale overflows the conversion. In practice each of these aborts the process: --tts-silence-scale=nan terminate called after throwing std::length_error --tts-silence-scale=inf terminate called after throwing std::length_error --tts-silence-scale=1e9 terminate called after throwing std::length_error Since scale exists to shorten long pauses and is normally within (0, 1), reject anything outside [0.01, 2], log, and return the audio unscaled. Any comparison with NaN is false, so a single range check rejects NaN too. Negative values were already rejected by OfflineTtsConfig::Validate(), which requires silence_scale >= 0.001. NaN passed that check, because NaN < 0.001 is false, and callers of the Generate API pass GeneratedAudioConfig::silence_scale directly and bypass Validate() entirely. --- sherpa-onnx/csrc/offline-tts.cc | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/sherpa-onnx/csrc/offline-tts.cc b/sherpa-onnx/csrc/offline-tts.cc index 1d25154d8e..bcd31f2b55 100644 --- a/sherpa-onnx/csrc/offline-tts.cc +++ b/sherpa-onnx/csrc/offline-tts.cc @@ -35,6 +35,17 @@ GeneratedAudio GeneratedAudio::ScaleSilence(float scale) const { if (scale == 1) { return *this; } + + // scale is used to shorten long pauses, so it is normally within (0, 1). + // Values outside this range are rejected: n below is computed as + // interval_length * scale and converted to an int32_t, and NaN, infinity or + // a very large scale make that conversion undefined. Note that any + // comparison with NaN is false, so NaN is rejected here as well. + if (!(scale >= 0.01f && scale <= 2.0f)) { + SHERPA_ONNX_LOGE("Silence scale %f is not in [0.01, 2]. Skip scaling.", + scale); + return *this; + } // if the interval is larger than 0.2 second, then we assume it is a pause int32_t threshold = static_cast(sample_rate * 0.2);