Skip to content

Commit e3fcb8d

Browse files
authored
Merge pull request #87 from dkl/byref
Fix dim shared byref initializer check
2 parents e669d91 + 5d08b88 commit e3fcb8d

10 files changed

+76
-10
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Version 1.06.0
6161
- Fixed inline asm procedure name mangling bug (missing underscore prefix) with -gen gcc -asm intel on dos/win32
6262
- #878: Fix fbc-64bit hangs on 'Case True' + 'Case False' inside 'Select Case As const'
6363
- Fix SELECT CASE AS CONST to allow both signed & unsigned case range values
64+
- #822, #814, #842: Compiler crash when initializing static/shared reference (DIM SHARED/STATIC BYREF) with non-constant initializer (e.g. another reference, or dynamic array element)
6465

6566

6667
Version 1.05.0

src/compiler/ast-node-typeini.bas

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,15 @@ private sub hFlushExprStatic( byval n as ASTNODE ptr, byval basesym as FBSYMBOL
522522
if( sym = NULL ) then
523523
sym = basesym
524524
end if
525+
525526
var sdtype = symbGetType( sym )
527+
var sfulldtype = symbGetFullType( sym )
528+
if( symbIsRef( sym ) ) then
529+
'' Initializers for references initialize the pointer,
530+
'' not an object of the symbol's type.
531+
sdtype = typeAddrOf( sdtype )
532+
sfulldtype = typeAddrOf( sfulldtype )
533+
end if
526534

527535
'' Get rhs expression
528536
var expr = n->l
@@ -543,7 +551,7 @@ private sub hFlushExprStatic( byval n as ASTNODE ptr, byval basesym as FBSYMBOL
543551
else
544552
'' different types?
545553
if( edtype <> sdtype ) then
546-
expr = astNewCONV( symbGetFullType( sym ), symbGetSubtype( sym ), expr, AST_CONVOPT_DONTCHKPTR )
554+
expr = astNewCONV( sfulldtype, symbGetSubtype( sym ), expr, AST_CONVOPT_DONTCHKPTR )
547555
assert( expr <> NULL )
548556
end if
549557

@@ -556,7 +564,6 @@ private sub hFlushExprStatic( byval n as ASTNODE ptr, byval basesym as FBSYMBOL
556564
end if
557565
'' literal string..
558566
else
559-
var sdtype = symbGetType( sym )
560567
'' not a wstring?
561568
if( sdtype <> FB_DATATYPE_WCHAR ) then
562569
'' convert?

src/compiler/ir-tac.bas

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,11 +725,17 @@ private sub _emitVarIniEnd( byval sym as FBSYMBOL ptr )
725725
end sub
726726

727727
private sub _emitVarIniI( byval sym as FBSYMBOL ptr, byval value as longint )
728-
emitVARINIi( symbGetType( sym ), value )
728+
dim realtype as integer
729+
dim realsubtype as FBSYMBOL ptr
730+
symbGetRealType( sym, realtype, realsubtype )
731+
emitVARINIi( realtype, value )
729732
end sub
730733

731734
private sub _emitVarIniF( byval sym as FBSYMBOL ptr, byval value as double )
732-
emitVARINIf( symbGetType( sym ), value )
735+
dim realtype as integer
736+
dim realsubtype as FBSYMBOL ptr
737+
symbGetRealType( sym, realtype, realsubtype )
738+
emitVARINIf( realtype, value )
733739
end sub
734740

735741
private sub _emitVarIniOfs _

src/compiler/parser-decl-var.bas

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ private sub hValidateGlobalVarInit( byval sym as FBSYMBOL ptr, byref initree as
824824

825825
'' Disallow initialization of global dynamic strings
826826
'' (not implemented - requires executing code)
827-
if( symbGetType( sym ) = FB_DATATYPE_STRING ) then
827+
if( (symbGetType( sym ) = FB_DATATYPE_STRING) and (not symbIsRef( sym )) ) then
828828
errReport( FB_ERRMSG_CANTINITDYNAMICSTRINGS, TRUE )
829829
astDelTree( initree )
830830
initree = NULL
@@ -833,7 +833,7 @@ private sub hValidateGlobalVarInit( byval sym as FBSYMBOL ptr, byref initree as
833833

834834
'' Check for constant initializer?
835835
'' (doing this check first, it results in a nicer error message)
836-
if( symbHasCtor( sym ) = FALSE ) then
836+
if( (not symbHasCtor( sym )) or symbIsRef( sym ) ) then
837837
if( astTypeIniIsConst( initree ) = FALSE ) then
838838
errReport( FB_ERRMSG_EXPECTEDCONST )
839839
astDelTree( initree )
@@ -877,9 +877,7 @@ private function hCheckAndBuildByrefInitializer( byval sym as FBSYMBOL ptr, byre
877877
astTypeIniAddAssign( initree, astNewADDROF( expr ), sym, ptrdtype, ptrsubtype )
878878
astTypeIniEnd( initree, TRUE )
879879

880-
if( (symbGetAttrib( sym ) and (FB_SYMBATTRIB_STATIC or FB_SYMBATTRIB_SHARED)) <> 0 ) then
881-
hCheckVarsUsedInGlobalInit( sym, initree )
882-
end if
880+
hValidateGlobalVarInit( sym, initree )
883881

884882
function = initree
885883
end function

src/compiler/symb.bi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ enum FB_SYMBATTRIB
174174
FB_SYMBATTRIB_TEMP = &h00400000
175175
FB_SYMBATTRIB_DESCRIPTOR = &h00800000
176176
FB_SYMBATTRIB_FUNCRESULT = &h01000000
177-
FB_SYMBATTRIB_REF = &h02000000 '' procedures returning BYREF
177+
FB_SYMBATTRIB_REF = &h02000000 '' VARs/FIELDs (if declared with BYREF), PROCs (functions/function pointers returning BYREF)
178178
FB_SYMBATTRIB_VIS_PRIVATE = &h04000000 '' UDT members only
179179
FB_SYMBATTRIB_VIS_PROTECTED = &h08000000 '' ditto
180180
FB_SYMBATTRIB_NAKED = &h10000000 '' procedures only
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
' TEST_MODE : COMPILE_ONLY_FAIL
2+
3+
redim shared as integer array(10)
4+
public dim shared byref as integer r = array(5)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
' TEST_MODE : COMPILE_ONLY_FAIL
2+
3+
public dim shared a as integer = 5
4+
public dim shared byref as integer b = a
5+
public dim shared byref as integer c = b
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
' TEST_MODE : COMPILE_ONLY_FAIL
2+
3+
Type UDT
4+
Dim As Integer I = 123
5+
End Type
6+
7+
Static As UDT u
8+
9+
Static Byref As Integer RI = u.I
10+
Print RI
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
' TEST_MODE : COMPILE_ONLY_FAIL
2+
3+
Type UDT1
4+
Dim As Integer I1
5+
End Type
6+
7+
Type UDT2
8+
Dim As Integer I2
9+
Static Byref As UDT1 ru1
10+
End Type
11+
12+
Dim As UDT1 u1
13+
14+
Dim Byref As UDT1 UDT2.ru1 = u1

tests/dim/byref.bas

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,4 +608,25 @@ SUITE( fbc_tests.dim_.byref_ )
608608
externs.globalExterns_proc
609609
END_TEST
610610

611+
TEST_GROUP( globalConstantInitWithoutOffset )
612+
type UDT1
613+
dummy as integer
614+
end type
615+
616+
type UDT2
617+
dummy as integer
618+
static byref r1 as UDT1
619+
static byref r2 as UDT1
620+
end type
621+
622+
'' Initializer for global ref var using a constant that is not an offset
623+
dim byref UDT2.r1 as UDT1 = *cptr(UDT1 ptr, 0)
624+
dim byref UDT2.r2 as UDT1 = *cptr(UDT1 ptr, 123)
625+
626+
TEST( default )
627+
CU_ASSERT( @UDT2.r1 = 0 )
628+
CU_ASSERT( @UDT2.r2 = 123 )
629+
END_TEST
630+
END_TEST_GROUP
631+
611632
END_SUITE

0 commit comments

Comments
 (0)