Skip to content

Commit fbbd94c

Browse files
committed
Analyser: fix crash bug on pointer dereference
* fix TODO `assert(0)` in `getUnaryOpWidth()`: compute actual width * add regression tests Fixes #302
1 parent d5d7f24 commit fbbd94c

File tree

2 files changed

+105
-4
lines changed

2 files changed

+105
-4
lines changed

analyser/conversion_checker_expr.c2

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fn ExprWidth getExprWidth(const Expr* e) {
9898
QualType qt = e.getType();
9999
qt = qt.getCanonicalType();
100100
if (qt.isPointer()) {
101-
result.width = 64; // TODO or 32
101+
result.width = cast<u8>(ast.getWordSize() * 8);
102102
result.is_signed = false;
103103
return result;
104104
}
@@ -156,9 +156,18 @@ fn ExprWidth getUnaryOpWidth(const UnaryOperator* u) {
156156
w.is_signed = false;
157157
break;
158158
case Deref:
159-
// must be pointer-type, return size of inner
160-
// TODO
161-
assert(0);
159+
Expr* e = cast<Expr*>(u);
160+
QualType qt = e.getType();
161+
qt = qt.getCanonicalType();
162+
if (qt.isPointer()) {
163+
w.width = cast<u8>(ast.getWordSize() * 8);
164+
w.is_signed = false;
165+
break;
166+
}
167+
assert(qt.isBuiltin());
168+
BuiltinType* bi = qt.getBuiltin();
169+
w.width = cast<u8>(bi.getWidth());
170+
w.is_signed = bi.isSigned();
162171
break;
163172
case Minus:
164173
w = getExprWidth(u.getInner());
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// @warnings{no-unused}
2+
module test;
3+
4+
// regression test for #302
5+
fn void test1(const u8* p) {
6+
i32 i = *p++;
7+
}
8+
9+
fn u8 get_digit(const char *p) {
10+
u8 digit = *p;
11+
u8 digit0 = '0';
12+
return digit - digit0;
13+
}
14+
15+
fn u8 get_digit_(const char *p) {
16+
u8 digit0 = '0';
17+
return *p - digit0;
18+
}
19+
20+
fn u8 get_digit_1(const char *p) {
21+
u8 digit0 = '0';
22+
return (u8)*p - digit0;
23+
}
24+
25+
fn u8 get_digit__(const char *p) {
26+
u8 digit0 = '0';
27+
return *p++ - digit0;
28+
}
29+
30+
fn u8 get_digit0(const char *p) {
31+
u8 digit = *p;
32+
return digit - '0';
33+
}
34+
35+
fn u8 get_digit0_(const char *p) {
36+
u8 digit = *p++;
37+
return digit - '0';
38+
}
39+
40+
fn u8 get_digit1(const char *p) {
41+
u8 digit;
42+
digit = *p - '0';
43+
return digit;
44+
}
45+
46+
fn u8 get_digit2(const char *p) {
47+
u8 digit;
48+
digit = *p++ - '0';
49+
return digit;
50+
}
51+
52+
fn u8 get_digit3(const char *p) {
53+
u8 digit = *p - '0';
54+
return digit;
55+
}
56+
57+
fn u8 get_digit4(const char *p) {
58+
u8 digit = *p++ - '0';
59+
return digit;
60+
}
61+
62+
fn u8 get_digit_a(const char *p) {
63+
u8 digit = p[0];
64+
u8 digit0 = '0';
65+
return digit - digit0;
66+
}
67+
68+
fn u8 get_digit__a(const char *p) {
69+
u8 digit0 = '0';
70+
return p[0] - digit0;
71+
}
72+
73+
fn u8 get_digit_1_a(const char *p) {
74+
u8 digit0 = '0';
75+
return (u8)p[0] - digit0;
76+
}
77+
78+
fn u8 get_digit0_a(const char *p) {
79+
u8 digit = p[0];
80+
return digit - '0';
81+
}
82+
83+
fn u8 get_digit1_a(const char *p) {
84+
u8 digit;
85+
digit = p[0] - '0';
86+
return digit;
87+
}
88+
89+
fn u8 get_digit3_a(const char *p) {
90+
u8 digit = p[0] - '0';
91+
return digit;
92+
}

0 commit comments

Comments
 (0)