fix array bounds check.
authorMario de Sousa <msousa@fe.up.pt>
Mon, 18 Jun 2012 15:52:09 +0100
changeset 599 60f3edcf6a8f
parent 598 0b1ee7e7123b
child 600 f5d4e9f91513
fix array bounds check.
stage3/array_range_check.cc
--- a/stage3/array_range_check.cc	Mon Jun 18 15:37:37 2012 +0100
+++ b/stage3/array_range_check.cc	Mon Jun 18 15:52:09 2012 +0100
@@ -119,24 +119,42 @@
     /* mismatch between number of indexes/subscripts. This error will be caught in check_dimension_count() so we ignore it. */
     if (NULL == dimension) 
       return;
-    
+
+    /* Check lower limit */
     if ( VALID_CVALUE( int64, l->elements[i]) && VALID_CVALUE( int64, dimension->lower_limit))
       if ( GET_CVALUE( int64, l->elements[i])   <  GET_CVALUE( int64, dimension->lower_limit))
       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
 
+    if ( VALID_CVALUE( int64, l->elements[i]) && VALID_CVALUE(uint64, dimension->lower_limit))
+      if ( GET_CVALUE( int64, l->elements[i])   <  GET_CVALUE(uint64, dimension->lower_limit))
+      {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
+
+    if ( VALID_CVALUE(uint64, l->elements[i]) && VALID_CVALUE(uint64, dimension->lower_limit))
+      if ( GET_CVALUE(uint64, l->elements[i])   <  GET_CVALUE(uint64, dimension->lower_limit))
+      {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
+
+    if ( VALID_CVALUE(uint64, l->elements[i]) && VALID_CVALUE( int64, dimension->lower_limit))
+      if ( GET_CVALUE(uint64, l->elements[i])   <  GET_CVALUE( int64, dimension->lower_limit))
+      {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
+
+    /* Repeat the same check, now for upper limit */
     if ( VALID_CVALUE( int64, l->elements[i]) && VALID_CVALUE( int64, dimension->upper_limit))
       if ( GET_CVALUE( int64, l->elements[i])   >  GET_CVALUE( int64, dimension->upper_limit))
       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
 
-    if ( VALID_CVALUE(uint64, l->elements[i]) && VALID_CVALUE(uint64, dimension->lower_limit))
-      if ( GET_CVALUE(uint64, l->elements[i])   <  GET_CVALUE(uint64, dimension->lower_limit))
+    if ( VALID_CVALUE( int64, l->elements[i]) && VALID_CVALUE(uint64, dimension->upper_limit))
+      if ( GET_CVALUE( int64, l->elements[i])   >  GET_CVALUE(uint64, dimension->upper_limit))
       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
 
     if ( VALID_CVALUE(uint64, l->elements[i]) && VALID_CVALUE(uint64, dimension->upper_limit))
       if ( GET_CVALUE(uint64, l->elements[i])   >  GET_CVALUE(uint64, dimension->upper_limit))
       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
       
-    /* TODO: what happens when one has a int64 cvalue, and another has a uint64 cvalue? */
+    if ( VALID_CVALUE(uint64, l->elements[i]) && VALID_CVALUE( int64, dimension->upper_limit))
+      if ( GET_CVALUE(uint64, l->elements[i])   >  GET_CVALUE( int64, dimension->upper_limit))
+      {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
+      
+   /* TODO: How do we make these comparisons between int and unsigned int, without the compiler complaining? */
   }
 }