Bug 132073 - Undo causes crash in Calc if Selection Changed sheet event writes to cell in another sheet
Summary: Undo causes crash in Calc if Selection Changed sheet event writes to cell in ...
Status: UNCONFIRMED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Calc (show other bugs)
Version:
(earliest affected)
4.1 all versions
Hardware: x86-64 (AMD64) All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords: haveBacktrace
Depends on:
Blocks: Crash-Assert Crash
  Show dependency treegraph
 
Reported: 2020-04-12 18:56 UTC by Gordon
Modified: 2025-06-22 11:26 UTC (History)
5 users (show)

See Also:
Crash report or crash signature:


Attachments
Demo of Undo Crashes (9.84 KB, application/vnd.oasis.opendocument.spreadsheet)
2020-05-20 21:08 UTC, Gordon
Details
bt with debug symbols (13.38 KB, text/plain)
2020-05-21 20:07 UTC, Julien Nabet
Details
The slightly changed demo file mentioned in comment by jag (10.11 KB, application/vnd.oasis.opendocument.spreadsheet)
2025-06-22 11:24 UTC, Wolfgang Jäger
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Gordon 2020-04-12 18:56:48 UTC
Description:
If a value is entered in a cell, control goes to the next cell.  Normally Undo will undo the change in the previous cell.  However if the sheet has the Selection Changed event linked to a macro that writes a value to a cell somewhere in the spreadsheet, Undo ends up taking control to the cell that was written by the Selection Changed event handler instead of the "last" modified cell.  If the Selection Changed event handler writes to a cell in a different sheet, sometimes Undo takes control to the cell in the other sheet and other times the spreadsheet just crashes.  It appears that when Undo is hit, it first tries to go back to the previous cell to change it back to its previous state, but the change in the selected cell then causes the Selection Changed handler to start execution before Undo can make its change.  The Selection Changed handler takes control to the cell where it does its write and control then goes back to Undo.  However, at this point it looks like Undo gets confused because it is now in a different cell, and the situation appears to be worse if that cell is on a different sheet.

Steps to Reproduce:
1. Create a spreadsheet with two sheets, Sheet 1, and Sheet 2
2. Create this macro in Sheet 1

Sub CrashTest (Event As Variant)

	Dim cell as object
	
	cell = thisComponent.sheets.getByName("Sheet 2").getCellByPosition(2,2)
	cell.value = 4
	
End Sub	

3. Create a Selection Changed sheet event in Sheet 1 that is linked to CrashTest.
4. Enter a value in a cell in Sheet 1 and then Undo

Actual Results:
Either control goes to cell C3 in Sheet 2 or crash.

Expected Results:
Selection Changed event does not get triggered and instead the previous value entered is undone.


Reproducible: Always


User Profile Reset: No


OpenGL enabled: Yes

Additional Info:
Version: 6.3.5.2 (x64)
Build ID: dd0751754f11728f69b42ee2af66670068624673
CPU threads: 4; OS: Windows 10.0; UI render: GL; VCL: win; 
Locale: en-US (en_US); UI-Language: en-US
Calc: CL

We are seeing this problem on Windows and Linux running several different versions of LibreOffice.
Comment 1 Gordon 2020-04-12 19:39:04 UTC
I noticed that the Event object is passed to the macro when it is triggered.  Is there anything contained in Event that would indicate that Selection Changed was triggered by Undo?  If so I could work around this by exiting the macro if it was triggered by Undo.  There appears to be no problem as long as the event handler does not write to a cell.  For our application, we do not want this to happen anyway if the user is just trying to do an Undo.
Comment 2 Xisco Faulí 2020-05-13 11:16:51 UTC
I can't reproduce it in

Version: 7.0.0.0.alpha1+
Build ID: 1ffe59ef31186e36ad0aa7bbcdd32e407ee8d26c
CPU threads: 4; OS: Linux 4.19; UI render: default; VCL: gtk3; 
Locale: en-US (en_US.UTF-8); UI: en-US
Calc: threaded

Please attach a sample document, as this makes it easier for us to verify the bug. 
I have set the bug's status to 'NEEDINFO'. Please change it back to 'UNCONFIRMED' once the requested document is provided.
(Please note that the attachment will be public, remove any sensitive information before attaching it. 
See https://wiki.documentfoundation.org/QA/FAQ#How_can_I_eliminate_confidential_data_from_a_sample_document.3F for help on how to do so.)
Comment 3 Gordon 2020-05-20 21:08:27 UTC
Created attachment 161053 [details]
Demo of Undo Crashes

Attached is a small spreadsheet demonstrating the bug we have encountered.

To test, enter a value anywhere on Sheet1 and hit <Enter>.  The macro will write a "4" to cell C3 on Sheet2, but control will go to the next row on Sheet1.

Then either hit CNTR/Z or select Undo from the Edit menu.

I have done additional testing and find that about 5% of the time the Undo will succeed.

Most of the time control will go to cell C3 on Sheet2 or crash.

The failure has occurred with version 6.3.5.2 on Windows and versions 5.4.7.2, 6.0.7.3 and 6.3.3.2 on Linux.  Version 6.0.7.3 is the supported version on my Linux distribution.

This looks to me like some type of timing issue and therefore might not be reproducible on all systems.  It looks like it succeeds if it is able to complete the undo before the Selection Changed event triggers the macro.
Comment 4 Gordon 2020-05-20 21:11:51 UTC
A sample spreadsheet has been attached to the previous comment.
Comment 5 Telesto 2020-05-20 21:30:18 UTC
Repro
Version: 7.0.0.0.alpha1+ (x64)
Build ID: 442c7b95e2ee94b66a9854d0cb22f8ecb76532c6
CPU threads: 4; OS: Windows 6.3 Build 9600; UI render: Skia/Raster; VCL: win; 
Locale: nl-NL (nl_NL); UI: en-US
Calc: CL

and with
Version: 5.0.0.0.alpha1+
Build ID: ab465b90f6c6da5595393a0ba73f33a1e71a2b65
Locale: nl-NL (nl_NL)

probably also in
3.5.7.2 (did crash, but not exact the same steps)
Comment 6 Julien Nabet 2020-05-21 20:07:30 UTC
Created attachment 161098 [details]
bt with debug symbols

On pc Debian x86-64 with master sources updated today + gen rendering, I had an assertion.
Comment 7 Xisco Faulí 2020-06-03 10:04:04 UTC
Also crashing in

Version 4.1.0.0.alpha0+ (Build ID: efca6f15609322f62a35619619a6d5fe5c9bd5a)
Comment 8 BogdanB 2022-01-27 22:41:22 UTC
No crash, please retest.

Version: 7.4.0.0.alpha0+ / LibreOffice Community
Build ID: 2f4f4cbeb8e50081d607b86b0475b93971c40ab8
CPU threads: 16; OS: Linux 5.13; UI render: default; VCL: gtk3
Locale: ro-RO (ro_RO.UTF-8); UI: en-US
Calc: threaded
Comment 9 QA Administrators 2024-01-28 03:13:56 UTC Comment hidden (obsolete)
Comment 10 Nicolas 2025-06-20 04:59:50 UTC
I was able to reproduce this issue on Windows 11 using LibreOffice 25.2.3.0 (x86_64).

Steps:
1. Created Sheet1 and Sheet2
2. Added the macro:
    ```
    Sub CrashTest(Event As Variant)
      Dim cell As Object
      cell = ThisComponent.Sheets.getByName("Sheet2").getCellByPosition(2,2)
      cell.Value = 4
    End Sub
    ```
3. Assigned the macro to Sheet1 > Event: Selection Changed
4. Entered values into multiple cells in Sheet1 (e.g., A1, A2, A3)
5. Pressed Ctrl + Z or Alt + Z

**Observed behavior:**
- On the first run, pressing Ctrl + Z switched focus to Sheet2 unexpectedly
- On repeated execution, LibreOffice Calc crashed

This confirms that the bug still occurs in the latest stable version (25.2.3.0) on Windows 11.
Comment 11 Wolfgang Jäger 2025-06-22 11:24:43 UTC
Created attachment 201409 [details]
The slightly changed demo file mentioned in comment by jag
Comment 12 Wolfgang Jäger 2025-06-22 11:26:22 UTC
Tests with V25.8.0.0.beta1
I didn't succeed with getting the bug reproducible, but ...
Additional note to get a situation where the bug will show with a higher probability:
Show Sheet1
Create a new window and make both winows small enough to see the relevant ranges of both sheets at the same time.
Change the selection in Sheet1. 
Play with both windows and hit Ctrl+Z now and then. 

MY result: Sooner or later I get the reported crash.

Remark: The new attachmenmt contains a small change to the event handling Sub:
The assignment for Sheet1.C3 is changed from =4 to =Now().
The number format string applied to the cell is now YYYY-MM-DD HH:MM:SS

Additional obervation: 
The time needed for the loading of the small demo file and getting to the point where permission for macros is aked for is absurdly long.