Bug 96038 - Shell function does not work whith .bat files and paths containing spaces
Summary: Shell function does not work whith .bat files and paths containing spaces
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
3.5.0 release
Hardware: All Windows (All)
: medium minor
Assignee: Not Assigned
URL:
Whiteboard: target:6.3.0 target:6.2.3
Keywords:
Depends on:
Blocks:
 
Reported: 2015-11-24 12:13 UTC by Roland Baudin
Modified: 2019-03-18 20:18 UTC (History)
1 user (show)

See Also:
Crash report or crash signature:


Attachments
Patch for LibreOffice 6.2.1 (359 bytes, patch)
2019-03-13 09:40 UTC, Roland Baudin
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Roland Baudin 2015-11-24 12:13:13 UTC
On Windows, in basic macros, the shell() function does not work when executing a .bat file and when the path contains spaces. The usual ConvertToURL() trick does not work when executing .bat files.

To reproduce the bug:

1. Create a directory C:\Temp\Temp Folder

2. Create a test.bat file with the following content:

echo Test
pause

and copy that file to C:\Temp and C:\Temp\Temp Folder

3. Copy C:\Windows\write.exe to C:\Temp and C:\Temp Folder to get an .exe test case

4. Create the following basic macro:

Sub Test

' Works
'Shell(ConvertToURL("C:\Temp\test.bat"), 1)

'Works
'Shell(ConvertToURL("C:\Temp\write.exe"), 1)

' Works
'Shell(ConvertToURL("C:\Temp\Test Folder\write.exe"), 1)

' Does not work
Shell(ConvertToURL("C:\Temp\Test Folder\test.bat"), 1)

' Works
'Shell("cmd.exe", 1, "/c " & "C:\Temp\write.exe")

' Does not work
'Shell("cmd.exe /c", 1, "C:\Temp\test.bat")

' Works
'Shell("cmd.exe /c", 1, """C:\Temp\Test Folder\write.exe""")

' Does not work
'Shell("cmd.exe /c", 1, """C:\Temp\Test Folder\test.bat""")

End Sub

5. Execute the macro and check that it's only possible to execute test.bat when the path does not contain space. On the contrary, executing write.exe is possible whatever the path contains spaces or not.
Comment 1 Buovjaga 2015-11-28 18:28:01 UTC
Reproduced.

Win 7 Pro 64-bit, Version: 5.0.3.2 (x64)
Build ID: e5f16313668ac592c1bfb310f4390624e3dbfb75
Locale: fi-FI (fi_FI)

LibreOffice 3.5.0rc3 
Build ID: 7e68ba2-a744ebf-1f241b7-c506db1-7d53735
Comment 2 QA Administrators 2017-01-03 19:36:44 UTC Comment hidden (obsolete)
Comment 3 Roland Baudin 2017-01-04 12:40:14 UTC
(In reply to QA Administrators from comment #2)

I tested with LibreOffice 5.2.4 under Windows 7 64 bits SP1 and the bug is still there (100% reproducible).
Comment 4 QA Administrators 2018-01-05 03:40:45 UTC Comment hidden (obsolete)
Comment 5 Roland Baudin 2018-01-16 08:21:22 UTC
I tested with LO Version: 5.4.4.2 (x64)
Build ID: 2524958677847fb3bb44820e40380acbe820f960
Threads CPU : 2; OS : Windows 6.1; UI Render : par défaut; 
Locale : fr-FR (fr_FR); Calc: group

The bug is still there.
100% reproducible!
Comment 6 Roland Baudin 2018-01-16 08:22:40 UTC
Note: this issue is problematic for the TexMaths extension, where I used a dirty workaround to bypass the issue.
Please fix!
Comment 7 Gibtnix 2018-04-23 16:59:40 UTC Comment hidden (no-value)
Comment 8 Roland Baudin 2019-02-06 12:51:55 UTC Comment hidden (no-value)
Comment 9 Roland Baudin 2019-03-13 09:40:48 UTC
Created attachment 149926 [details]
Patch for LibreOffice 6.2.1
Comment 10 Roland Baudin 2019-03-13 09:41:34 UTC
Looking at the LibreOffice source code, I finally found the cause of the issue and I attach a proposed patch to this bug report.

Here is the explanation: in the Windows version of LibreOffice, when calling the basic Shell(...) function (located in basic/source/runtime/methods.cxx), a function is_batch_file() is called in turn, to check if the program file path to execute is a batch or an executable.

The function is_batch_file() is located in sal/osl/w32/procimpl.cxx and simply checks if the file extension is equal to bat (or cmd or btm).

This works as expected, except when the file path contains space characters. In that case, the file path is *quoted* before the call to is_batch_file() and for a batch file the file extension becomes bat" (note the quote) instead of bat, and thus the call to is_batch_file() wrongly returns false in that case.

I fixed this by simply adding a comparison to bat", cmd" and btm".

The proposed patch is for LibreOffice 6.2.1. I built the patched version in Windows and checked that the issue has gone.
Comment 11 Buovjaga 2019-03-13 11:26:46 UTC
Great, can you submit your patch to gerrit for review? https://wiki.documentfoundation.org/Development/gerrit

Please use the master branch for your submission.
Comment 12 Roland Baudin 2019-03-13 22:01:25 UTC
(In reply to Buovjaga from comment #11)
> Great, can you submit your patch to gerrit for review?
> https://wiki.documentfoundation.org/Development/gerrit
> 
> Please use the master branch for your submission.

I submitted the patch (in master branch) using gerrit. I hope I did it well because it was much more complicated than fixing the bug!
Comment 13 Buovjaga 2019-03-13 22:12:46 UTC
(In reply to Roland Baudin from comment #12)
> (In reply to Buovjaga from comment #11)
> > Great, can you submit your patch to gerrit for review?
> > https://wiki.documentfoundation.org/Development/gerrit
> > 
> > Please use the master branch for your submission.
> 
> I submitted the patch (in master branch) using gerrit. I hope I did it well
> because it was much more complicated than fixing the bug!

Yeah, it is a bit of a hurdle :)

About your patch, I hope you won't get frustrated, but it would be best, if you changed the first line of the commit message to:

tdf#96038 Make shell function work with paths containing spaces

You can do it by giving the command

git commit --amend

and then editing the old message and saving.

Then you can just submit the patch again and it will become a new version of the same patch. It matches it up with the help of the "Change-Id" line.
Comment 14 Roland Baudin 2019-03-14 21:39:26 UTC
(In reply to Buovjaga from comment #13)
> Then you can just submit the patch again and it will become a new version of
> the same patch. It matches it up with the help of the "Change-Id" line.

Done! I hope I did it the right way now...
Comment 15 Buovjaga 2019-03-14 22:01:11 UTC
(In reply to Roland Baudin from comment #14)
> (In reply to Buovjaga from comment #13)
> > Then you can just submit the patch again and it will become a new version of
> > the same patch. It matches it up with the help of the "Change-Id" line.
> 
> Done! I hope I did it the right way now...

Yep! I see Stephan had a wish: "While this works, it feels a bit hackish ;) I'd prefer to instead change get_file_extension to return the extension of the unquoted pathname."

If you look into that and edit the file you can do the git add <file>, git commit --amend and submit to gerrit again.
Comment 16 Roland Baudin 2019-03-16 10:24:34 UTC
(In reply to Buovjaga from comment #15)
> (In reply to Roland Baudin from comment #14)
> > (In reply to Buovjaga from comment #13)
> > > Then you can just submit the patch again and it will become a new version of
> > > the same patch. It matches it up with the help of the "Change-Id" line.
> > 
> > Done! I hope I did it the right way now...
> 
> Yep! I see Stephan had a wish: "While this works, it feels a bit hackish ;)
> I'd prefer to instead change get_file_extension to return the extension of
> the unquoted pathname."
> 
> If you look into that and edit the file you can do the git add <file>, git
> commit --amend and submit to gerrit again.

OK, I submitted a new version of the patch that does (hopefully) what Stephan requested.
Comment 17 Commit Notification 2019-03-18 14:21:31 UTC
Roland Baudin committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/+/cf89d6dbfd72e60e459b2ffef313a6d8b477857b%5E%21

tdf#96038 Make shell function work with paths containing spaces

It will be available in 6.3.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 18 Commit Notification 2019-03-18 20:18:16 UTC
Roland Baudin committed a patch related to this issue.
It has been pushed to "libreoffice-6-2":

https://git.libreoffice.org/core/+/8ecd161ac9c36bcabd98c6d5e22c72fbb8d03502%5E%21

tdf#96038 Make shell function work with paths containing spaces

It will be available in 6.2.3.

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.