bug 1391482 move AudioBuffer parameter checking to constructor r?padenot draft
authorKarl Tomlinson <karlt+@karlt.net>
Wed, 16 Aug 2017 17:38:43 +1200
changeset 654566 108e46a68476c95af583b27e2030dd402dbe34bb
parent 654565 740dbfc1e33a1a00a59fc5795403e5894ad58dca
child 654567 aab2e830e91408b81a4ff079e2ede072696b68ec
push id76601
push userktomlinson@mozilla.com
push dateMon, 28 Aug 2017 23:35:34 +0000
reviewerspadenot
bugs1391482
milestone57.0a1
bug 1391482 move AudioBuffer parameter checking to constructor r?padenot for sharing with a new factory method in a future patch. MozReview-Commit-ID: LAtbRVttMh8
dom/media/webaudio/AudioBuffer.cpp
dom/media/webaudio/AudioBuffer.h
--- a/dom/media/webaudio/AudioBuffer.cpp
+++ b/dom/media/webaudio/AudioBuffer.cpp
@@ -154,20 +154,32 @@ AudioBufferMemoryTracker::CollectReports
     "Memory used by AudioBuffer objects (Web Audio).");
 
   return NS_OK;
 }
 
 AudioBuffer::AudioBuffer(nsPIDOMWindowInner* aWindow,
                          uint32_t aNumberOfChannels,
                          uint32_t aLength,
-                         float aSampleRate)
+                         float aSampleRate,
+                         ErrorResult& aRv)
   : mOwnerWindow(do_GetWeakReference(aWindow)),
     mSampleRate(aSampleRate)
 {
+  // Note that a buffer with zero channels is permitted here for the sake of
+  // AudioProcessingEvent, where channel counts must match parameters passed
+  // to createScriptProcessor(), one of which may be zero.
+  if (aSampleRate < WebAudioUtils::MinSampleRate ||
+      aSampleRate > WebAudioUtils::MaxSampleRate ||
+      aNumberOfChannels > WebAudioUtils::MaxChannelCount ||
+      !aLength || aLength > INT32_MAX) {
+    aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
+    return;
+  }
+
   mSharedChannels.mDuration = aLength;
   mJSChannels.SetLength(aNumberOfChannels);
   mozilla::HoldJSObjects(this);
   AudioBufferMemoryTracker::RegisterAudioBuffer(this);
 }
 
 AudioBuffer::~AudioBuffer()
 {
@@ -217,31 +229,22 @@ AudioBuffer::SetSharedChannels(
 /* static */ already_AddRefed<AudioBuffer>
 AudioBuffer::Create(nsPIDOMWindowInner* aWindow, uint32_t aNumberOfChannels,
                     uint32_t aLength, float aSampleRate,
                     already_AddRefed<ThreadSharedFloatArrayBufferList>
                       aInitialContents,
                     ErrorResult& aRv)
 {
   RefPtr<ThreadSharedFloatArrayBufferList> initialContents = aInitialContents;
-
-  // Note that a buffer with zero channels is permitted here for the sake of
-  // AudioProcessingEvent, where channel counts must match parameters passed
-  // to createScriptProcessor(), one of which may be zero.
-  if (aSampleRate < WebAudioUtils::MinSampleRate ||
-      aSampleRate > WebAudioUtils::MaxSampleRate ||
-      aNumberOfChannels > WebAudioUtils::MaxChannelCount ||
-      !aLength || aLength > INT32_MAX) {
-    aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
+  RefPtr<AudioBuffer> buffer =
+    new AudioBuffer(aWindow, aNumberOfChannels, aLength, aSampleRate, aRv);
+  if (aRv.Failed()) {
     return nullptr;
   }
 
-  RefPtr<AudioBuffer> buffer =
-    new AudioBuffer(aWindow, aNumberOfChannels, aLength, aSampleRate);
-
   if (initialContents) {
     MOZ_ASSERT(initialContents->GetChannels() == aNumberOfChannels);
     buffer->SetSharedChannels(initialContents.forget());
   }
 
   return buffer.forget();
 }
 
--- a/dom/media/webaudio/AudioBuffer.h
+++ b/dom/media/webaudio/AudioBuffer.h
@@ -108,17 +108,17 @@ public:
   /**
    * Returns a reference to an AudioChunk containing the sample data.
    * The AudioChunk can have a null buffer if there is no data.
    */
   const AudioChunk& GetThreadSharedChannelsForRate(JSContext* aContext);
 
 protected:
   AudioBuffer(nsPIDOMWindowInner* aWindow, uint32_t aNumberOfChannels,
-              uint32_t aLength, float aSampleRate);
+              uint32_t aLength, float aSampleRate, ErrorResult& aRv);
   ~AudioBuffer();
 
   void
   SetSharedChannels(already_AddRefed<ThreadSharedFloatArrayBufferList> aBuffer);
 
   bool RestoreJSChannelData(JSContext* aJSContext);
 
   already_AddRefed<ThreadSharedFloatArrayBufferList>