Bug 125180 - Bad behaviour in BASIC procedure with several optional parameters.
Summary: Bad behaviour in BASIC procedure with several optional parameters.
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
Inherited From OOo
Hardware: x86-64 (AMD64) All
: medium normal
Assignee: Andreas Heinisch
URL:
Whiteboard: target:7.0.0
Keywords:
Depends on:
Blocks: Macro-StarBasic
  Show dependency treegraph
 
Reported: 2019-05-08 22:01 UTC by Luis
Modified: 2022-07-12 06:06 UTC (History)
5 users (show)

See Also:
Crash report or crash signature:


Attachments
Test file (10.59 KB, application/vnd.oasis.opendocument.text)
2019-05-08 22:01 UTC, Luis
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Luis 2019-05-08 22:01:11 UTC
Description:
In a BASIC procedure with two optional parameters when you call the procedure with the first parameter omited and the second parameter present then the fist parameter receives a value and the function IsMissing returs false.

I attached a test file with this BASIC code:

option explicit

Sub Main
	SubOneParameter()
	SubOneParameter(1)
	SubTwoParameters()
	SubTwoParameters(,)
	SubTwoParameters(,2)
	SubTwoParameters(1,)
	SubTwoParameters(1,2)	
End Sub

sub SubOneParameter(optional lPar1 as integer)
	if ismissing(lPar1) then
		msgbox("Missing parameter")
	else 
		msgbox("Not missing parameter, value "+lPar1)
	endif		
end sub

sub SubTwoParameters(optional lPar1 as integer,optional lPar2 as integer)
dim cMsg as string
	if ismissing(lPar1) then
		cMsg="Missing first parameter"
	else 
		cMsg="Not missing first parameter, value "+lPar1
	endif
	cMsg=cMsg+chr(13)
	if ismissing(lPar2) then
		cMsg=cMsg+"Missing second parameter"
	else 
		cMsg=cMsg+"Not missing second parameter, value "+lPar2
	endif
	msgbox(cMsg)
end sub

Actual Results:
In The call SubTwoParameters(,) the first parameter (not present) receives the value 448 ??? and shows message
Not missing first parameter, value 448
Missing second parameter

In The call SubTwoParameters(,2) the first parameter (not present) receives the value 448 ??? and shows message
Not missing first parameter, value 448
Not missing second parameter, value 2

Expected Results:
When the first parameter is not present never must reveive a value.


Reproducible: Always


User Profile Reset: Yes



Additional Info:
Comment 1 Luis 2019-05-08 22:01:58 UTC
Created attachment 151253 [details]
Test file
Comment 2 Oliver Brinzing 2019-05-09 16:58:52 UTC
confirming.

looks like the comma "," is evaluted as parameter ?
Comment 5 himajin100000 2020-01-28 10:17:25 UTC
or maybe...(not sure)
https://opengrok.libreoffice.org/xref/core/basic/source/sbx/sbxint.cxx?r=4fdc90c5#55
Comment 6 himajin100000 2020-01-28 17:54:27 UTC
seems my patch for tdf#118544 is the culprit. (e.g. Implicitly converting Variant/Error to Integer if the parameter is declared as Integer, and this is no longer of the type SbxError )
Comment 7 himajin100000 2020-01-31 12:28:02 UTC
my guess might be wrong, I'll see the code more closely just a bit later.
Comment 8 Andreas Heinisch 2020-03-05 11:51:42 UTC
I investigated the error and I think I need some clarifications: 

- in VB the function IsMissing works only with variant datatypes
- in LO IsMissing works or should work even with other datatypes?!? In the majority of the cases it works :). I does not work in the case, where the missing parameter is before any non missing parameters, e.g., SubTwoParameters(,2)

Because, if I change the method signature to variant datatypes, everything works as expected:
SubTwoParameters(optional lPar1 as variant, optional lPar2 as variant)

Any thoughts?
Comment 9 Mike Kaganski 2020-03-05 12:15:24 UTC
(In reply to Andreas Heinisch from comment #8)
> I investigated the error and I think I need some clarifications: 
> 
> - in VB the function IsMissing works only with variant datatypes
> - in LO IsMissing works or should work even with other datatypes?!?

Why are you asking? You have just fixed tdf#36737 ;-)

> In the
> majority of the cases it works :). I does not work in the case, where the
> missing parameter is before any non missing parameters, e.g.,
> SubTwoParameters(,2)

When you fixed the mentioned bug, in SbiRuntime::StepPARAM there was initial part assigning error 448 to missing previous arguments. That looks relevant. There should be a difference somewhere that treats actually missing parameter differently compared to error 448.
Comment 10 Andreas Heinisch 2020-03-05 16:10:46 UTC
(In reply to Mike Kaganski from comment #9)
> (In reply to Andreas Heinisch from comment #8)
> > I investigated the error and I think I need some clarifications: 
> > 
> > - in VB the function IsMissing works only with variant datatypes
> > - in LO IsMissing works or should work even with other datatypes?!?
> 
> Why are you asking? You have just fixed tdf#36737 ;-)

The more I work with these optionals/missing parameters, the more I get confused hahaha, but I found many RefCards, where it states, that it should work with every datatype, whereas in VB it works only with Variants.

> 
> > In the
> > majority of the cases it works :). I does not work in the case, where the
> > missing parameter is before any non missing parameters, e.g.,
> > SubTwoParameters(,2)
> 
> When you fixed the mentioned bug, in SbiRuntime::StepPARAM there was initial
> part assigning error 448 to missing previous arguments. That looks relevant.
> There should be a difference somewhere that treats actually missing
> parameter differently compared to error 448.

The thing is that the parameter already has a value in StepPARAM, because it has been converted from Error to Int. In SbiRuntime::StepEMPTY() it gets the error value (https://opengrok.libreoffice.org/xref/core/basic/source/runtime/runtime.cxx?r=8e323fca#2744) and later it will be converted to the specified type, for instance in https://opengrok.libreoffice.org/xref/core/basic/source/sbx/sbxuint.cxx?r=4fdc90c5#53.

I will search for a fix ...
Comment 11 Andreas Heinisch 2020-03-09 11:06:58 UTC
https://gerrit.libreoffice.org/c/core/+/90215/1
Comment 12 Commit Notification 2020-04-02 10:58:32 UTC
Andreas Heinisch committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/84b884135ee419fe7abfefa7b4b651a649cf9ad9

tdf#79426, tdf#125180 - Don't convert missing parameters to requested type

It will be available in 7.0.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.