Skip to content

Commit 08fff05

Browse files
committed
fbc: sf.net # 994: Bad handling of macro argument evaluation
- reset file pointers after evaluating arguments in __fb_query_symbol__, __fb_eval__, __fb_arg_extract__, __fb_iif__ - Some fbc built in macros like create a separate lexer context to evaluate arguments at compile time. - When the lexer context is no longer needed, the previous lexer context needs to be restored. - fbc was failing to correctly restore file pointers after freeing the context - this would result in the file pointers being not synchronized with the memory buffers and memory pointers - this would happen when the macros and arguments to be expanded sit on either side of an 8Kb boundary - 8KB is the hard coded buffer size for reading in chunks of source code
1 parent c9a282e commit 08fff05

File tree

11 files changed

+145
-3
lines changed

11 files changed

+145
-3
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Version 1.10.1
2323
- rtlib: emscripten: CURDIR and EXEPATH always returned ""
2424
- emscripten/fbc: -sASYNCIFY wasn't passed to emcc at link-time as needed by recent Emscripten
2525
- sf.net #993: ignore (parse) single line comments for macros invoked with optional parentheses instead of passing as an argument to macro
26+
- sf.net #994: Bad handling of macro argument evaluation - reset file pointers after evaluating arguments in __fb_query_symbol__, __fb_eval__, __fb_arg_extract__, __fb_iif__
2627

2728

2829
Version 1.10.0

src/compiler/lex-utf.bas

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ end function
300300
private function hUTF16LEToUTF16LE( ) as integer static
301301

302302
if( get( #env.inf.num, , lex.ctx->buffw ) = 0 ) then
303-
function = cunsg(seek( env.inf.num ) - lex.ctx->filepos) \ len( ushort )
303+
lex.ctx->physfilepos = seek( env.inf.num )
304+
function = cunsg(lex.ctx->physfilepos - lex.ctx->filepos) \ len( ushort )
304305
else
305306
function = 0
306307
end if
@@ -600,7 +601,8 @@ end function
600601
private function hUTF32LEToUTF32LE( ) as integer static
601602

602603
if( get( #env.inf.num, , lex.ctx->buffw ) = 0 ) then
603-
function = cunsg(seek( env.inf.num ) - lex.ctx->filepos) \ sizeof( ulong )
604+
lex.ctx->physfilepos = seek( env.inf.num )
605+
function = cunsg(lex.ctx->physfilepos - lex.ctx->filepos) \ sizeof( ulong )
604606
else
605607
function = 0
606608
end if

src/compiler/lex.bas

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ sub lexPopCtx( )
6060
DWstrAllocate( lex.ctx->deftextw, 0 )
6161
end if
6262

63+
'' restore file pointer position if current context is different from previous
64+
if( (lex.ctx-1)->physfilepos > 0 ) then
65+
if( (lex.ctx-1)->physfilepos <> lex.ctx->physfilepos ) then
66+
seek #env.inf.num, lex.ctx->physfilepos
67+
end if
68+
end if
69+
6370
lex.ctx -= 1
6471

6572
end sub
@@ -133,9 +140,11 @@ sub lexInit _
133140
if( is_fb_eval ) then
134141
lex.ctx->filepos = (lex.ctx-1)->filepos
135142
lex.ctx->lastfilepos = (lex.ctx-1)->lastfilepos
143+
lex.ctx->physfilepos = (lex.ctx-1)->physfilepos
136144
else
137145
lex.ctx->filepos = 0
138146
lex.ctx->lastfilepos = 0
147+
lex.ctx->physfilepos = 0
139148
end if
140149

141150
'' only if it's not on an inc file
@@ -227,7 +236,8 @@ private function hReadChar _
227236
select case as const env.inf.format
228237
case FBFILE_FORMAT_ASCII
229238
if( get( #env.inf.num, , lex.ctx->buff ) = 0 ) then
230-
lex.ctx->bufflen = seek( env.inf.num ) - lex.ctx->filepos
239+
lex.ctx->physfilepos = seek( env.inf.num )
240+
lex.ctx->bufflen = lex.ctx->physfilepos - lex.ctx->filepos
231241
lex.ctx->buffptr = @lex.ctx->buff
232242
end if
233243

src/compiler/lex.bi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ type LEX_TKCTX
129129

130130
filepos as integer
131131
lastfilepos as integer
132+
physfilepos as integer
132133

133134
currline as DZSTRING '' current line in text form
134135

src/compiler/symb-define.bas

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,10 @@ private function hDefQuerySymZ_cb( byval argtb as LEXPP_ARGTB ptr, byval errnum
10411041
lex.ctx->defptr = lex.ctx->deftext.data
10421042
lex.ctx->deflen += len( *sexpr )
10431043

1044+
DZstrConcatAssign( lex.ctx->deftext, LFCHAR )
1045+
lex.ctx->defptr = lex.ctx->deftext.data
1046+
lex.ctx->deflen += len( LFCHAR )
1047+
10441048
'' if filtervalue is zero then set the default methods to use for
10451049
'' look-up depending on what we are looking for
10461050
if( filtervalue = 0 ) then
@@ -1073,6 +1077,9 @@ private function hDefQuerySymZ_cb( byval argtb as LEXPP_ARGTB ptr, byval errnum
10731077
FB_TKCLASS_QUIRKWD, FB_TKCLASS_OPERATOR
10741078

10751079
sym = cIdentifierOrUDTMember( )
1080+
if( sym = NULL ) then
1081+
retry = TRUE
1082+
end if
10761083
end select
10771084

10781085
'' for some symbols, we maybe want to reset and try a TYPEOF below
@@ -1129,6 +1136,10 @@ private function hDefQuerySymZ_cb( byval argtb as LEXPP_ARGTB ptr, byval errnum
11291136
DZstrAssign( lex.ctx->deftext, *sexpr )
11301137
lex.ctx->defptr = lex.ctx->deftext.data
11311138
lex.ctx->deflen += len( *sexpr )
1139+
1140+
DZstrConcatAssign( lex.ctx->deftext, LFCHAR )
1141+
lex.ctx->defptr = lex.ctx->deftext.data
1142+
lex.ctx->deflen += len( LFCHAR )
11321143
end if
11331144

11341145
end if
@@ -1183,6 +1194,12 @@ private function hDefQuerySymZ_cb( byval argtb as LEXPP_ARGTB ptr, byval errnum
11831194
res = str( -1 )
11841195
end select
11851196

1197+
if( *errnum <> FB_ERRMSG_OK ) then
1198+
errReport( *errnum )
1199+
'' error recovery: skip until next line (in the buffer)
1200+
hSkipUntil( FB_TK_EOL, TRUE )
1201+
end if
1202+
11861203
lex.ctx->reclevel -= 1
11871204

11881205
lexPopCtx()
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
const fbc_FB_QUERY_SYMBOL_symbclass = 0
2+
#macro SomeLongishExpressionToHitTheBadSpot(longish_argument_name)
3+
longish_argument_name
4+
#endmacro
5+
'' Padding to fill the source file to 8Kb---------------------------------------
6+
'' Padding to fill the source file to 8Kb---------------------------------------
7+
'' Padding to fill the source file to 8Kb---------------------------------------
8+
'' Padding to fill the source file to 8Kb---------------------------------------
9+
'' Padding to fill the source file to 8Kb---------------------------------------
10+
'' Padding to fill the source file to 8Kb---------------------------------------
11+
'' Padding to fill the source file to 8Kb---------------------------------------
12+
'' Padding to fill the source file to 8Kb---------------------------------------
13+
'' Padding to fill the source file to 8Kb---------------------------------------
14+
'' Padding to fill the source file to 8Kb---------------------------------------
15+
'' Padding to fill the source file to 8Kb---------------------------------------
16+
'' Padding to fill the source file to 8Kb---------------------------------------
17+
'' Padding to fill the source file to 8Kb---------------------------------------
18+
'' Padding to fill the source file to 8Kb---------------------------------------
19+
'' Padding to fill the source file to 8Kb---------------------------------------
20+
'' Padding to fill the source file to 8Kb---------------------------------------
21+
'' Padding to fill the source file to 8Kb---------------------------------------
22+
'' Padding to fill the source file to 8Kb---------------------------------------
23+
'' Padding to fill the source file to 8Kb---------------------------------------
24+
'' Padding to fill the source file to 8Kb---------------------------------------
25+
'' Padding to fill the source file to 8Kb---------------------------------------
26+
'' Padding to fill the source file to 8Kb---------------------------------------
27+
'' Padding to fill the source file to 8Kb---------------------------------------
28+
'' Padding to fill the source file to 8Kb---------------------------------------
29+
'' Padding to fill the source file to 8Kb---------------------------------------
30+
'' Padding to fill the source file to 8Kb---------------------------------------
31+
'' Padding to fill the source file to 8Kb---------------------------------------
32+
'' Padding to fill the source file to 8Kb---------------------------------------
33+
'' Padding to fill the source file to 8Kb---------------------------------------
34+
'' Padding to fill the source file to 8Kb---------------------------------------
35+
'' Padding to fill the source file to 8Kb---------------------------------------
36+
'' Padding to fill the source file to 8Kb---------------------------------------
37+
'' Padding to fill the source file to 8Kb---------------------------------------
38+
'' Padding to fill the source file to 8Kb---------------------------------------
39+
'' Padding to fill the source file to 8Kb---------------------------------------
40+
'' Padding to fill the source file to 8Kb---------------------------------------
41+
'' Padding to fill the source file to 8Kb---------------------------------------
42+
'' Padding to fill the source file to 8Kb---------------------------------------
43+
'' Padding to fill the source file to 8Kb---------------------------------------
44+
'' Padding to fill the source file to 8Kb---------------------------------------
45+
'' Padding to fill the source file to 8Kb---------------------------------------
46+
'' Padding to fill the source file to 8Kb---------------------------------------
47+
'' Padding to fill the source file to 8Kb---------------------------------------
48+
'' Padding to fill the source file to 8Kb---------------------------------------
49+
'' Padding to fill the source file to 8Kb---------------------------------------
50+
'' Padding to fill the source file to 8Kb---------------------------------------
51+
'' Padding to fill the source file to 8Kb---------------------------------------
52+
'' Padding to fill the source file to 8Kb---------------------------------------
53+
'' Padding to fill the source file to 8Kb---------------------------------------
54+
'' Padding to fill the source file to 8Kb---------------------------------------
55+
'' Padding to fill the source file to 8Kb---------------------------------------
56+
'' Padding to fill the source file to 8Kb---------------------------------------
57+
'' Padding to fill the source file to 8Kb---------------------------------------
58+
'' Padding to fill the source file to 8Kb---------------------------------------
59+
'' Padding to fill the source file to 8Kb---------------------------------------
60+
'' Padding to fill the source file to 8Kb---------------------------------------
61+
'' Padding to fill the source file to 8Kb---------------------------------------
62+
'' Padding to fill the source file to 8Kb---------------------------------------
63+
'' Padding to fill the source file to 8Kb---------------------------------------
64+
'' Padding to fill the source file to 8Kb---------------------------------------
65+
'' Padding to fill the source file to 8Kb---------------------------------------
66+
'' Padding to fill the source file to 8Kb---------------------------------------
67+
'' Padding to fill the source file to 8Kb---------------------------------------
68+
'' Padding to fill the source file to 8Kb---------------------------------------
69+
'' Padding to fill the source file to 8Kb---------------------------------------
70+
'' Padding to fill the source file to 8Kb---------------------------------------
71+
'' Padding to fill the source file to 8Kb---------------------------------------
72+
'' Padding to fill the source file to 8Kb---------------------------------------
73+
'' Padding to fill the source file to 8Kb---------------------------------------
74+
'' Padding to fill the source file to 8Kb---------------------------------------
75+
'' Padding to fill the source file to 8Kb---------------------------------------
76+
'' Padding to fill the source file to 8Kb---------------------------------------
77+
'' Padding to fill the source file to 8Kb---------------------------------------
78+
'' Padding to fill the source file to 8Kb---------------------------------------
79+
'' Padding to fill the source file to 8Kb---------------------------------------
80+
'' Padding to fill the source file to 8Kb---------------------------------------
81+
'' Padding to fill the source file to 8Kb---------------------------------------
82+
'' Padding to fill the source file to 8Kb---------------------------------------
83+
'' Padding to fill the source file to 8Kb---------------------------------------
84+
'' Padding to fill the source file to 8Kb---------------------------------------
85+
'' Padding to fill the source file to 8Kb---------------------------------------
86+
'' Padding to fill the source file to 8Kb---------------------------------------
87+
'' Padding to fill the source file to 8Kb---------------------------------------
88+
'' Padding to fill the source file to 8Kb---------------------------------------
89+
'' Padding to fill the source file to 8Kb---------------------------------------
90+
'' Padding to fill the source file to 8Kb---------------------------------------
91+
'' Padding to fill the source file to 8Kb---------------------------------------
92+
'' Padding to fill the source file to 8Kb---------------------------------------
93+
'' Padding to fill the source file to 8Kb---------------------------------------
94+
'' Padding to fill the source file to 8Kb---------------------------------------
95+
'' Padding to fill the source file to 8Kb---------------------------------------
96+
'' Padding to fill the source file to 8Kb---------------------------------------
97+
'' Padding to fill the source file to 8Kb---------------------------------------
98+
'' That should be close enough
99+
print __FB_QUERY_SYMBOL__( _
100+
SomeLongishExpressionToHitTheBadSpot( fbc_FB_QUERY_SYMBOL_symbclass ), _
101+
SomeUndefinedSymbol )
102+
print "This is the last lines of the file - This is the last lines of the file"
103+
print "This is the last lines of the file - This is the last lines of the file"
104+
print "This is the last lines of the file - This is the last lines of the file"
105+
print "This is the last lines of the file - This is the last lines of the file"
106+
print "This is the last lines of the file - This is the last lines of the file"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fb_query_symbol-error-1.bas(101) error 42: Variable not declared, SomeUndefinedSymbol in 'SomeUndefinedSymbol )'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fb_query_symbol-error-1.bas(101) error 42: Variable not declared, SomeUndefinedSymbol in 'SomeUndefinedSymbol )'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fb_query_symbol-error-1.bas(101) error 42: Variable not declared, SomeUndefinedSymbol in 'SomeUndefinedSymbol )'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fb_query_symbol-error-1.bas(101) error 42: Variable not declared, SomeUndefinedSymbol in 'SomeUndefinedSymbol )'

0 commit comments

Comments
 (0)