From 4c3e3cf1b8a201733c5203d348e6eb275aca0e8a Mon Sep 17 00:00:00 2001 From: manuaudio Date: Sat, 15 Aug 2026 08:58:33 -0700 Subject: [PATCH] FLAC: do not convert out of range properties to int STREAMINFO carries the total sample count in a 36 bit field and the sample rate in a 20 bit one, so a structurally valid file can declare 2^36-1 frames at 1 Hz. The millisecond length is then ~6.9e13 and the conversion to int is undefined: taglib/flac/flacproperties.cpp:136:35: runtime error: 6.87195e+13 is outside the range of representable values of type 'int' The bitrate on the next line is reachable the other way round, with a short stream at a high declared rate making the divisor tiny: taglib/flac/flacproperties.cpp:137:35: runtime error: 1.75922e+10 is outside the range of representable values of type 'int' Leave the field at its default rather than converting. Two reports before the change, none after. The 23 FLAC, APE, Ogg Vorbis, Opus and Speex files in tests/data report identical channels, sample rate, bitrate and length before and after, and the suite runs 576 tests either way. Assisted-By: Claude Code (Claude Opus 5) --- taglib/flac/flacproperties.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/taglib/flac/flacproperties.cpp b/taglib/flac/flacproperties.cpp index 0e829147..d308800b 100644 --- a/taglib/flac/flacproperties.cpp +++ b/taglib/flac/flacproperties.cpp @@ -25,6 +25,8 @@ #include "flacproperties.h" +#include + #include "tstring.h" #include "tdebug.h" @@ -131,10 +133,19 @@ void FLAC::Properties::read(const ByteVector &data, offset_t streamLength) d->sampleFrames = (hi << 32) | lo; + // The frame count is a 36 bit field and the sample rate a 20 bit one, so the + // millisecond length can land outside int, and a short stream at a high rate + // does the same to the bitrate. Converting a double the destination type + // cannot represent is undefined, so leave the field at its default instead. if(d->sampleFrames > 0 && d->sampleRate > 0) { const auto length = static_cast(d->sampleFrames) * 1000.0 / d->sampleRate; - d->length = static_cast(length + 0.5); - d->bitrate = static_cast(static_cast(streamLength) * 8.0 / length + 0.5); + if(length > 0.0 && length < static_cast(std::numeric_limits::max())) { + d->length = static_cast(length + 0.5); + + const double bitrate = static_cast(streamLength) * 8.0 / length; + if(bitrate >= 0.0 && bitrate < static_cast(std::numeric_limits::max())) + d->bitrate = static_cast(bitrate + 0.5); + } } if(data.size() >= pos + 16)