Bug 165757 - ByVal variant array arguments allow to modify passed variables in subroutines
Summary: ByVal variant array arguments allow to modify passed variables in subroutines
Status: UNCONFIRMED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
unspecified
Hardware: x86-64 (AMD64) All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2025-03-15 15:24 UTC by mrupio
Modified: 2025-03-19 14:11 UTC (History)
3 users (show)

See Also:
Crash report or crash signature:


Attachments
Sample file. (9.86 KB, application/vnd.oasis.opendocument.spreadsheet)
2025-03-15 15:24 UTC, mrupio
Details

Note You need to log in before you can comment on or make changes to this bug.
Description mrupio 2025-03-15 15:24:51 UTC
Created attachment 199823 [details]
Sample file.

When I pass variant array to subroutine by value I can modify the external variable (array) inside the subroutine. Is it by design? If so, the documentation of "ByVal" should state it explicitly.

I tested it on Windows 10 x64, Calc 25.2.1.2 and on Linux x64, Calc 6.4.7.2.
Comment 1 Xisco Faulí 2025-03-17 09:24:25 UTC
@Mike, I thought you might be interested in this issue
Comment 2 Vladimir Sokolinskiy 2025-03-19 12:27:37 UTC
Possible misunderstandings with the ByVal construct are related to the fundamentally different behavior of arrays in LO Basic and VBA.

Unlike VBA, in LO basic after executing the assignment operator

array2=array1

the variables array1 and array2 will refer to the same array instance in memory.

Thus, in LO Basic, the behavior of the ByVal construct for array arguments is similar to the behavior for object arguments. After executing the procedure (subroutine, function), the actual parameter - variable will refer to the same array in memory, but this does not guarantee the immutability of the specified array.
Comment 3 mrupio 2025-03-19 13:28:51 UTC
> Thus, in LO Basic, the behavior of the ByVal construct for array arguments
> is similar to the behavior for object arguments.

So perhaps it would be a good idea to remind it in the "ByVal" documentation?
Comment 4 Vladimir Sokolinskiy 2025-03-19 13:57:47 UTC
I think it's a good idea.

Also, it might be worth explaining the basic concepts.
Compare:

1. LO Basic ([1]):

ByRef: The argument is passed by reference. ByRef is the default.

ByVal: The argument is passed by value. Its value can be modified by the called routine.


2. VBA ([2]):

by reference

A way of passing the address of an argument to a procedure instead of passing the value. This allows the procedure to access the actual variable. As a result, the variable's actual value can be changed by the procedure to which it is passed. Unless otherwise specified, arguments are passed by reference.

by value

A way of passing the value of an argument to a procedure instead of passing the address. This allows the procedure to access a copy of the variable. As a result, the variable's actual value can't be changed by the procedure to which it is passed.


[1] https://help.libreoffice.org/25.8/en-US/text/sbasic/shared/03090406.html?DbPAR=BASIC

[2] https://learn.microsoft.com/en-us/office/vba/language/glossary/vbe-glossary#by-reference
Comment 5 Vladimir Sokolinskiy 2025-03-19 14:11:36 UTC
Found a corresponding explanation in LO for ByRef and ByVal ([3]).

[3]. https://help.libreoffice.org/25.8/en-US/text/sbasic/shared/01020300.html?&DbPAR=BASIC&System=WIN