Bug 145487 - UI BASIC - Macro causes infinite sheets to be added
Summary: UI BASIC - Macro causes infinite sheets to be added
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Calc (show other bugs)
Version:
(earliest affected)
7.2.2.2 release
Hardware: x86-64 (AMD64) Linux (All)
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: Macro
  Show dependency treegraph
 
Reported: 2021-11-01 10:27 UTC by studog
Modified: 2022-10-19 15:52 UTC (History)
3 users (show)

See Also:
Crash report or crash signature:


Attachments
valgrind trace from a fresh dev build (7.16 MB, text/plain)
2021-11-03 05:42 UTC, studog
Details

Note You need to log in before you can comment on or make changes to this bug.
Description studog 2021-11-01 10:27:03 UTC
A fairly unassuming macro causes the Add Sheet button to add infinite sheets to a workbook.

This is affecting my current project which isn't large but has complexity. I've narrowed it down to a small 100% reproducible test case.

Steps to reproduce
1. Start with Calc (and all Office apps) complete closed. Open Calc.
2. Tools -> Macros -> Edit Macros
3. In the editor click the Modules toolbar button to add 'Module1' to 'Untitled 1'. Modules button -> expand 'Untitled 1' -> select 'Standard' -> New... -> accept defaults by clicking Ok -> Close Macro Organizer dialog
4. In the navigation on the left, select the newly added 'Module1': Expand 'Untitled 1' -> Expand Standard -> double click 'Module1'.
5. Add macro to module. There will be a pre-populated template, delete that. Then add this:
function infiniteSheets(q1 as variant, q2 as variant, q3 as variant, q4 as variant)
        dim w1 as variant
        if IsEmpty(w1) then
                infiniteSheets = w1
                exit function
        end if
        infiniteSheets = ""
end function
6. Set a breakpoint on line 3 by double-clicking just to the left of the '3' line number. A red circle with a dot should appear.
7. Switch back to the untitled spreadsheet window.
8. In cell A1 enter the text '=infinitesheets(A2,B2,C2,D2)' then hit enter.
9. OBSERVE: the breakpoint isn't hit. This is probably a secondary bug worth its own bug report, but it's not the focus of this bug report.
10. In the status bar, click the Add Sheet button.
11. OBSERVE: The breakpoint is hit.
12. Press the run button in the Macro Editor window.
13. OBSERVE: The main Calc window is now adding an infinite number of sheets.
14. Clicking the Add Sheet button a second time halts the process.

In my original project there was some dependency on the macro arguments. I was passing in an array from a column of cells that happened to have no values or formulas. This was argument number 5 of 6 arguments. The problem would reproduce when this empty array was in positions 4, 5, or 6, but not in 3 or 1.
The reproduction above takes 4 arguments but I haven't tried to find out if that's important or not. The reproduction doesn't actually use any of the arguments, unlike my project.

While I've been typing this, I let the process run. It has stopped adding sheets after 3688 sheets, but the cursor is still in the spinning wheel form indicating processing is ongoing, just no longer adding sheets.
UPDATE: Sheets are still being added, the UI is just updating with extreme lag. Currently at 4984.

In my project, I was able to avoid the issue by removing parts of the macro, but it wasn't clear why that worked. In the reproduction case, the delay of responding to the breakpoint hit to press run to let it continue appears to be critical. Run the reproduction and skip setting the breakpoint in step 6, the problem won't occur. I suspect that how long the macro runs vis a vis the Add Sheet functionality running is important.

Part of my project is a macro that instantiates a new templated sheet. Today is the first time I hit the Add Sheet button on my project in quite a long time. I don't know if this bug is new in 7.2.2.2 or has been around for a while.

Data:

studog@home:~$ uname -a
Linux home 5.4.0-87-generic #98~18.04.1-Ubuntu SMP Wed Sep 22 10:45:04 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
studog@home:~$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.6 LTS
Release:	18.04
Codename:	bionic

Version: 7.2.2.2 / LibreOffice Community
Build ID: 20(Build:2)
CPU threads: 8; OS: Linux 5.4; UI render: default; VCL: gtk3
Locale: en-US (C.UTF-8); UI: en-US
Ubuntu package version: 1:7.2.2~rc2-0ubuntu0.18.04.1~lo1
Calc: threaded

7.2.2.2 is a recent upgrade:
2021-10-17 01:30:37 upgrade libreoffice-base-drivers:amd64 1:7.1.6~rc2-0ubuntu0.18.04.1~lo1 1:7.2.2~rc2-0ubuntu0.18.04.1~lo1
2021-10-17 01:30:38 upgrade libreoffice-calc:amd64 1:7.1.6~rc2-0ubuntu0.18.04.1~lo1 1:7.2.2~rc2-0ubuntu0.18.04.1~lo1
Comment 1 studog 2021-11-03 02:58:07 UTC
I temporarily removed $HOME/.config/libreoffice/4 and reproduced the bug, so it's not my user profile.
Comment 2 studog 2021-11-03 05:42:06 UTC
Created attachment 176086 [details]
valgrind trace from a fresh dev build
Comment 3 Mike Kaganski 2021-11-19 06:01:36 UTC
Repro: Version: 7.2.3.1 (x64) / LibreOffice Community
Build ID: 1d5dee817bde88d78dbcc0d00f88492568e131d5
CPU threads: 12; OS: Windows 10.0 Build 19043; UI render: default; VCL: win
Locale: ru-RU (ru_RU); UI: en-US
Calc: threaded
Comment 4 Rafael Lima 2021-11-22 18:32:18 UTC
One interesting finding about this bug is that it has nothing to do with your macro. The same bug happens with any macro provided that you insert a sheet while the processing is stopped by a break point.

For instance, try the following macro:

Function IncrementByTen(v1 as Variant) As Variant
    IncrementByTen = v1 + 10
End Function

Then insert any value in cell A1 (e.g. 5) and in cell A2 enter '=INCREMENTBYTEN(A1)'

Place a break point in the second line of this function (where 'IncrementByTen = v1 + 10' is). Now insert a sheet and when the break point is reached, if you click the Run button, infinite sheets will be inserted.

Inserting a sheet without any break points does not cause the problem.

This bug is probably related to how user functions are called when a break point is active, causing an infinite loop.