Bug 145570 - ReDim spills out of scope
Summary: ReDim spills out of scope
Status: UNCONFIRMED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
7.2.2.2 release
Hardware: x86-64 (AMD64) Linux (All)
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-11-06 16:46 UTC by Joshua Coppersmith
Modified: 2021-11-23 10:13 UTC (History)
4 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joshua Coppersmith 2021-11-06 16:46:12 UTC
If you pass a Variant holding an array to a Function normally you may access that array by its parameter name, as expected, regardless of array dimension count. However, if you ReDim that parameter name with different dimension count even in an excluded code branch, BEFORE attempting to access the array, the access to the array under the passed dimension count will not compile.

The example below will not compile.

Sub StartBug()
	Dim A(1 To 2, 1 To 2)
	FuncBug(A)
End Sub
Function FuncBug(A As Variant)
	If False Then
		ReDim A(1 To 3)
	Else
		A(1,1) = 42
	End If
End Function

This next example below will compile.

Sub StartBug()
	Dim A(1 To 2, 1 To 2)
	FuncBug(A)
End Sub
Function FuncBug(A As Variant)
	If True Then
		A(1,1) = 42
	Else
		ReDim A(1 To 3)
	End If
End Function

Simply put, the compiler seems--I mean seems, not is--naive when protecting against dimension count errors.

There would seem to be a heartier workaround than ordering of the branches by Dimming and ReDimming an array locally to FuncBug then assigning that back to A before exit, but this bug might relate to Bug 133811 and seems worth consideration.
Comment 1 himajin100000 2021-11-07 21:02:54 UTC
just a guess:

should we prepare stack-thingy that holds dimensions for each block , pop and reset to that dimensions here

https://opengrok.libreoffice.org/xref/core/basic/source/comp/loops.cxx?r=ab9b67bb#63

and before Parse() here?

https://opengrok.libreoffice.org/xref/core/basic/source/comp/loops.cxx?r=ab9b67bb#74
Comment 2 himajin100000 2021-11-07 21:06:55 UTC
and similar fix may be needed for single-line redim and for other branching syntax such as Switch-case.

Another thing I'm just a bit concerned is "Go To" Statement.
Comment 3 himajin100000 2021-11-07 21:35:54 UTC
just a guess:

Sub Main()
Dim X() As Integer
A: 
Redim X(1)
Goto C: ' Probably While runtime gotes to C, Parser goes to B, not C.
B:
X(1,1) = 3
Exit Sub
C:
Redim X(1,1)
Goto B:
End Sub
Comment 4 Joshua Coppersmith 2021-11-10 02:10:21 UTC
Looking at the following

Sub StartUp1()
	Dim Passed(1 To 2) As Variant
	FuncBug1(Passed)
End Sub
Function FuncBug1(Passed() As Variant)
	Dim LocalDimmed(1 To 2) As Variant
	'LocalDimmed(1, 1) = 42 'Will not compile for a locally Dimmed array
	Passed(1, 1) = 42 'Compiles and works, ignoring 2nd index
	MsgBox Passed(1, 1, 404, "Hi, how are you?") 'Shows 42
End Function

we see--as a fun side note--that passed arrays may not even generate a runtime error with mismatched dimensions. This is harmless because when accessed with too many indices the code works, but when accessed with too few indices the runtime generates an 'Index out of defined range' error.

Quirkiness aside, this leads to the question: Should an array passed to a function even be checked for dimension count at all at compile time if that makes no claims about operation at runtime? I get that the scoping issue would still exist for locally Dimmed arrays, but I am just wondering about the philosophy of issuing compile-time errors on things that can't really be checked statically anyway.
Comment 5 Xisco Faulí 2021-11-23 10:13:42 UTC
@Andreas, @Mike, I thought you might be interested in this issue