Bug 145069 - The return value of a Basic function is not initialized
Summary: The return value of a Basic function is not initialized
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
Inherited From OOo
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-10-11 12:31 UTC by Andreas Heinisch
Modified: 2023-09-15 05:51 UTC (History)
1 user (show)

See Also:
Crash report or crash signature:


Attachments
There is six bugs in code we successfully identified it so we mentioned these in this picture with code (deleted)
2021-10-31 09:03 UTC, Amad Khan
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Andreas Heinisch 2021-10-11 12:31:28 UTC
Description:
Consider the following snippet in a Calc document:

Function sheetNames As String

oDoc=thisComponent
sheets = oDoc.sheets
For i = 0 to sheets.Count-1
    sheetNames = sheetNames + sheets.getByIndex(i).Name
Next i

MsgBox sheetNames

End Function

Steps to Reproduce:
1. Open a Calc document
2. Press ALT+F11
3. Open the default module and copy/paste the above snippet
4. Execute it twice

Actual Results:
First time: Sheet1
Second time: Sheet1Sheet1

Expected Results:
First time: Sheet1
Second time: Sheet1


Reproducible: Always


User Profile Reset: No



Additional Info:
Version: 7.1.0.3 (x64) / LibreOffice Community
Build ID: f6099ecf3d29644b5008cc8f48f42f4a40986e4c
CPU threads: 6; OS: Windows 10.0 Build 19042; UI render: Skia/Raster; VCL: win
Locale: de-DE (de_DE); UI: en-US
Calc: CL

and in 

Version 4.0.0.1 (Build ID: 527dba6f6e0cfbbc71bd6e7b88a52699bb48799)
Comment 1 Eike Rathke 2021-10-11 13:44:01 UTC
Isn't this even a feature? The "function name" return variable is static and keeps its value if not initialized otherwise?
Comment 2 Andreas Heinisch 2021-10-11 13:57:41 UTC
From https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/function-statement

"If Static isn't used, the value of local variables is not preserved between calls."

So I thought that the value should be cleared. How about recursions?
Comment 3 Amad Khan 2021-10-31 09:03:32 UTC Comment hidden (spam)
Comment 4 Amad Khan 2021-10-31 09:03:58 UTC Comment hidden (spam)
Comment 5 Buovjaga 2022-11-09 15:17:17 UTC Comment hidden (noise)
Comment 6 tanh 2022-11-20 03:02:32 UTC
(In reply to Andreas Heinisch from comment #2)
> From
> https://docs.microsoft.com/en-us/office/vba/language/reference/user-
> interface-help/function-statement
> 
> "If Static isn't used, the value of local variables is not preserved between
> calls."
> 
> So I thought that the value should be cleared. How about recursions?

I've not seen VBA documentation that makes it clear that the name of a function is a "local" variable. 

But all versions of VBA treat it that way. The name of a function is a local variable of that function, and is initialised at every call of that function.

Regarding recursion 'static' is not normally used with recursive VBA functions -- it kind of defeats the purpose in common cases.
Comment 7 Mike Kaganski 2023-09-15 05:51:05 UTC
Already repro with OOo 3.2