From 14c2319dcca895c0669974b66e026aac3c29b16b Mon Sep 17 00:00:00 2001 From: Henrixounez Date: Sun, 18 Aug 2019 18:26:02 +0200 Subject: [PATCH] compiler: prevent accessing negative index of arrays --- compiler/parser.v | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compiler/parser.v b/compiler/parser.v index 6778669298..ffb93b92f1 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -1963,11 +1963,15 @@ fn (p mut Parser) index_expr(typ_ string, fn_ph int) string { } // expression inside [ ] if is_arr { + index_pos := p.cgen.cur_line.len T := p.table.find_type(p.expression()) // Allows only i8-64 and u8-64 to be used when accessing an array if T.parent != 'int' && T.parent != 'u32' { p.check_types(T.name, 'int') } + if p.cgen.cur_line.right(index_pos).replace(' ', '').int() < 0 { + p.error('cannot access negative array index') + } } else { T := p.table.find_type(p.expression())