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: 2024-08-02 13:55 UTC (History)
4 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)
Comment 8 QA Administrators 2021-08-15 03:45:56 UTC Comment hidden (obsolete)
Comment 9 Julien Nabet 2022-04-24 11:07:19 UTC
Testing the initial case on Excel:
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

Excel returns 
"abc 123 jhi"
not 
"abc 123 def"
Comment 10 QA Administrators 2024-07-10 03:13:38 UTC
Dear deustrno,

To make sure we're focusing on the bugs that affect our users today, LibreOffice QA is asking bug reporters and confirmers to retest open, confirmed bugs which have not been touched for over a year.

There have been thousands of bug fixes and commits since anyone checked on this bug report. During that time, it's possible that the bug has been fixed, or the details of the problem have changed. We'd really appreciate your help in getting confirmation that the bug is still present.

If you have time, please do the following:

Test to see if the bug is still present with the latest version of LibreOffice from https://www.libreoffice.org/download/

If the bug is present, please leave a comment that includes the information from Help - About LibreOffice.
 
If the bug is NOT present, please set the bug's Status field to RESOLVED-WORKSFORME and leave a comment that includes the information from Help - About LibreOffice.

Please DO NOT

Update the version field
Reply via email (please reply directly on the bug tracker)
Set the bug's Status field to RESOLVED - FIXED (this status has a particular meaning that is not 
appropriate in this case)


If you want to do more to help you can test to see if your issue is a REGRESSION. To do so:
1. Download and install oldest version of LibreOffice (usually 3.3 unless your bug pertains to a feature added after 3.3) from https://downloadarchive.documentfoundation.org/libreoffice/old/

2. Test your bug
3. Leave a comment with your results.
4a. If the bug was present with 3.3 - set version to 'inherited from OOo';
4b. If the bug was not present in 3.3 - add 'regression' to keyword


Feel free to come ask questions or to say hello in our QA chat: https://web.libera.chat/?settings=#libreoffice-qa

Thank you for helping us make LibreOffice even better for everyone!

Warm Regards,
QA Team

MassPing-UntouchedBug