Guard against out-of-bounds dims accesses, even in optimized, no-asserts
authorA. Unique TensorFlower <gardener@tensorflow.org>
Wed, 4 Apr 2018 18:15:56 +0000 (11:15 -0700)
committerTensorFlower Gardener <gardener@tensorflow.org>
Wed, 4 Apr 2018 18:18:50 +0000 (11:18 -0700)
builds.

PiperOrigin-RevId: 191617948

tensorflow/contrib/lite/toco/model.h

index 64269d3..9bd72e7 100644 (file)
@@ -1507,7 +1507,14 @@ class Shape {
 
   // We still have that one convenience accessor to avoid
   // the awkward double bracket issue:  shape.dims()[i].
-  int dims(int i) const { return dims_[i]; }
+  int dims(int i) const {
+    // Always check for out-of-bounds accesses, even in optimized builds where
+    // standard assertions are disabled. Out-of-bounds access here is a common
+    // occurence.
+    CHECK_GE(i, 0);
+    CHECK_GT(dims_.size(), i);
+    return dims_[i];
+  }
 
   bool operator==(const Shape& comp) const {
     return (this->dims_ == comp.dims());