troldal / OpenXLSX

A C++ library for reading, writing, creating and modifying Microsoft Excel® (.xlsx) files.
BSD 3-Clause "New" or "Revised" License
1.33k stars 316 forks source link

crash when trying to save to xlsx while running executable from Program Files folder #136

Open krystianMichalak opened 2 years ago

krystianMichalak commented 2 years ago

I have run into a crash after adding openxlsx to our apllication. By default on windows application are installed in Program Files folder which requires administrator priviliges to modify its content. The crash happens because in zippy there is a temp file created with the same path as running exe. Check out: external/zippy/zippy.hpp l:10736

And because it is not possible to create a file in this location if application is running with user priviliges(by default) it throws an exception.

In my opinion it can be improved by using default temp directory for the temp file.

Here are changes I have made to fix this issue:

https://github.com/krystianMichalak/OpenXLSX/commit/f1f426c636190fd9012d7ed5b9e752786aa4785f

punk-floyd commented 1 year ago

Just ran into this, myself.

The problem is that the tempPath generation is looking for a terminal '/' but most Windows-based paths use '\' as the file separator. For Windows builds, you probably want to use std::string::find_last_of("/\\"), or maybe std::filesystem::path facilities if C++17 is okay.

Also, the actual error is being lost because the return value of mz_zip_writer_init_file isn't being checked. You can fix that with:

if (!mz_zip_writer_init_file(&tempArchive, tempPath.c_str(), 0))
    throw ZipRuntimeError(mz_zip_get_error_string(tempArchive.m_last_error));

Also note that this bug is also in the KZip/KZip.cpp implementation.