Check bounds on TDArray
Bug: skia:14415
Change-Id: I23ba6f08e7fc6d2bbbb2ebd762984039cec4555a
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/724776
Commit-Queue: Herb Derby <herb@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
diff --git a/include/private/base/SkTDArray.h b/include/private/base/SkTDArray.h
index b08d285..b2d02c1 100644
--- a/include/private/base/SkTDArray.h
+++ b/include/private/base/SkTDArray.h
@@ -10,6 +10,7 @@
#include "include/private/base/SkAPI.h"
#include "include/private/base/SkAssert.h"
+#include "include/private/base/SkDebug.h"
#include "include/private/base/SkTo.h"
#include <algorithm>
@@ -152,20 +153,18 @@
const T* end() const { return this->data() + this->size(); }
T& operator[](int index) {
- SkASSERT(index < this->size());
- return this->data()[index];
+ return this->data()[this->checkIndex(index)];
}
const T& operator[](int index) const {
- SkASSERT(index < this->size());
- return this->data()[index];
+ return this->data()[this->checkIndex(index)];
}
const T& back() const {
- SkASSERT(this->size() > 0);
+ this->checkNotEmpty();
return this->data()[this->size() - 1];
}
T& back() {
- SkASSERT(this->size() > 0);
+ this->checkNotEmpty();
return this->data()[this->size() - 1];
}
@@ -228,6 +227,25 @@
}
private:
+ void checkNotEmpty() const {
+ if (this->empty()) SK_UNLIKELY {
+ SkUNREACHABLE;
+ }
+ }
+
+ int checkIndex(int i) const {
+ if (0 <= i && i < this->size()) SK_LIKELY {
+ return i;
+ } else SK_UNLIKELY {
+
+#if defined(SK_DEBUG)
+ sk_print_index_out_of_bounds(SkToSizeT(i), SkToSizeT(this->size()));
+#else
+ SkUNREACHABLE;
+#endif
+ }
+ }
+
SkTDStorage fStorage;
};