Skip to content

Commit d6f7562

Browse files
committed
Overflow checks: move cConstIntExprRanged() to parser-decl-symbtype.bas
1 parent cc1dc09 commit d6f7562

File tree

3 files changed

+74
-69
lines changed

3 files changed

+74
-69
lines changed

src/compiler/parser-compound-select-const.bas

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -181,75 +181,6 @@ private function hSelConstAddCase _
181181
function = TRUE
182182
end function
183183

184-
''
185-
type RANGEVALUES
186-
as longint smin
187-
as longint smax
188-
as ulongint umax
189-
end type
190-
191-
''
192-
function cConstIntExprRanged _
193-
( _
194-
byval expr as ASTNODE ptr, _
195-
byval todtype as integer _
196-
) as longint
197-
198-
static range( FB_SIZETYPE_BOOLEAN to FB_SIZETYPE_UINT64 ) as RANGEVALUES = _
199-
{ _
200-
/' FB_SIZETYPE_BOOLEAN '/ ( -1, 0, 0 ), _
201-
/' FB_SIZETYPE_INT8 '/ ( -128, 127, 127 ), _
202-
/' FB_SIZETYPE_UINT8 '/ ( 0, 127, 255 ), _
203-
/' FB_SIZETYPE_INT16 '/ ( -32768, 32767, 32767 ), _
204-
/' FB_SIZETYPE_UINT16 '/ ( 0, 32767, 65535 ), _
205-
/' FB_SIZETYPE_INT32 '/ ( -2147483648ll, 2147483647ll, 2147483647ll ), _
206-
/' FB_SIZETYPE_UINT32 '/ ( 0, 2147483647ll, 4294967295ll ), _
207-
/' FB_SIZETYPE_INT64 '/ ( -9223372036854775808ull, 9223372036854775807ull, 9223372036854775807ull ), _
208-
/' FB_SIZETYPE_UINT64 '/ ( 0, 9223372036854775807ull, 18446744073709551615ull ) _
209-
}
210-
211-
dim as longint value = any
212-
dim as integer dtype = any
213-
dim as integer result = any
214-
dim as RANGEVALUES ptr r = any
215-
216-
r = @range( typeGetSizeType( todtype ) )
217-
218-
dtype = astGetDataType( expr )
219-
220-
'' cConstIntExpr() will flush expr
221-
value = cConstIntExpr( expr, FB_DATATYPE_LONGINT )
222-
223-
if( typeIsSigned( dtype ) ) then
224-
if( typeIsSigned( todtype ) ) then
225-
result = (value >= r->smin) and (value <= r->smax)
226-
else
227-
if( dtype = FB_DATATYPE_LONGINT and todtype = FB_DATATYPE_ULONGINT ) then
228-
result = (value >= 0) and (culngint(value) <= culngint(r->smax))
229-
else
230-
result = (value >= 0) and (culngint(value) <= culngint(r->umax))
231-
end if
232-
endif
233-
else
234-
if( typeIsSigned( todtype ) ) then
235-
if( dtype = FB_DATATYPE_ULONGINT and todtype = FB_DATATYPE_LONGINT ) then
236-
result = (culngint(value) <= culngint(r->smax))
237-
else
238-
result = (culngint(value) <= culngint(r->umax))
239-
end if
240-
else
241-
result = (culngint(value) <= r->umax)
242-
endif
243-
end if
244-
245-
if( not result ) then
246-
errReportWarn( FB_WARNINGMSG_CONVOVERFLOW )
247-
end if
248-
249-
function = value
250-
251-
end function
252-
253184
'' cSelConstStmtNext = CASE (ELSE | (ConstExpression{int} (',' ConstExpression{int})*)) .
254185
sub cSelConstStmtNext( byval stk as FB_CMPSTMTSTK ptr )
255186
'' CASE

src/compiler/parser-decl-symbtype.bas

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,75 @@ function cConstIntExpr _
2929
function = astConstFlushToInt( expr, dtype )
3030
end function
3131

32+
''
33+
type RANGEVALUES
34+
as longint smin
35+
as longint smax
36+
as ulongint umax
37+
end type
38+
39+
''
40+
function cConstIntExprRanged _
41+
( _
42+
byval expr as ASTNODE ptr, _
43+
byval todtype as integer _
44+
) as longint
45+
46+
static range( FB_SIZETYPE_BOOLEAN to FB_SIZETYPE_UINT64 ) as RANGEVALUES = _
47+
{ _
48+
/' FB_SIZETYPE_BOOLEAN '/ ( -1, 0, 0 ), _
49+
/' FB_SIZETYPE_INT8 '/ ( -128, 127, 127 ), _
50+
/' FB_SIZETYPE_UINT8 '/ ( 0, 127, 255 ), _
51+
/' FB_SIZETYPE_INT16 '/ ( -32768, 32767, 32767 ), _
52+
/' FB_SIZETYPE_UINT16 '/ ( 0, 32767, 65535 ), _
53+
/' FB_SIZETYPE_INT32 '/ ( -2147483648ll, 2147483647ll, 2147483647ll ), _
54+
/' FB_SIZETYPE_UINT32 '/ ( 0, 2147483647ll, 4294967295ll ), _
55+
/' FB_SIZETYPE_INT64 '/ ( -9223372036854775808ull, 9223372036854775807ull, 9223372036854775807ull ), _
56+
/' FB_SIZETYPE_UINT64 '/ ( 0, 9223372036854775807ull, 18446744073709551615ull ) _
57+
}
58+
59+
dim as longint value = any
60+
dim as integer dtype = any
61+
dim as integer result = any
62+
dim as RANGEVALUES ptr r = any
63+
64+
r = @range( typeGetSizeType( todtype ) )
65+
66+
dtype = astGetDataType( expr )
67+
68+
'' cConstIntExpr() will flush expr
69+
value = cConstIntExpr( expr, FB_DATATYPE_LONGINT )
70+
71+
if( typeIsSigned( dtype ) ) then
72+
if( typeIsSigned( todtype ) ) then
73+
result = (value >= r->smin) and (value <= r->smax)
74+
else
75+
if( dtype = FB_DATATYPE_LONGINT and todtype = FB_DATATYPE_ULONGINT ) then
76+
result = (value >= 0) and (culngint(value) <= culngint(r->smax))
77+
else
78+
result = (value >= 0) and (culngint(value) <= culngint(r->umax))
79+
end if
80+
endif
81+
else
82+
if( typeIsSigned( todtype ) ) then
83+
if( dtype = FB_DATATYPE_ULONGINT and todtype = FB_DATATYPE_LONGINT ) then
84+
result = (culngint(value) <= culngint(r->smax))
85+
else
86+
result = (culngint(value) <= culngint(r->umax))
87+
end if
88+
else
89+
result = (culngint(value) <= r->umax)
90+
endif
91+
end if
92+
93+
if( not result ) then
94+
errReportWarn( FB_WARNINGMSG_CONVOVERFLOW )
95+
end if
96+
97+
function = value
98+
99+
end function
100+
32101
private function cSymbolTypeFuncPtr( byval is_func as integer ) as FBSYMBOL ptr
33102
dim as integer dtype = any, mode = any, attrib = any
34103
dim as FBSYMBOL ptr proc = any, subtype = any

src/compiler/parser.bi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,11 @@ declare function cConstIntExpr _
801801
byval expr as ASTNODE ptr, _
802802
byval dtype as integer = FB_DATATYPE_INTEGER _
803803
) as longint
804+
declare function cConstIntExprRanged _
805+
( _
806+
byval expr as ASTNODE ptr, _
807+
byval dtype as integer = FB_DATATYPE_INTEGER _
808+
) as longint
804809
declare function cOperatorNew( ) as ASTNODE ptr
805810
declare sub cOperatorDelete( )
806811

0 commit comments

Comments
 (0)