Skip to content

Commit 386135e

Browse files
committed
Fix guarding condition to avoid reading out of bound values in accessing M table variables
1 parent fb56155 commit 386135e

3 files changed

Lines changed: 4 additions & 4 deletions

File tree

examples/java/src/com/mlang/MValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ static MValue m_array_index(MValue[] array, int tableStart, MValue index, int si
276276

277277
if (indexInteger < 0) {
278278
return zero;
279-
} else if (indexInteger > size) {
279+
} else if (indexInteger >= size) {
280280
return mUndefined;
281281
} else {
282282
return array[tableStart + indexInteger];

examples/ocaml/mvalue.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ let m_table_value_at_index (variable_array : m_array) (table_start : int)
114114
let offset = int_of_float index.value in
115115
match offset with
116116
| x when x < 0 -> m_zero
117-
| x when x > size -> m_undef
117+
| x when x >= size -> m_undef
118118
| _ -> Array.get variable_array (offset + table_start)
119119

120120
let m_max (x : m_value) (y : m_value) : m_value =

src/mlang/backend_compilers/bir_to_dgfip_c.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,8 @@ let rec generate_c_expr (e : expression Pos.marked)
284284
Dand
285285
( Dand
286286
( Dvar (Local idx_var, Def),
287-
Dbinop
288-
("<=", Dvar (Local idx_var, Val), Dlit (float_of_int size)) ),
287+
Dbinop ("<", Dvar (Local idx_var, Val), Dlit (float_of_int size))
288+
),
289289
Daccess (Pos.unmark var, Def, idx_var) )
290290
in
291291
let value_comp =

0 commit comments

Comments
 (0)