Bug 1542685 - Avoid integer overflows by multiplying doubles. r=padenot

SaferMultDiv(time, audioScale, videoScale) could easily result in overflow
because all three args are roughly equal, and SaferMultDiv would always do the
multiplication first. The worst-case is then multiplying an int64_t to another
int64_t that have very similar values. Since we represent time here in
microseconds, this would overflow after only 50 minutes.

Differential Revision: https://phabricator.services.mozilla.com/D26494

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Andreas Pehrson 2019-04-08 14:27:13 +00:00
Родитель 53a245f3e2
Коммит ef14eac7d8
1 изменённых файлов: 11 добавлений и 10 удалений

Просмотреть файл

@ -103,24 +103,25 @@ class DriftCompensator {
return aTime;
}
int64_t videoScaleUs = (aNow - mAudioStartTime).ToMicroseconds();
int64_t audioScaleUs = FramesToUsecs(samples, mAudioRate).value();
int64_t videoDurationUs = (aTime - mAudioStartTime).ToMicroseconds();
if (videoScaleUs == 0) {
videoScaleUs = audioScaleUs;
if (aNow == mAudioStartTime) {
LOG(LogLevel::Warning,
"DriftCompensator %p video scale 0, assuming no drift", this);
return aTime;
}
double videoScaleUs = (aNow - mAudioStartTime).ToMicroseconds();
double audioScaleUs = FramesToUsecs(samples, mAudioRate).value();
double videoDurationUs = (aTime - mAudioStartTime).ToMicroseconds();
TimeStamp reclocked =
mAudioStartTime +
TimeDuration::FromMicroseconds(
SaferMultDiv(videoDurationUs, audioScaleUs, videoScaleUs).value());
mAudioStartTime + TimeDuration::FromMicroseconds(
videoDurationUs * audioScaleUs / videoScaleUs);
LOG(LogLevel::Debug,
"DriftCompensator %p GetVideoTime, v-now: %.3fs, a-now: %.3fs; %.3fs "
"-> %.3fs (d %.3fms)",
this, (aNow - mAudioStartTime).ToSeconds(),
static_cast<double>(audioScaleUs) / 1000000.0,
TimeDuration::FromMicroseconds(audioScaleUs).ToSeconds(),
(aTime - mAudioStartTime).ToSeconds(),
(reclocked - mAudioStartTime).ToSeconds(),
(reclocked - aTime).ToMilliseconds());