diff --git a/taglib/matroska/ebml/ebmlmkinfo.cpp b/taglib/matroska/ebml/ebmlmkinfo.cpp index fd5e6f24..959d3e2a 100644 --- a/taglib/matroska/ebml/ebmlmkinfo.cpp +++ b/taglib/matroska/ebml/ebmlmkinfo.cpp @@ -24,6 +24,9 @@ ***************************************************************************/ #include "ebmlmkinfo.h" + +#include + #include "ebmlstringelement.h" #include "ebmluintelement.h" #include "ebmlfloatelement.h" @@ -62,8 +65,9 @@ void EBML::MkInfo::parse(Matroska::Properties *properties) const } } - properties->setLengthInMilliseconds( - static_cast(duration * static_cast(timestampScale) / 1000000.0)); + const double length = duration * static_cast(timestampScale) / 1000000.0; + if(length >= 0.0 && length + 0.5 <= static_cast(std::numeric_limits::max())) + properties->setLengthInMilliseconds(static_cast(length)); properties->setTitle(parseTitle()); } diff --git a/taglib/matroska/ebml/ebmlmktracks.cpp b/taglib/matroska/ebml/ebmlmktracks.cpp index 14cf7a00..5852d3bf 100644 --- a/taglib/matroska/ebml/ebmlmktracks.cpp +++ b/taglib/matroska/ebml/ebmlmktracks.cpp @@ -24,6 +24,9 @@ ***************************************************************************/ #include "ebmlmktracks.h" + +#include + #include "ebmlstringelement.h" #include "ebmluintelement.h" #include "ebmlfloatelement.h" @@ -76,11 +79,16 @@ void EBML::MkTracks::parse(Matroska::Properties *properties) const } } if(bitDepth || channels) { - properties->setSampleRate(static_cast(samplingFrequency)); - properties->setBitsPerSample(static_cast(bitDepth)); - properties->setChannels(static_cast(channels)); - properties->setCodecName(codecId); - return; + const auto maxInt = static_cast(std::numeric_limits::max()); + if(samplingFrequency >= 0.0 && + samplingFrequency + 0.5 <= static_cast(maxInt) && + bitDepth <= maxInt && channels <= maxInt) { + properties->setSampleRate(static_cast(samplingFrequency)); + properties->setBitsPerSample(static_cast(bitDepth)); + properties->setChannels(static_cast(channels)); + properties->setCodecName(codecId); + return; + } } } } diff --git a/taglib/riff/wav/wavproperties.cpp b/taglib/riff/wav/wavproperties.cpp index b8eb5d9f..211ac6c7 100644 --- a/taglib/riff/wav/wavproperties.cpp +++ b/taglib/riff/wav/wavproperties.cpp @@ -26,6 +26,7 @@ #include "wavproperties.h" #include +#include #include "tdebug.h" #include "wavfile.h" @@ -172,13 +173,25 @@ void RIFF::WAV::Properties::read(File *file) 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); + const auto maxInt = static_cast(std::numeric_limits::max()); + if(length > 0.0 && length + 0.5 <= maxInt) { + d->length = static_cast(length + 0.5); + + const double bitrate = static_cast(streamLength) * 8.0 / length; + if(bitrate >= 0.0 && bitrate + 0.5 <= maxInt) + d->bitrate = static_cast(bitrate + 0.5); + } } else { if(const unsigned int byteRate = data.toUInt(8, false); byteRate > 0) { - d->length = static_cast(static_cast(streamLength) * 1000.0 / byteRate + 0.5); - d->bitrate = static_cast(byteRate * 8.0 / 1000.0 + 0.5); + const auto maxInt = static_cast(std::numeric_limits::max()); + const double length = static_cast(streamLength) * 1000.0 / byteRate; + if(length >= 0.0 && length + 0.5 <= maxInt) + d->length = static_cast(length + 0.5); + + const double bitrate = byteRate * 8.0 / 1000.0; + if(bitrate + 0.5 <= maxInt) + d->bitrate = static_cast(bitrate + 0.5); } } } diff --git a/taglib/shorten/shortenproperties.cpp b/taglib/shorten/shortenproperties.cpp index 96e61a3f..452acb12 100644 --- a/taglib/shorten/shortenproperties.cpp +++ b/taglib/shorten/shortenproperties.cpp @@ -26,6 +26,8 @@ #include "shortenproperties.h" +#include + #include "shortenutils.h" using namespace TagLib; @@ -63,9 +65,16 @@ Shorten::Properties::Properties(const PropertyValues *values, ReadStyle style) : d->bitsPerSample = values->bitsPerSample; d->sampleFrames = values->sampleFrames; - d->bitrate = static_cast(d->sampleRate * d->bitsPerSample * d->channelCount / 1000.0 + 0.5); - if(d->sampleRate > 0) - d->length = static_cast(static_cast(d->sampleFrames) * 1000.0 / d->sampleRate + 0.5); + const auto maxInt = static_cast(std::numeric_limits::max()); + const double bitrate = static_cast(d->sampleRate) * d->bitsPerSample * d->channelCount / 1000.0; + if(bitrate >= 0.0 && bitrate + 0.5 <= maxInt) + d->bitrate = static_cast(bitrate + 0.5); + + if(d->sampleRate > 0) { + const double length = static_cast(d->sampleFrames) * 1000.0 / d->sampleRate; + if(length > 0.0 && length + 0.5 <= maxInt) + d->length = static_cast(length + 0.5); + } } } diff --git a/taglib/trueaudio/trueaudioproperties.cpp b/taglib/trueaudio/trueaudioproperties.cpp index 88c714b6..ca3c4ba5 100644 --- a/taglib/trueaudio/trueaudioproperties.cpp +++ b/taglib/trueaudio/trueaudioproperties.cpp @@ -29,6 +29,8 @@ #include "trueaudioproperties.h" +#include + #include "tdebug.h" #include "tstring.h" @@ -144,8 +146,14 @@ void TrueAudio::Properties::read(const ByteVector &data, offset_t streamLength) 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); + const auto maxInt = static_cast(std::numeric_limits::max()); + if(length > 0.0 && length + 0.5 <= maxInt) { + d->length = static_cast(length + 0.5); + + const double bitrate = static_cast(streamLength) * 8.0 / length; + if(bitrate >= 0.0 && bitrate + 0.5 <= maxInt) + d->bitrate = static_cast(bitrate + 0.5); + } } } } diff --git a/taglib/wavpack/wavpackproperties.cpp b/taglib/wavpack/wavpackproperties.cpp index b36758f7..0f6fa1d6 100644 --- a/taglib/wavpack/wavpackproperties.cpp +++ b/taglib/wavpack/wavpackproperties.cpp @@ -31,6 +31,7 @@ #include #include +#include #include "tstring.h" #include "tdebug.h" @@ -314,8 +315,14 @@ void WavPack::Properties::read(File *file, offset_t streamLength) 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); + const auto maxInt = static_cast(std::numeric_limits::max()); + if(length > 0.0 && length + 0.5 <= maxInt) { + d->length = static_cast(length + 0.5); + + const double bitrate = static_cast(streamLength) * 8.0 / length; + if(bitrate >= 0.0 && bitrate + 0.5 <= maxInt) + d->bitrate = static_cast(bitrate + 0.5); + } } }