1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

compiler: prevent accessing negative index of arrays

This commit is contained in:
Henrixounez 2019-08-18 18:26:02 +02:00 committed by Alexander Medvednikov
parent 38db845378
commit 14c2319dcc

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