Bug 150458 - Incorrect calculation of the boundaries of the For loop (Option VbaSupport 1)
Summary: Incorrect calculation of the boundaries of the For loop (Option VbaSupport 1)
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
7.3.5.2 release
Hardware: All All
: medium normal
Assignee: Mike Kaganski
URL:
Whiteboard: target:25.2.0 target:24.8.1
Keywords:
Depends on:
Blocks: Macro-VBA
  Show dependency treegraph
 
Reported: 2022-08-17 13:14 UTC by Vladimir Sokolinskiy
Modified: 2024-08-19 21:06 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 Vladimir Sokolinskiy 2022-08-17 13:14:23 UTC
Run macro:

Option Explicit
Sub test
  Dim i As Long, j As Long, k As Long
  k=10
  For i=1 To k
    k=1
    j=i
  Next i
  
  Msgbox j
End Sub    

Shown: 1
Must be: 10

`For loop` bounds must be evaluated once at the beginning.
Comment 1 Mike Kaganski 2022-08-17 13:21:45 UTC
It should definitely be that way for VBA [1]. But why expect the same from non-compatibility mode? 

[1] https://stackoverflow.com/questions/27824936/for-start-to-end-loop-with-end-variable-changing-mid-loop
Comment 2 Vladimir Sokolinskiy 2022-08-17 13:51:00 UTC
I was born with this knowledge. :)

OK, let's limit to a bug in case
Comment 3 Vladimir Sokolinskiy 2022-08-17 13:52:07 UTC
Option VbaSupport  1
Comment 4 Mike Kaganski 2022-08-17 14:03:36 UTC
Repro using Version: 7.4.0.3 (x64) / LibreOffice Community
Build ID: f85e47c08ddd19c015c0114a68350214f7066f5a
CPU threads: 12; OS: Windows 10.0 Build 19044; UI render: Skia/Raster; VCL: win
Locale: ru-RU (ru_RU); UI: en-US
Calc: CL Jumbo

(with Option VBASupport 1, as mentioned in comment 3)

http://msdn.microsoft.com/en-us/library/ee157012.aspx
Comment 5 QA Administrators 2024-08-17 03:15:56 UTC Comment hidden (obsolete)
Comment 6 Mike Kaganski 2024-08-17 21:34:41 UTC
https://gerrit.libreoffice.org/c/core/+/172005
Comment 7 Commit Notification 2024-08-18 07:09:37 UTC
Mike Kaganski committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/afb46ee8803a04664f26da3e1f8f88a75d92cb7f

tdf#150458: evaluate for loop's end and step once in VBA support mode

It will be available in 25.2.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.
Comment 8 Vladimir Sokolinskiy 2024-08-18 12:23:05 UTC
Mike, thank you very much for the correction!
Comment 9 Commit Notification 2024-08-19 21:06:22 UTC
Mike Kaganski committed a patch related to this issue.
It has been pushed to "libreoffice-24-8":

https://git.libreoffice.org/core/commit/89476d1b95945871645a044c6f80ef6419e08db0

tdf#150458: evaluate for loop's end and step once in VBA support mode

It will be available in 24.8.1.

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.