Files
odfstudio/basic
Mike Kaganski 99247bfc4e tdf#149151: disambiguate method parameters and array indexes
SbiRuntime::FindElement ends with a call to CheckArray(). The latter
takes a variable, checks if that is an array and if it has parameters;
and if both are true, tries to obtain an element of the array defined
by the parameters as array indexes (using SbxDimArray::Get).

The problem was, that the variable may actually be array; it may have
parameters; but this array can be the result of a function call; the
variable may be SbxMethod; and the parameters may be for the function,
not array indexes.

Specifically, the call like

  Iif(True, Array("A","B"), Array("B","A"))

searched for an entity named Iif; found that it was a RTL method;
prepared the arguments for it (consisting of four elements: 0 - the
method itself; 1 - boolean True; 2 - array ["A", "B"]; 3 - array
["B", "A"]); the function executed, putting the correct value to the
params[0], which was the method itself (so its value became an array);
and finally, the SbxMethod variable with the array value got passed
to CheckArray. The latter found out that it's an array, and that it
has parameters (those four) - and tried to get the element, which
index was defined by the parameters.

Here comes the specifics of LibreOffice Basic's argument handling.
SbxDimArray::Get only cares about first arguments expected by the
array dimensions; it ignores the rest. Since the array was single-
dimensional, it only considered parameter 1 (parameter 0 is, by
convention, reserved for return value). That was a boolean. When it
was False, its GetInteger returned 0; and finally, CheckArray took
element 0 from the array, returning a scalar value, as if it was

  Iif(False, Array("A","B"), Array("B","A"))(0)

But when the boolean was True, another Basic specific took place:
its numerical value is -1; and trying to get element -1 from the
array resulted in "Index out of defined range".

This change disambiguates between the cases of array index, and
function parameters, checking if the variable itself, and its 0th
parameter are both SbxMethods. Then it doesn't try to treat the
parameters as indexes into the array.

Change-Id: I597f94a7e688c8bba12d87a1bd5871491c25cac0
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/187145
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
2025-06-29 07:55:39 +02:00
..
…
…
…

StarBASIC Interpreter

Contains the StarBASIC Interpreter

This implements a macro language that, when in VBA compatibility mode, is intended to be interoperable with Visual Basic for Applications, allowing people to run macros embedded in their documents.

See also

http://wiki.openoffice.org/wiki/Basic