Bug 85371 - Redim with function name
Summary: Redim with function name
Status: VERIFIED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
4.2.6.3 release
Hardware: x86 (IA32) Linux (All)
: medium minor
Assignee: Andreas Heinisch
URL:
Whiteboard: target:7.1.0
Keywords:
Depends on:
Blocks: Macro-StarBasic
  Show dependency treegraph
 
Reported: 2014-10-23 15:22 UTC by ge.huber
Modified: 2020-10-30 13:04 UTC (History)
3 users (show)

See Also:
Crash report or crash signature:


Attachments
Nothing new here. Textfile with the code from the description. (9.30 KB, application/vnd.oasis.opendocument.text)
2014-10-23 15:22 UTC, ge.huber
Details
Screenshot of the error (32.87 KB, image/png)
2019-08-11 17:12 UTC, ge.huber
Details

Note You need to log in before you can comment on or make changes to this bug.
Description ge.huber 2014-10-23 15:22:44 UTC
Created attachment 108304 [details]
Nothing new here. Textfile with the code from the description.

Trying to Redim a field inside a function with the function name fails with an "Reading Error. Property is read only"-error. Run this code, to see the error:

Sub Main
Dim FieldOfLongs() As Long
Dim l As Long
l = foo(FieldOfLongs) 
End Sub
 
Function foo(FieldOfLongs() As Long) As Long 
Print "foo ist actually " & foo
Dim Zero As Long
Redim FieldOfLongs(Zero) As Long 'Redim works
Redim FieldOfLongs(foo) As Long 'Redim fails with error
End Function

The expected behaviour is, that the function name should behave just like any other variable within the function. 

Problem arises on 
Lubuntu 14.04
with
Version: 4.2.6.3
Build-ID: 420m0(Build:3)

The error actually says "Lesefehler. Eigenschaft ist schreibgeschützt" in my Germann version of LO.
Comment 1 Buovjaga 2014-10-24 19:17:50 UTC
I confirm the error.

Version: 4.4.0.0.alpha1+
Build ID: 0a82645c360158f9cc0fdabe2a52f1ff8f981bed
TinderBox: Win-x86@39, Branch:master, Time: 2014-10-24_06:59:23
Comment 2 ge.huber 2014-11-16 18:44:59 UTC
There seem to be more problems with using the function name as a variable inside the function. It can not be used as the loop variable in a For ... Next loop. 
 
Sub Main
Print foob()
Print fool()
End Sub
 
'This surrogate works 
Function foob() As long
foob = 1 
LoopLable:
  Print foob
  foob = foob + 1
If foob < 4 Then Goto LoopLable 
End Function
 
'While the For ... Next doesn't
Function fooc() As long
For fooc = 1 To 3
  Print fooc
Next fooc
End Function
Comment 3 QA Administrators 2015-12-20 16:06:20 UTC Comment hidden (obsolete)
Comment 4 QA Administrators 2017-01-03 19:46:03 UTC Comment hidden (obsolete)
Comment 5 Buovjaga 2019-08-11 11:31:03 UTC
Still confirmed

Arch Linux 64-bit
Version: 6.4.0.0.alpha0+
Build ID: 37fc9f51a8de11d40632e8cda17ccf1fa4b1f503
CPU threads: 8; OS: Linux 5.2; UI render: default; VCL: gtk3; 
Locale: fi-FI (fi_FI.UTF-8); UI-Language: en-US
Calc: threaded
Built on 6 August 2019
Comment 6 ge.huber 2019-08-11 17:12:12 UTC
Created attachment 153300 [details]
Screenshot of the error
Comment 7 ge.huber 2019-08-11 17:15:36 UTC
@Buovjaga 2019-08-11 11:31:03 UTC
Thank you for reminding me.
The error is still with me in the version supplied with Lubuntu 18.04 LTS, which is Version: 6.0.7.3, Build-ID: 1:6.0.7-0ubuntu0.18.04.8
Comment 8 ge.huber 2019-08-11 17:26:36 UTC
(In reply to ge.huber from comment #2)
> There seem to be more problems with using the function name as a variable
> inside the function. It can not be used as the loop variable in a For ...
> Next loop. 

Hopefully correct version, that is the one, which shows the bug clearly.

Sub Main
Print "foob is " & foob() 'works
Print "fooc is " & fooc() 'fails
End Sub
 
'This surrogate works 
Function foob() As long
foob = 1 
LoopLable:
  Print "foob is now " & foob
  foob = foob + 1
If foob < 4 Then Goto LoopLable 
End Function
 
'While the For ... Next doesn't
Function fooc() As long
For fooc = 1 To 3 'error occurs here
  Print "fooc is now " & fooc
Next fooc
End Function
Comment 9 himajin100000 2019-08-29 21:12:17 UTC
Just a guess:

As I pointed out in tdf#57308, SbMethod is silently added to refParams at index 0.

This method basically has the flag SbxFlagBits::Read only, so we cannot write to this variable.

https://opengrok.libreoffice.org/xref/core/basic/source/classes/sbxmod.cxx?r=889dc7bf#502

However, if the variable is used in a function(pMeth) with the same name as the variable, the variable will be given a SbxFlagBits::Write.

https://opengrok.libreoffice.org/xref/core/basic/source/runtime/runtime.cxx?r=889dc7bf#1651

and the flag will be restored later in StepPUT.

On the other hand if the variable is used in StepINITFOR,StepNext

https://opengrok.libreoffice.org/xref/core/basic/source/comp/loops.cxx?r=ab9b67bb#238
https://opengrok.libreoffice.org/xref/core/basic/source/inc/opcodes.hxx?r=abe5a1a5#28
https://opengrok.libreoffice.org/xref/core/basic/source/runtime/runtime.cxx?r=889dc7bf#763
https://opengrok.libreoffice.org/xref/core/basic/source/runtime/runtime.cxx?r=889dc7bf#StepINITFOR
https://opengrok.libreoffice.org/xref/core/basic/source/runtime/runtime.cxx?r=889dc7bf#2546

SbxFlagBits::Write is not set to this variable. I guess this is the cause.
Comment 10 Commit Notification 2020-10-30 04:12:57 UTC
Andreas Heinisch committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/48d46bcc903aedf0dd82746222c5333e3cd116a1

tdf#85371 - grant write access to the method used as a variable

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 11 Andreas Heinisch 2020-10-30 06:32:11 UTC
Credits again to himajin100000@gmail.com and Mike Kaganski.
Comment 12 Buovjaga 2020-10-30 13:04:33 UTC
I verify the fix

Arch Linux 64-bit
Version: 7.1.0.0.alpha1+
Build ID: 1a4ae360d06ae300a8fd5482b3b3a86dc021750d
CPU threads: 8; OS: Linux 5.9; UI render: default; VCL: kf5
Locale: fi-FI (fi_FI.UTF-8); UI: en-US
Calc: threaded
Built on 30 October 2020