Don't add offsets to nullptr in GrGLOpsRenderPass

Change-Id: I54e4069134e66fee0bd85e96b5e9502a3c1fc7d9
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/279087
Reviewed-by: Mike Klein <mtklein@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Chris Dalton <csmartdalton@google.com>
diff --git a/src/gpu/gl/GrGLOpsRenderPass.cpp b/src/gpu/gl/GrGLOpsRenderPass.cpp
index 7ac48a5..260376e 100644
--- a/src/gpu/gl/GrGLOpsRenderPass.cpp
+++ b/src/gpu/gl/GrGLOpsRenderPass.cpp
@@ -134,10 +134,10 @@
     this->setupGeometry(fActiveVertexBuffer.get(), baseVertex, nullptr, 0);
     if (fGpu->glCaps().drawRangeElementsSupport()) {
         fGpu->drawRangeElements(fPrimitiveType, minIndexValue, maxIndexValue, indexCount,
-                                GR_GL_UNSIGNED_SHORT, fIndexPointer + baseIndex);
+                                GR_GL_UNSIGNED_SHORT, this->offsetForBaseIndex(baseIndex));
     } else {
         fGpu->drawElements(fPrimitiveType, indexCount, GR_GL_UNSIGNED_SHORT,
-                           fIndexPointer + baseIndex);
+                           this->offsetForBaseIndex(baseIndex));
     }
 }
 
@@ -159,7 +159,7 @@
         this->setupGeometry(fActiveVertexBuffer.get(), baseVertex, fActiveInstanceBuffer.get(),
                             baseInstance + i);
         fGpu->drawElementsInstanced(fPrimitiveType, indexCount, GR_GL_UNSIGNED_SHORT,
-                                    fIndexPointer + baseIndex,
+                                    this->offsetForBaseIndex(baseIndex),
                                     std::min(instanceCount - i, maxInstances));
     }
 }
diff --git a/src/gpu/gl/GrGLOpsRenderPass.h b/src/gpu/gl/GrGLOpsRenderPass.h
index 3d325b0..b186237 100644
--- a/src/gpu/gl/GrGLOpsRenderPass.h
+++ b/src/gpu/gl/GrGLOpsRenderPass.h
@@ -43,6 +43,14 @@
     void setupGeometry(const GrBuffer* vertexBuffer, int baseVertex, const GrBuffer* instanceBuffer,
                        int baseInstance);
 
+    const void* offsetForBaseIndex(int baseIndex) const {
+        if (!fIndexPointer) {
+            // nullptr != 0. Adding an offset to a nullptr is undefined.
+            return (void*)(baseIndex * sizeof(uint16_t));
+        }
+        return fIndexPointer + baseIndex;
+    }
+
     void onBegin() override;
     void onEnd() override;
     bool onBindPipeline(const GrProgramInfo& programInfo, const SkRect& drawBounds) override;