diff --git a/taglib/mp4/mp4properties.cpp b/taglib/mp4/mp4properties.cpp index 38f83269..dbb2aa44 100644 --- a/taglib/mp4/mp4properties.cpp +++ b/taglib/mp4/mp4properties.cpp @@ -25,6 +25,8 @@ #include "mp4properties.h" +#include + #include "tdebug.h" #include "tstring.h" #include "tmap.h" @@ -203,8 +205,15 @@ MP4::Properties::read(File *file, const Atoms *atoms) } } } - if(unit > 0 && length > 0) - d->length = static_cast(static_cast(length) * 1000.0 / static_cast(unit) + 0.5); + // The mdhd duration is a signed 64 bit field in version 1 and the timescale + // beside it may be as low as 1, so the millisecond length can land outside + // int. Converting a double the destination type cannot represent is + // undefined, so leave the field at its default instead. + if(unit > 0 && length > 0) { + const double lengthMs = static_cast(length) * 1000.0 / static_cast(unit); + if(lengthMs > 0.0 && lengthMs < static_cast(std::numeric_limits::max())) + d->length = static_cast(lengthMs + 0.5); + } MP4::Atom *atom = trak->find("mdia", "minf", "stbl", "stsd"); if(!atom) {