Bug 136467 - Regression on Basic Iif() function
Summary: Regression on Basic Iif() function
Status: RESOLVED NOTABUG
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
6.4.2.2 release
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords: bibisected, bisected
Depends on:
Blocks: Macro-StarBasic
  Show dependency treegraph
 
Reported: 2020-09-04 11:42 UTC by Alain Romedenne
Modified: 2020-09-06 01:59 UTC (History)
5 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 Alain Romedenne 2020-09-04 11:42:32 UTC
Description:
Iif(condition, expression1, expression2) evaluates BOTH expressions before execution. Exceptions can be raised unconditionnally.

e.g.
Sub _inverse_
	Print Iif(x=0,0,1/x)
End Sub

Steps to Reproduce:
1. Copy _inverse_ example routine
2. Run it 
3. Observe the result

Actual Results:
"Division by zero" error is thrown 

Expected Results:
Warning "0" should appear


Reproducible: Always


User Profile Reset: No



Additional Info:
Expected behaviour is obtained with libO 6.1.2
Comment 1 Julien Nabet 2020-09-04 17:19:56 UTC
I thought SbRtl_Iif could be a start point but it's not even called.
I don't know where "Iif" is dealt in LO and above all how to find the location.
(git grep -in iif didn't return anything interesting).
Comment 2 Andreas Heinisch 2020-09-04 17:31:56 UTC
At this line, the execution of the macro starts: https://opengrok.libreoffice.org/xref/core/basic/source/runtime/runtime.cxx?r=781b6ac9#788 and the error occurs in https://opengrok.libreoffice.org/xref/core/basic/source/runtime/runtime.cxx?r=781b6ac9#1375 because both expressions are evaluated before Iif is called.
Comment 3 Julien Nabet 2020-09-04 18:00:55 UTC
(In reply to Andreas Heinisch from comment #2)
> At this line, the execution of the macro starts:
> https://opengrok.libreoffice.org/xref/core/basic/source/runtime/runtime.
> cxx?r=781b6ac9#788 and the error occurs in
> https://opengrok.libreoffice.org/xref/core/basic/source/runtime/runtime.
> cxx?r=781b6ac9#1375 because both expressions are evaluated before Iif is
> called.

Ok but where is "iif" in these locations?
Comment 4 Andreas Heinisch 2020-09-04 18:19:19 UTC
The problem is that variable "x" is called from the stack and BEFORE the function  Iif is called all the steps in that line are performed, leading to a division by zero error. Therefore, Iif is never called.
Comment 5 Julien Nabet 2020-09-05 08:04:58 UTC
(In reply to Andreas Heinisch from comment #4)
> The problem is that variable "x" is called from the stack and BEFORE the
> function  Iif is called all the steps in that line are performed, leading to
> a division by zero error. Therefore, Iif is never called.

Ok I had missed the "before". Thank you for your patience.
Comment 6 Andreas Heinisch 2020-09-05 09:48:06 UTC
You are welcome. I am glad that I could help.
Comment 7 Aron Budea 2020-09-06 01:59:01 UTC
(In reply to Alain Romedenne from comment #0)
> Description:
> Iif(condition, expression1, expression2) evaluates BOTH expressions before
> execution. Exceptions can be raised unconditionnally.
I don't think this is a regression. In fact, I don't think it's a bug, I haven't found a reference to StarBasic, but VBA has the same function:
https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/iif-function

This states:
"IIf always evaluates both truepart and falsepart, even though it returns only one of them. Because of this, you should watch for undesirable side effects. For example, if evaluating falsepart results in a division by zero error, an error occurs even if expr is True."

Further note on the change that happened in 6.3:
> e.g.
> Sub _inverse_
> 	Print Iif(x=0,0,1/x)
> End Sub
If you do it like this, there's been a division by zero in older versions as well:
x=0
Print Iif(x=0,0,1/x)

The difference in the evaluation of the original sample came with the following commit:
https://cgit.freedesktop.org/libreoffice/core/commit/?id=15c39bb2e75df40c30bcbf789d815376dd2e31ce
author		Eike Rathke <erack@redhat.com>	2019-05-02 20:54:16 +0200
committer	Eike Rathke <erack@redhat.com>	2019-05-02 22:35:35 +0200

Resolves: tdf#124605 ditch "if operand 1 is Empty, result is operand 2"