Bug 79426 - Macros: Passing an error object to a function causes a run time error.
Summary: Macros: Passing an error object to a function causes a run time error.
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
4.2.3.3 release
Hardware: Other All
: low minor
Assignee: Andreas Heinisch
URL:
Whiteboard: BSA target:7.0.0
Keywords:
Depends on:
Blocks: Macro-StarBasic
  Show dependency treegraph
 
Reported: 2014-05-29 19:48 UTC by Ewald Anderl
Modified: 2020-04-02 11:58 UTC (History)
4 users (show)

See Also:
Crash report or crash signature:


Attachments
Writer doc with additional code examples, analysis and screen shots (145.76 KB, application/vnd.oasis.opendocument.text)
2014-05-29 19:48 UTC, Ewald Anderl
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Ewald Anderl 2014-05-29 19:48:22 UTC
Created attachment 100121 [details]
Writer doc with additional code examples, analysis and screen shots

Problem description:
====================
Passing an error object to a function causes a run time error. Any reference within the function to the argument containing an error object such as “CVErr(2)“, causes the run time error: “BASIC runtime error. Argument is not optional.”

How to reproduce the problem:
=============================
1. In a LibreOffice document with Macros, open Tools --> Macros --> Organize Macros --> pick any module to edit

2. Type (or cut/paste) the following code in to the “Module” of your choice. (Module1 will do)
-------------------------
Sub TestCVErr
	Dim vErr
	Dim nValue as Integer
	Dim ss
	vErr = CVErr(2)
	nValue = vErr
	ss = ""
	ss = ss & "VarType, TypeName, and nValue = " & VarType(vErr)
	ss = ss & ", " & TypeName(vErr)& ", " & nValue
	Print ss
	Print fnGetErrNum(vErr)	'This causes failure
End Sub

Function fnGetErrNum(vErr)
	Dim nValue as Integer
	nValue = vErr
	fnGetErrNum() = nValue
End Function
-----------------------------------
3. Run  TestCVErr

4. The program stops with a run time error ( “BASIC runtime error. Argument is not optional.”) in the function

5. Replace the last line in TestCVErr() with the following, passing the integer nValue rather than the error object:
------------------------------------
	Print fnGetErrNum(nValue)	'This works
------------------------------------
6. Run TestCVErr and the function returns the number correctly.

Current behavior:
===================
If the argument being passed to the function contains an error object, any reference to it will cause a run time error. The same code that works in the main routine will fail if placed within a function referencing the vErr as an argument. The same behavior was observed in both a Linux (fc20) and a Windows (Win7/64-Bit) environment.

Expected behavior:
===================
The function should return an integer value of 2; this should be the same behavior as the code in the  main routine “nValue = vErr” which casts the error object into an integer value.

              
Operating System: All
Version: 4.2.3.3 release
Comment 1 Joel Madero 2015-02-25 23:30:41 UTC
Verified:
ubuntu 14.10 x64
LibreOffice 4.4.1.2 rc

Marking as:
New
Minor - workaround exists, does not prevent high quality work. If there is no workaround then I suppose "normal" would be the appropriate - not major.


low - incredibly low chance of this affecting many users at all. Nearly a year later, no dupes, no additional comments, etc...

CC'ing Noel to get his opinion.
Comment 2 tommy27 2016-04-16 07:27:26 UTC Comment hidden (obsolete)
Comment 3 QA Administrators 2017-05-22 13:24:20 UTC Comment hidden (obsolete)
Comment 4 Buovjaga 2019-08-11 11:23:13 UTC
Still repro with file

Version: 6.4.0.0.alpha0+ (x86)
Build ID: c738be4de6886a0c96b7d10df7e78c8b2964c135
CPU threads: 4; OS: Windows 10.0; UI render: default; VCL: win; 
Locale: fi-FI (fi_FI); UI-Language: en-US
Calc: threaded
Comment 6 Andreas Heinisch 2020-03-02 13:01:23 UTC
The problem lies in the following line of https://opengrok.libreoffice.org/xref/core/basic/source/runtime/runtime.cxx?r=efa9ea10#4060, since the variable is initialized as an error type, LO tries to search for an optional value. However, the variable has no error in it, i.e., pVar->GetError() is equal to ERRCODE_NONE.

if( pVar->GetType() == SbxERROR && nIdx )

So, we should only search for optionals, if the variable has not been found (ERRCODE_BASIC_NAMED_NOT_FOUND).
Comment 7 Commit Notification 2020-04-02 10:58:22 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.