Bug 1206362 - be careful about double -> int conversion. r=padenot, a=lizzard
authorKarl Tomlinson <karlt+@karlt.net>
Mon, 21 Sep 2015 15:44:10 +1200
changeset 296152 e9d6e9e5ab707b4c5f436c288195938840eea3af
parent 296151 f062d521fd5806e0f85c6c2156a66a11803e7a98
child 296153 4ecde2f4404f7b1b01505ac07c42a8a3aefcc127
push id5245
push userraliiev@mozilla.com
push dateThu, 29 Oct 2015 11:30:51 +0000
treeherdermozilla-beta@dac831dc1bd0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspadenot, lizzard
bugs1206362
milestone43.0a2
Bug 1206362 - be careful about double -> int conversion. r=padenot, a=lizzard
dom/media/webaudio/AudioBufferSourceNode.cpp
--- a/dom/media/webaudio/AudioBufferSourceNode.cpp
+++ b/dom/media/webaudio/AudioBufferSourceNode.cpp
@@ -698,18 +698,24 @@ AudioBufferSourceNode::SendOffsetAndDura
   int32_t offsetSamples = std::max(0, NS_lround(mOffset * rate));
 
   // Don't set parameter unnecessarily
   if (offsetSamples > 0) {
     aStream->SetInt32Parameter(BUFFERSTART, offsetSamples);
   }
 
   if (mDuration != std::numeric_limits<double>::min()) {
-    bufferEnd = std::min(bufferEnd,
-                         offsetSamples + NS_lround(mDuration * rate));
+    MOZ_ASSERT(mDuration >= 0.0); // provided by Start()
+    MOZ_ASSERT(rate >= 0.0f); // provided by AudioBuffer::Create()
+    static_assert(std::numeric_limits<double>::digits >=
+                  std::numeric_limits<decltype(bufferEnd)>::digits,
+                  "bufferEnd should be represented exactly by double");
+    // + 0.5 rounds mDuration to nearest sample when assigned to bufferEnd.
+    bufferEnd = std::min<double>(bufferEnd,
+                                 offsetSamples + mDuration * rate + 0.5);
   }
   aStream->SetInt32Parameter(BUFFEREND, bufferEnd);
 
   MarkActive();
 }
 
 void
 AudioBufferSourceNode::Stop(double aWhen, ErrorResult& aRv)