Bug 1325707: Follow-up, fix invalid unsigned comparison. r=gerald,a=jcristau
authorJean-Yves Avenard <jyavenard@mozilla.com>
Tue, 21 Feb 2017 15:54:50 +0100
changeset 376421 eeeb9c132d0ed10d0f76f51a4b25cfb2f76bd671
parent 376420 66e711359af5799d9f133f59fab3d730cf8a20cb
child 376422 be7c68c323128bc06a964afe3ff4da2717119b68
push id6996
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 20:48:21 +0000
treeherdermozilla-beta@d89512dab048 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgerald, jcristau
bugs1325707
milestone53.0a2
Bug 1325707: Follow-up, fix invalid unsigned comparison. r=gerald,a=jcristau MozReview-Commit-ID: 26bxAyhxbsx
dom/media/platforms/agnostic/OpusDecoder.cpp
--- a/dom/media/platforms/agnostic/OpusDecoder.cpp
+++ b/dom/media/platforms/agnostic/OpusDecoder.cpp
@@ -180,36 +180,43 @@ OpusDataDecoder::DoDecode(MediaRawData* 
 
   if (!mLastFrameTime || mLastFrameTime.ref() != aSample->mTime) {
     // We are starting a new block.
     mFrames = 0;
     mLastFrameTime = Some(aSample->mTime);
   }
 
   // Maximum value is 63*2880, so there's no chance of overflow.
-  uint32_t frames_number = opus_packet_get_nb_frames(aSample->Data(),
-                                                    aSample->Size());
+  int frames_number =
+    opus_packet_get_nb_frames(aSample->Data(), aSample->Size());
   if (frames_number <= 0) {
-    OPUS_DEBUG("Invalid packet header: r=%ld length=%ld",
-               frames_number, aSample->Size());
+    OPUS_DEBUG("Invalid packet header: r=%d length=%u",
+               frames_number, uint32_t(aSample->Size()));
     return MediaResult(NS_ERROR_DOM_MEDIA_DECODE_ERR,
                        RESULT_DETAIL("Invalid packet header: r=%d length=%u",
                                      frames_number, uint32_t(aSample->Size())));
   }
 
-  uint32_t samples = opus_packet_get_samples_per_frame(
+  int samples = opus_packet_get_samples_per_frame(
     aSample->Data(), opus_int32(mOpusParser->mRate));
 
 
   // A valid Opus packet must be between 2.5 and 120 ms long (48kHz).
-  uint32_t frames = frames_number*samples;
+  CheckedInt32 totalFrames =
+    CheckedInt32(frames_number) * CheckedInt32(samples);
+  if (!totalFrames.isValid()) {
+    return MediaResult(NS_ERROR_DOM_MEDIA_DECODE_ERR,
+                       RESULT_DETAIL("Frames count overflow"));
+  }
+
+  int frames = totalFrames.value();
   if (frames < 120 || frames > 5760) {
-    OPUS_DEBUG("Invalid packet frames: %u", frames);
+    OPUS_DEBUG("Invalid packet frames: %d", frames);
     return MediaResult(NS_ERROR_DOM_MEDIA_DECODE_ERR,
-                       RESULT_DETAIL("Invalid packet frames:%u", frames));
+                       RESULT_DETAIL("Invalid packet frames:%d", frames));
   }
 
   AlignedAudioBuffer buffer(frames * channels);
   if (!buffer) {
     return MediaResult(NS_ERROR_OUT_OF_MEMORY, __func__);
   }
 
   // Decode to the appropriate sample type.
@@ -221,40 +228,41 @@ OpusDataDecoder::DoDecode(MediaRawData* 
   int ret = opus_multistream_decode(mOpusDecoder,
                                     aSample->Data(), aSample->Size(),
                                     buffer.get(), frames, false);
 #endif
   if (ret < 0) {
     return MediaResult(NS_ERROR_DOM_MEDIA_DECODE_ERR,
                        RESULT_DETAIL("Opus decoding error:%d", ret));
   }
-  NS_ASSERTION(uint32_t(ret) == frames, "Opus decoded too few audio samples");
+  NS_ASSERTION(ret == frames, "Opus decoded too few audio samples");
   CheckedInt64 startTime = aSample->mTime;
 
   // Trim the initial frames while the decoder is settling.
   if (mSkip > 0) {
     int32_t skipFrames = std::min<int32_t>(mSkip, frames);
     int32_t keepFrames = frames - skipFrames;
-    OPUS_DEBUG("Opus decoder skipping %d of %d frames", skipFrames, frames);
+    OPUS_DEBUG(
+      "Opus decoder skipping %d of %d frames", skipFrames, frames);
     PodMove(buffer.get(),
             buffer.get() + skipFrames * channels,
             keepFrames * channels);
     startTime = startTime + FramesToUsecs(skipFrames, mOpusParser->mRate);
     frames = keepFrames;
     mSkip -= skipFrames;
   }
 
   if (aSample->mDiscardPadding > 0) {
-    OPUS_DEBUG("Opus decoder discarding %u of %u frames",
+    OPUS_DEBUG("Opus decoder discarding %u of %d frames",
                aSample->mDiscardPadding, frames);
     // Padding discard is only supposed to happen on the final packet.
     // Record the discard so we can return an error if another packet is
     // decoded.
-    if (aSample->mDiscardPadding > frames) {
-    // Discarding more than the entire packet is invalid.
+    if (aSample->mDiscardPadding > uint32_t(frames)) {
+      // Discarding more than the entire packet is invalid.
       OPUS_DEBUG("Opus error, discard padding larger than packet");
       return MediaResult(NS_ERROR_DOM_MEDIA_FATAL_ERR,
                          RESULT_DETAIL("Discard padding larger than packet"));
     }
 
     mPaddingDiscarded = true;
     frames = frames - aSample->mDiscardPadding;
   }