Bug 149151 - Error in the Basic IIf function.
Summary: Error in the Basic IIf function.
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
6.0.0.3 release
Hardware: All All
: medium normal
Assignee: Mike Kaganski
URL:
Whiteboard: target:26.2.0 target:25.8.0.0.beta2
Keywords:
Depends on:
Blocks: Macro-StarBasic
  Show dependency treegraph
 
Reported: 2022-05-18 10:05 UTC by Vladimir Sokolinskiy
Modified: 2025-07-01 07:08 UTC (History)
4 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vladimir Sokolinskiy 2022-05-18 10:05:33 UTC
1.Run the next Basic script.

Sub Test1
  Dim i As Long, arr
  arr=IIf(i=0, Array(), Array(0))
End Sub

An error occurs that must not occur.

2.Run the next Basic script.

Sub Test2
  Dim i As Long, arr
  arr=IIf(i=1, Array(), Array(0))
  Msgbox IsArray(arr)
End Sub

Returns False, must be True.
Comment 1 Rafael Lima 2022-05-18 18:09:17 UTC
I can reproduce the error in LO 7.3. I also tried the following variation:

Sub TestA
    Dim i as Long, arr, a, b
    i = 0
    a = Array()
    b = Array(0)
    arr = Iif(i = 0, a, b)
End Sub

Which returned the error "Inadmissible value or data type. Index out of defined range." This does not seem to make sense since we're not trying to access internal data of the array.

This issue has something to do with the Array object. The following code runs without issues:

Sub TestB
    Dim i as Long, arr, a, b
    i = 0
    a = 3
    b = 4
    arr = Iif(i = 0, a, b)
End Sub

System info:
Version: 7.3.3.2 / LibreOffice Community
Build ID: 30(Build:2)
CPU threads: 12; OS: Linux 5.13; UI render: default; VCL: kf5 (cairo+xcb)
Locale: pt-BR (pt_BR.UTF-8); UI: en-US
Ubuntu package version: 1:7.3.3~rc2-0ubuntu0.21.10.1~lo1
Calc: threaded
Comment 2 Aron Budea 2022-07-10 01:47:46 UTC
Already in 6.0.0.3.
Comment 3 edil 2024-01-25 20:56:59 UTC
It seems that IIF evaluate both values before give a return value.
In PitonYack pag 77 : "The IIf (“Immediate If”) function returns one of two values based on a conditional expression.". May be array is not a valid value.
Workaround :
Sub TestA
    Dim i as Long, arr, a, b
    i = 0
    a = Array()
    b = Array(0)
    arr = b : If i = 0 Then arr = a
    ' arr = Iif(i = 0, a, b)
End Sub
Comment 4 Jean-Pierre Sanchez 2024-02-14 17:11:01 UTC
The following line raises an error
  arr = Iif(True, Array("A","B"), Array("B","A")) ' Raises an error

The following line does not work properly: arr = "A"
arr = Iif(False, Array("A","B"), Array("B","A")) ' 

IIF exposes the same issue when with functions returning an Array

Sub Main
	arr = Iif(true, getArray1(), getArray2())	
End Sub

Function getArray1() As Variant
	arr  = Array("A","B")
	getArray1 = arr
End Function

Function getArray2() As Variant
	arr  = Array("B","A")
	getArray2 = arr
End Function
Comment 5 mrupio 2025-06-28 15:29:35 UTC
I tried this code

Sub Test_NoError
  Dim arr2 As Variant
  arr2 = Iif(false, getArray(), getArray())	
End Sub

Sub Test_Error
  Dim arr2 As Variant
  arr2 = Iif(true, getArray(), getArray())	
End Sub

Function getArray() As Variant
  Dim arr As Variant
  arr  = Array("A","B")
  getArray = arr
End Function

with the same results (invocation with "true" - error, with "false" - no error).

Next, I changed SbRtl_Iif function (see https://opengrok.libreoffice.org/xref/core/basic/source/runtime/methods1.cxx?r=b710ba48503372ddaf10a17ce7f4f3340bc8adb8#405 ) into

void SbRtl_Iif(StarBASIC*, SbxArray& rPar, bool)
{
    if (rPar.Count() != 4)
        return StarBASIC::Error(ERRCODE_BASIC_BAD_ARGUMENT);
    *rPar.Get(0) = *rPar.Get(2);
}

so that first argument of "iif" doesn't matter... Or does it? The results are still the same.
Despite the fact that the modified function doesn't check if the "iif" function was called with true or false, the results are still the same.
So, the error occurs or not even when the paths of SbRtl_Iif execution are the same. It only depends on whether "iif" was called with true or false.

The same happens with function:

void SbRtl_Iif(StarBASIC*, SbxArray& rPar, bool)
{
    if (rPar.Count() != 4)
        return StarBASIC::Error(ERRCODE_BASIC_BAD_ARGUMENT);
    *rPar.Get(0) = *rPar.Get(3);
}

What do you think?
Comment 6 mrupio 2025-06-28 15:30:45 UTC
Version: 26.2.0.0.alpha0+ (X86_64) / LibreOffice Community
Build ID: c0c3d93e577d24085663bcb1a62596bc5ab7e678
CPU threads: 4; OS: Windows 10 X86_64 (build 19045); UI render: Skia/Vulkan; VCL: win
Locale: pl-PL (pl_PL); UI: en-US
Calc: threaded
Comment 7 Mike Kaganski 2025-06-28 22:50:17 UTC
https://gerrit.libreoffice.org/c/core/+/187145
Comment 8 Commit Notification 2025-06-29 05:56:23 UTC
Mike Kaganski committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/99247bfc4e42652baddf606f90e31fb4d22006ab

tdf#149151: disambiguate method parameters and array indexes

It will be available in 26.2.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 9 Vladimir Sokolinskiy 2025-06-29 11:03:13 UTC
Mike, thank you very much for fixing the bug!
Comment 10 Commit Notification 2025-07-01 07:08:24 UTC
Mike Kaganski committed a patch related to this issue.
It has been pushed to "libreoffice-25-8":

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

tdf#149151: disambiguate method parameters and array indexes

It will be available in 25.8.0.0.beta2.

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.