Bug 1313541 - Add WebGLBuffer::ValidateRange. - r=ethlin, a=gchang
authorJeff Gilbert (:jgilbert) <jgilbert@mozilla.com>
Tue, 11 Oct 2016 17:37:29 -0700
changeset 439976 f83d9fa111a18732b271b772575bc810b7d5d751
parent 439975 9ee2e73a7312b52ff928569441d1b67d6d12bff0
child 439977 9db4213007d93076c10f2fd954830344974185fb
push id36143
push userjkingston@mozilla.com
push dateWed, 16 Nov 2016 22:20:38 +0000
reviewersethlin, gchang
bugs1313541
milestone51.0a2
Bug 1313541 - Add WebGLBuffer::ValidateRange. - r=ethlin, a=gchang MozReview-Commit-ID: GtT1YppMzLV
dom/canvas/WebGLBuffer.cpp
dom/canvas/WebGLBuffer.h
--- a/dom/canvas/WebGLBuffer.cpp
+++ b/dom/canvas/WebGLBuffer.cpp
@@ -153,16 +153,35 @@ WebGLBuffer::BufferData(GLenum target, s
 
     // Warning: Possibly shared memory.  See bug 1225033.
     if (!ElementArrayCacheBufferData(data, size)) {
         mByteLength = 0;
         mContext->ErrorOutOfMemory("%s: Failed update index buffer cache.", funcName);
     }
 }
 
+bool
+WebGLBuffer::ValidateRange(const char* funcName, size_t byteOffset, size_t byteLen) const
+{
+    auto availLength = mByteLength;
+    if (byteOffset > availLength) {
+        mContext->ErrorInvalidValue("%s: Offset passes the end of the buffer.", funcName);
+        return false;
+    }
+    availLength -= byteOffset;
+
+    if (byteLen > availLength) {
+        mContext->ErrorInvalidValue("%s: Offset+size passes the end of the buffer.",
+                                    funcName);
+        return false;
+    }
+
+    return true;
+}
+
 ////////////////////////////////////////
 
 bool
 WebGLBuffer::ElementArrayCacheBufferData(const void* ptr,
                                          size_t bufferSizeInBytes)
 {
     if (mContent == Kind::ElementArray)
         return mCache->BufferData(ptr, bufferSizeInBytes);
--- a/dom/canvas/WebGLBuffer.h
+++ b/dom/canvas/WebGLBuffer.h
@@ -49,16 +49,17 @@ public:
     size_t ByteLength() const { return mByteLength; }
 
     bool ElementArrayCacheBufferData(const void* ptr, size_t bufferSizeInBytes);
 
     void ElementArrayCacheBufferSubData(size_t pos, const void* ptr,
                                         size_t updateSizeInBytes);
 
     bool Validate(GLenum type, uint32_t max_allowed, size_t first, size_t count) const;
+    bool ValidateRange(const char* funcName, size_t byteOffset, size_t byteLen) const;
 
     bool IsElementArrayUsedWithMultipleTypes() const;
 
     WebGLContext* GetParentObject() const {
         return mContext;
     }
 
     virtual JSObject* WrapObject(JSContext* cx, JS::Handle<JSObject*> givenProto) override;