Bug 123025

Summary: ReDim Preserve fails if array is filled by Split
Product: LibreOffice Reporter: Gerhard Weydt <gerhard.weydt>
Component: BASICAssignee: Andreas Heinisch <andreas.heinisch>
Status: VERIFIED FIXED    
Severity: normal CC: himajin100000, oliver.brinzing
Priority: medium    
Version: Inherited From OOo   
Hardware: All   
OS: All   
See Also: https://bugs.documentfoundation.org/show_bug.cgi?id=123263
Whiteboard: target:7.1.0
Crash report or crash signature: Regression By:
Bug Depends on:    
Bug Blocks: 127592    

Description Gerhard Weydt 2019-01-28 22:49:04 UTC
If an array is filled by the Split function, subsequent use of ReDim always emties the array, regardless whether it worked befor.

Try this test code (Xray has been used because it easily displays the array; if you don't want to install it, use msgbox arr(0) instead, seeing arr(0) empty will be convincing enough). The Xray calls are numbered to fit the output below.

sub test

dim s as string, arr(1) as string

arr(0) = "aa"
arr(1) = "bb"
Xray arr  '[1]
ReDim Preserve arr(1)
Xray arr  '[2]

s = "a/b"
arr = Split(s, "/")
Xray arr  '[3]
ReDim Preserve arr(1)
Xray arr  '[4]

arr(0) = "aa"
arr(1) = "bb"
Xray arr  '[5]
ReDim Preserve arr(1)
Xray arr  '[6]

end sub

Executing this macro you will get:

[1] after entering data: correct
          Tabelle : T( 0 To 1 ) As string  
 
(0)       | string : "aa"
(1)       | string : "bb"

[2] after ReDim Preserve: correct
          Tabelle : T( 0 To 1 ) As string  
 
(0)       | string : "aa"
(1)       | string : "bb"

[3] after assigning the values by Split: correct
          Tabelle : T( 0 To 1 ) As variant  
 
(0)       | string : "a"
(1)       | string : "b"

[4] after ReDim Preserve: w r o n g
          Tabelle : T( 0 To 1 ) As empty  
 
(0)       | <empty> 
(1)       | <empty> 

[5] after entering new data: still wrong, even though this should be as in [1]
          Tabelle : T( 0 To 1 ) As empty  
 
(0)       | <empty> 
(1)       | <empty> 

[6] after ReDim Preserve: wrong, consequently
          Tabelle : T( 0 To 1 ) As empty  
 
(0)       | <empty> 
(1)       | <empty> 

So it seems that assigning values to the array by Split does something wrong to the array, such that ReDim Preserve cannot find the correct information.
Comment 1 Oliver Brinzing 2019-01-29 18:03:06 UTC
already reproducible with AOO 4.1.5

it will work if the variable "arr" is a variant, e.g.

dim arr() as variant
or
dim arr()
Comment 2 himajin100000 2019-02-08 05:31:49 UTC
https://opengrok.libreoffice.org/xref/core/basic/source/runtime/methods1.cxx?r=48314f25#1638
https://opengrok.libreoffice.org/xref/core/basic/source/runtime/methods1.cxx?r=48314f25#1644

Changing these SbxVARIANT to SbxSTRING seems to do the trick, but I'm not sure I'm doing things correctly.
Comment 3 himajin100000 2019-02-08 05:48:39 UTC
Investigating...

==================
REM  *****  BASIC  *****
Option Explicit
sub test

dim s as string 
dim arr(1) as Integer

s = "a/b"
arr = Split(s, "/")
Msgbox(arr(0))
ReDim Preserve arr(1)
Msgbox(arr(0))

end sub

Showing "a" in Msgbox Functions(though arr is declared as an array of Integer)
Comment 4 himajin100000 2019-02-08 11:00:46 UTC
Hmmm...It seems the new problem I pointed out was not related to Split function

Option Explicit 
Sub Main
	Dim a(1) As Integer
	Dim b(1) As String
	b = a
End Sub

runs without error.
Comment 5 Commit Notification 2020-10-06 15:18:51 UTC
Andreas Heinisch committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/698e5d54182d96a1fd0c3b864ba0e618f82dd1f1

tdf#123025 - fixed broken tests for cverr method

It will be available in 7.1.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 6 Commit Notification 2020-10-16 10:31:53 UTC
Andreas Heinisch committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/d6b7cc3f7c07b98c90194e8b33cf44b94804b525

tdf#123025 - ReDim Preserve fails if array is filled by Split

It will be available in 7.1.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 7 Andreas Heinisch 2020-10-16 12:16:44 UTC
Credits go to  himajin100000@gmail.com for pointing out the problem as well.