Download it now!
Bug 126913 - Wrong behavior of Basic "mid" statement
Summary: Wrong behavior of Basic "mid" statement
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
Inherited From OOo
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-08-14 09:47 UTC by deustrno
Modified: 2019-08-15 06:27 UTC (History)
3 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 deustrno 2019-08-14 09:47:06 UTC
Execute the following basic code and
run function "my_function".

function my_function () as string
   dim my_string as string
   let my_string = "abc def jhi"
   mid( my_string, 5 ) = "123"
   let my_function = my_string
end function

What value was returned by this function?

Actual behavior:
* "microsoft office 2016 excel":
  return value is "abc 123 def";
* "libreoffice 6.2.5.2 calc":
  return value is "abc 123".

Expected behavior:
return values must be identical.
Comment 2 himajin100000 2019-08-14 10:20:24 UTC
Reproduced, except for the result being ""abc 123 jhi"
Comment 3 Oliver Brinzing 2019-08-14 12:48:42 UTC
IMHO this is not a bug, to make mid() vba compatible, use 

Option VBASupport 1

Sub Main
    msgbox my_function()
End Sub

function my_function () as string
   dim my_string as string
   let my_string = "abc def jhi"
   mid( my_string, 5 ) = "123"
   let my_function = my_string
end function
Comment 4 Julien Nabet 2019-08-14 16:53:39 UTC
(In reply to Oliver Brinzing from comment #3)
> IMHO this is not a bug, to make mid() vba compatible, use 
> 
> Option VBASupport 1
> 
> Sub Main
>     msgbox my_function()
> End Sub
>...

On pc Debian x86-64 with master sources updated today, I got 
abc 123

with VBASupport equal to 1 or 0, whatever
Comment 5 Julien Nabet 2019-08-14 17:23:35 UTC
With this patch:
diff --git a/basic/source/runtime/methods.cxx b/basic/source/runtime/methods.cxx
index fe266fdc009c..777b21040230 100644
--- a/basic/source/runtime/methods.cxx
+++ b/basic/source/runtime/methods.cxx
@@ -1159,7 +1159,7 @@ void SbRtl_Mid(StarBASIC *, SbxArray & rPar, bool bWrite)
 
                 OUStringBuffer aResultStr = aArgStr;
                 sal_Int32 nErase = nReplaceLen;
-                aResultStr.remove( nStartPos, nErase );
+                aResultStr.remove( nStartPos, nErase - nStartPos);
                 aResultStr.insert(
                     nStartPos, aReplaceStr.getStr(), std::min(nReplaceLen, nReplaceStrLen));

it works.
But this unit test fails:
    s = "The lightbrown fox"
    Mid(s, 5, 10, "lazy")
it expects:
"The lazy fox"
but with the patch I get:
"The lazyrown fox"

Perhaps, I could use "nErase - nStartPos" only when using VBASupport 1
but:
1) Still I don't know if the patch is right
2) Changing behaviour of mid function may bring some regressions for some other users

=> There's no more Basic expert in https://wiki.documentfoundation.org/FindTheExpert, so I'm a bit stuck.
Comment 6 Julien Nabet 2019-08-14 17:24:20 UTC
I put in cc the bugtracker related to the QA test which fails with the test patch quoted in my previous comment.
Comment 7 Oliver Brinzing 2019-08-15 06:27:01 UTC
(In reply to Julien Nabet from comment #4)
> On pc Debian x86-64 with master sources updated today, I got abc 123

sorry, only checked with LO 6.0.7.3 (VBASupport 1 worked but 0 crashes LO) and AOO 4.1.5, behaviour changed with LO 6.1.

maybe a side effect from fixes:

Bug 111313 - Crashed in Calc Macro (Basic)
Bug 121325 - 4-argument form of Mid() BASIC function broken in 6.x (worked in 5.x and OO)