Bug 96097 - FILESAVE: Autosave or Save a copy on calc with signed macro breaks the next save
Summary: FILESAVE: Autosave or Save a copy on calc with signed macro breaks the next save
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Calc (show other bugs)
Version:
(earliest affected)
4.3.5.2 release
Hardware: x86-64 (AMD64) All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard: target:5.2.0 target:5.1.4
Keywords: patch
Depends on:
Blocks:
 
Reported: 2015-11-27 07:34 UTC by Guillaume Smaha
Modified: 2016-10-25 19:01 UTC (History)
3 users (show)

See Also:
Crash report or crash signature:


Attachments
Test file (10.85 KB, application/vnd.oasis.opendocument.spreadsheet)
2015-11-27 07:34 UTC, Guillaume Smaha
Details
Remove pMedium->CloseStorage() fix the issue (774 bytes, patch)
2016-04-14 09:10 UTC, Guillaume Smaha
Details
Call CanDisposeStorage_Impl may be more proper than delete the 2 CloseStorage (930 bytes, patch)
2016-04-14 09:11 UTC, Guillaume Smaha
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Guillaume Smaha 2015-11-27 07:34:53 UTC
Created attachment 120835 [details]
Test file

Overview:
After save a copy or an autosave, when you try to save, this error occurs "The File could not be written".

Steps to Reproduce:
The document only contains an empty macro which is signed.
1) Open the document
2) Enable or disable macro (not important)
3) Save a copy or edit and wait for autosave
4) Save


Actual Results:
Impossible to save after an autosave or a save a copy

Build Date & Hardware:
Linux (4.2.0-18-generic #22-Ubuntu SMP) - 5.1 (master)
Linux (4.2.0-18-generic #22-Ubuntu SMP) - 5.0 RC
Linux (4.2.0-18-generic #22-Ubuntu SMP) - 4.4.7
Linux (4.2.0-18-generic #22-Ubuntu SMP) - 4.3.7
Windows - 4.3.7
Windows - 4.3.5



Additional Information:

The issues occurs on sfx2/source/doc/objstor.cxx:1171

try {
uno::Reference < beans::XPropertySet > xPropSet( GetStorage(), uno::UNO_QUERY_THROW );

because (DocShell *)this -> (SfxMedium *)pMedium -> (SfxMedium_Impl *)pImp -> (OStorage *)xStorage -> (OStorage_Impl *)m_pImpl is NULL
where (DocShell *) this->pImp->m_xDocStorage = pMedium->pImp->xStorage.


After some researchs, with bTryToPreserveScriptSignature = true (signed macro), this is the 2 calls of pMedium->Close(); in objstor.cxx (line 1597 and 1656) which call dispose() function of OStorage object and set pMedium->pImp->xStorage->m_pImpl to NULL.
Deleting the 2 calls fix the issues on Lubuntu and Windows but it's maybe improper to delete these Close calls.


So, I tried some cases to save the calc file to understand the issue:    breakpoint on SfxObjectShell::SaveTo_Impl


1) Save Ctrl+S (before autosave):
Enter in the section in objstor.cxx from line 1230 to 1262
Call DisconnectStorage_Impl, pMedium->pImp->bDisposeStorage is set to false.
So dispose() will be not called in pMedium->Close();.

After the save:
pImp->m_xDocStorage-> = pMedium (same)
pImp->m_xDocStorage->m_pImpl = (OStorage_Impl *) 0x2faf5f0
     
Then, no error occurs.


2) Save a copy (before autosave) or Autosave:
Enter in the section in objstor.cxx from line 1312 to 1323
No call to Disconnect so pMedium->pImp->bDisposeStorage = true
When pMedium->Close(); is called, pMedium->pImp->xStorage->m_pImpl is set to NULL.
  
After save:
pImp->m_xDocStorage-> = pMedium (same)
pImp->m_xDocStorage->m_pImpl = NULL
      
Then, error occurs on the next save.
    
    
3) Save As (before autosave):
Enter in the section in objstor.cxx from line 1312 to 1323
No call to Disconnect so pMedium->pImp->bDisposeStorage = true
When pMedium->Close(); is called, pMedium->pImp->xStorage->m_pImpl is set to NULL.
        
pImp->m_xDocStorage-> = rMedium (new SfxMedium)
pImp->m_xDocStorage->m_pImpl = NULL
        
Then, no error occurs on the next save because pMedium is replaced by the target SfxMedium.
    

I tried to make a patch adding after (line 1660) AddLog( OSL_LOG_PREFIX "SaveAs/Export" );

if ( bCopyTo )
{
    pMedium->CanDisposeStorage_Impl( false );
}

The patch works perfectly on Lubuntu but a new error occurs on Windows :
 - During a save a copy, a popup indicate that the file opened is "read only"
 - After that, on saving, an error occurs in a popup "Error saving the document. Object not accessible. The object cannot be assigned due to insufficient user right"

I don't know if this will help you but currently I don't find why an error occurs on Windows.
Comment 1 raal 2015-12-06 20:47:49 UTC
I can confirm with Version: 5.2.0.0.alpha0+
Build ID: de9d0e797903e7ecc19be2b05c7e89d5936ae02d
Threads 4; Ver: Linux 4.2; Render: default; 
TinderBox: Linux-rpm_deb-x86_64@70-TDF, Branch:master, Time: 2015-12-03_04:13:00
Comment 2 Guillaume Smaha 2016-04-14 09:10:47 UTC
Created attachment 124320 [details]
Remove pMedium->CloseStorage() fix the issue
Comment 3 Guillaume Smaha 2016-04-14 09:11:54 UTC
Created attachment 124321 [details]
Call CanDisposeStorage_Impl may be more proper than delete the 2 CloseStorage
Comment 4 Commit Notification 2016-04-28 06:23:09 UTC
Guillaume Smaha committed a patch related to this issue.
It has been pushed to "master":

http://cgit.freedesktop.org/libreoffice/core/commit/?id=d7ef7e3e4bc9c6277e5b2d08c2657fea8cf0e639

tdf#96097: Can't save a doc with signed macro after autosave/save a copy

It will be available in 5.2.0.

The patch should be included in the daily builds available at
http://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
http://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 5 Commit Notification 2016-04-28 07:22:39 UTC
Guillaume Smaha committed a patch related to this issue.
It has been pushed to "master":

http://cgit.freedesktop.org/libreoffice/core/commit/?id=4d2bb795820ef30f230605194207a3bd50299127

Unit test for tdf#96097: Can't save/pointer null

It will be available in 5.2.0.

The patch should be included in the daily builds available at
http://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
http://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 6 Commit Notification 2016-05-31 22:17:58 UTC
Guillaume Smaha committed a patch related to this issue.
It has been pushed to "libreoffice-5-1":

http://cgit.freedesktop.org/libreoffice/core/commit/?id=127c56b0501b90283c0bb2f0a06ba242186bc1ca&h=libreoffice-5-1

tdf#96097: Can't save a doc with signed macro after autosave/save a copy

It will be available in 5.1.4.

The patch should be included in the daily builds available at
http://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
http://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.