Skip to content

Commit 1a0ab15

Browse files
authored
Merge pull request #302 from boriel/bugfix/array_check_warning
Fix static array boundary check not always emitted
2 parents e4d1561 + b4864b0 commit 1a0ab15

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed

symbols/arrayaccess.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,16 @@ def make_node(cls, id_, arglist, lineno):
119119
btype = gl.SYMBOL_TABLE.basic_types[gl.BOUND_TYPE]
120120
for i, b in zip(arglist, variable.bounds):
121121
lower_bound = NUMBER(b.lower, type_=btype, lineno=lineno)
122+
123+
if is_number(i.value) or is_const(i.value):
124+
val = i.value.value
125+
if val < b.lower or val > b.upper:
126+
warning(lineno, "Array '%s' subscript out of range" % id_)
127+
122128
i.value = BINARY.make_node('MINUS',
123129
TYPECAST.make_node(btype, i.value, lineno),
124130
lower_bound, lineno, func=lambda x, y: x - y,
125131
type_=btype)
126-
if is_number(i.value) or is_const(i.value):
127-
val = i.value.value
128-
if val < 0 or val > b.count:
129-
warning(lineno, "Array '%s' subscript out of range" % id_)
130132
else:
131133
btype = gl.SYMBOL_TABLE.basic_types[gl.BOUND_TYPE]
132134
for arg in arglist:
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
org 32768
2+
__START_PROGRAM:
3+
di
4+
push ix
5+
push iy
6+
exx
7+
push hl
8+
exx
9+
ld hl, 0
10+
add hl, sp
11+
ld (__CALL_BACK__), hl
12+
ei
13+
ld a, (_aux.__DATA__ + 4)
14+
ld (_c), a
15+
ld a, (_aux1.__DATA__ + -1)
16+
ld (_c), a
17+
ld (0), a
18+
ld hl, 0
19+
ld b, h
20+
ld c, l
21+
__END_PROGRAM:
22+
di
23+
ld hl, (__CALL_BACK__)
24+
ld sp, hl
25+
exx
26+
pop hl
27+
exx
28+
pop iy
29+
pop ix
30+
ei
31+
ret
32+
__CALL_BACK__:
33+
DEFW 0
34+
ZXBASIC_USER_DATA:
35+
_c:
36+
DEFB 00
37+
_aux:
38+
DEFW __LABEL0
39+
_aux.__DATA__.__PTR__:
40+
DEFW _aux.__DATA__
41+
_aux.__DATA__:
42+
DEFB 00h
43+
DEFB 00h
44+
DEFB 00h
45+
DEFB 00h
46+
__LABEL0:
47+
DEFW 0000h
48+
DEFB 01h
49+
_aux1:
50+
DEFW __LABEL1
51+
_aux1.__DATA__.__PTR__:
52+
DEFW _aux1.__DATA__
53+
_aux1.__DATA__:
54+
DEFB 00h
55+
DEFB 00h
56+
DEFB 00h
57+
__LABEL1:
58+
DEFW 0000h
59+
DEFB 01h
60+
; Defines DATA END --> HEAP size is 0
61+
ZXBASIC_USER_DATA_END:
62+
; Defines USER DATA Length in bytes
63+
ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA
64+
END
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
dim c as Ubyte
2+
dim aux (0 TO 3) as ubyte
3+
let c = aux(4)
4+
5+
dim aux1(1 TO 3) as ubyte
6+
let c = aux1(0)
7+
poke 0, c ' Use c to avoid warning
8+

tests/functional/test_errmsg.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,6 @@ array_err.bas:2: Mismatched vector size. Expected 11 elements, got 1.
145145
pararray2.bas:8: Array q type does not match parameter type
146146
>>> process_file('pararray4.bas')
147147
pararray4.bas:8: Array q type does not match parameter type
148-
148+
>>> process_file('array_check_warn.bas')
149+
array_check_warn.bas:3: warning: Array 'aux' subscript out of range
150+
array_check_warn.bas:6: warning: Array 'aux1' subscript out of range

0 commit comments

Comments
 (0)