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)