compiler: prevent accessing negative index of arrays

pull/1661/head
Henrixounez 2019-08-18 18:26:02 +02:00 committed by Alexander Medvednikov
parent 38db845378
commit 14c2319dcc
1 changed files with 4 additions and 0 deletions

View File

@ -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())