Is this a bug in script invoke: passing Integer-sized Long argument as 0

When I try to pass a Long via script.invoke it is passed as 0 if the receiving argument is declared as Long but the value fits in an Integer. If the same value is passed to Any or Integer, the correct value is received. Values that need a Long are also correctly received.

Whenever Script_invoke_Bug_invokee_3 in the code below is called (because the value fits in an Integer), it reports y_A as 0! In all other cases the correct value is reported.

I am running this version:
Version: 7.6.4.1 (X86_64) / LibreOffice Community
Build ID: e19e193f88cd6c0525a17fb7a176ed8e6a3e2aa1
CPU threads: 8; OS: Linux 5.3; UI render: default; VCL: kf5 (cairo+xcb)
Locale: en-GB (en_GB.UTF-8); UI: en-GB
Calc: threaded

Const Int_Max = 2 ^ 15 - 1		' Seems to be right
Const Int_Min = - 2 ^ 15		' Seems to be right

Sub Script_invoke_Bug
	Dim	z_Integer					as	Integer	:	z_Integer	= 1024
	Script_invoke_Bug_invoker (z_Integer)

	Dim	z_Long_1024					as	Integer	:	z_Long_1024	= 1024
	Script_invoke_Bug_invoker (z_Long_1024)

	Dim	z_Long						as	Long	:	z_Long		= 1048576
	Script_invoke_Bug_invoker (z_Long)

	Script_invoke_Bug_invoker (1048575)

	Script_invoke_Bug_invoker (Int_Max)

	Script_invoke_Bug_invoker (Int_Max + 1)

	Script_invoke_Bug_invoker (Int_Min)

	Script_invoke_Bug_invoker (Int_Min - 1)
End Sub

Sub Script_invoke_Bug_invoker (y as Any)
	Dim	z_Count						as Integer	:	z_Count		= IIf (y < Int_Min or Int_Max < y, 2, 3)
	Dim	z_Invokee					as	String	:	z_Invokee	= "Script_invoke_Bug_invokee_" + z_Count
	Dim	z_URI						as	String	:	z_URI		= "vnd.sun.star.script:Standard.Pjt_Scratch." + z_Invokee + "?language=Basic&location=application"
	Dim z_Script					as	Object	:	z_Script	= ThisComponent.scriptProvider.getScript (z_URI)

	Dim	z_Parameters	(0 to 2)	as	Any
	z_Parameters (0)	= y
	z_Parameters (1)	= y
	z_Parameters (2)	= y

	Dim	z_Out_Parameter_Indices	()	as	Integer
	Dim	z_Out_Parameters		()	as	Any

	z_Script.invoke (z_Parameters, z_Out_Parameter_Indices, z_Out_Parameters)
End Sub

Sub Script_invoke_Bug_invokee_3 (y_A as Long, y_B as Integer, y_C as Any)
	MsgBox ("A = " + y_A + ",  B = " + y_B + ",  C = " + y_C)
End Sub

Sub Script_invoke_Bug_invokee_2 (y_A as Long, y_C as Any)
	MsgBox ("A = " + y_A + ",  C = " + y_C)
End Sub

It indeed looks like a bug. Some related code is discussed in tdf#133889 (which is different - but this one is definitely affected by the same “convert to a smallest integer type” hack described there).

Or is this caused by that fix maybe? Will check 7.1.

→ aha, it works OK in 7.1; fails in 7.2. Either that one, or tdf#133887, are natural suspects.
Please file a bug.

1 Like

I shall do so, presuming it is not unreasonably complicated.

2 Likes

The following macro Test demonstrates the same effect. Place it in Standard.Module1 of the document.

Sub Test
  Dim oScript
  oSCript=ThisComponent.scriptProvider.getScript("vnd.sun.star.script:Standard.Module1.S_Ref?language=Basic&location=document")
  oScript.Invoke Array(1), Array(), Array()  ' Shows 0. Must be 1.
 End Sub

Sub S_Ref(n As Long)
  Msgbox n    ' Show 0. Must be 1.
End Sub

If in the S_Ref macro you change the parameter type to one of the following: Byte, Integer, Date, Currency, String, Variant, then the result is correct (other than 0).
If you change the type to Double, the result is incorrect (0).

Total 6:2 in favor of the truth. :slightly_smiling_face:

2 Likes

That works too, but I was interested to see more different cases, including edge cases at once without having to change the code. Trying Double as well is of course an important case that also belongs in a regression test.

Edit P.S. I also wanted to investigate whether the datatype of the source effected the behaviour.

tdf#159412

1 Like

Since it is correctly passed to an argument of type Any, a workaround is to use this type in the “invokee”, perhaps immediately assigning the value to a local Long variable. This is somewhat worse at detecting errors than cleanly declaring the argument as Long, but it is the best I can think of, if one has control of the invokee.

It is a bug, registered as 159412 – Passing Integer-sized Long argument via script.invoke as 0 .

A fix for that bug has been merged to the master version and is available in the daily builds.
I have not yet been able to test the fix, perhaps because my Linux (Open Suse 15.3) needs upgrading.

For a workaround, declare the receiving argument as Any, as in my comment.