Mesa (master): ir_constant: Don't assert on out-of-bounds array accesses

Ian Romanick idr at kemper.freedesktop.org
Tue Aug 17 20:05:22 UTC 2010


Module: Mesa
Branch: master
Commit: 664364052f362af2789e6b0fa88b6a5ba66ba936
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=664364052f362af2789e6b0fa88b6a5ba66ba936

Author: Ian Romanick <ian.d.romanick at intel.com>
Date:   Tue Aug 17 12:57:28 2010 -0700

ir_constant: Don't assert on out-of-bounds array accesses

Several optimization paths, including constant folding, can lead to
accessing an ir_constant array with an out of bounds index.  The GLSL
spec lets us produce "undefined" results, but it does not let us
crash.

Fixes piglit test case glsl-array-bounds-01 and glsl-array-bounds-03.

---

 src/glsl/ir.cpp |   16 +++++++++++++++-
 1 files changed, 15 insertions(+), 1 deletions(-)

diff --git a/src/glsl/ir.cpp b/src/glsl/ir.cpp
index dd059e4..ebb5927 100644
--- a/src/glsl/ir.cpp
+++ b/src/glsl/ir.cpp
@@ -519,7 +519,21 @@ ir_constant *
 ir_constant::get_array_element(unsigned i) const
 {
    assert(this->type->is_array());
-   assert(i < this->type->length);
+
+   /* From page 35 (page 41 of the PDF) of the GLSL 1.20 spec:
+    *
+    *     "Behavior is undefined if a shader subscripts an array with an index
+    *     less than 0 or greater than or equal to the size the array was
+    *     declared with."
+    *
+    * Most out-of-bounds accesses are removed before things could get this far.
+    * There are cases where non-constant array index values can get constant
+    * folded.
+    */
+   if (int(i) < 0)
+      i = 0;
+   else if (i >= this->type->length)
+      i = this->type->length - 1;
 
    return array_elements[i];
 }




More information about the mesa-commit mailing list