# HG changeset patch # User Markus Mützel # Date 1585295553 -3600 # Node ID b895daca20e2bbc20a6e4e59f5826450526a405f # Parent 65ddd575a5f5c14c0bd806a7e6599c90babc588e Avoid potential lifetime issues with temporary std::wstring objects. * sysdep.cc (F__open_with_system_app__, get_regkey_value, get_regkey_names), file-ops.cc (rename), lo-sysdep.cc (get_dirlist, fopen, getenv_wrapper), oct-shlib.cc (octave_w32_shlib): Explicitly create std::wstring object with well-defined lifetime when passing the pointer to the underlying c_str to a function. diff -r 65ddd575a5f5 -r b895daca20e2 libinterp/corefcn/sysdep.cc --- a/libinterp/corefcn/sysdep.cc Wed Mar 25 08:24:13 2020 -0700 +++ b/libinterp/corefcn/sysdep.cc Fri Mar 27 08:52:33 2020 +0100 @@ -218,9 +218,8 @@ std::string file = args(0).xstring_value ("__open_with_system_app__: argument must be a filename"); #if defined (OCTAVE_USE_WINDOWS_API) - HINSTANCE status - = ShellExecuteW (0, 0, octave::sys::u8_to_wstring (file).c_str (), - 0, 0, SW_SHOWNORMAL); + std::wstring wfile = octave::sys::u8_to_wstring (file); + HINSTANCE status = ShellExecuteW (0, 0, wfile.c_str (), 0, 0, SW_SHOWNORMAL); // ShellExecute returns a value greater than 32 if successful. return octave_value (reinterpret_cast (status) > 32); @@ -821,9 +820,10 @@ LONG result; HKEY h_subkey; - result = RegOpenKeyExW (h_rootkey, - sys::u8_to_wstring (subkey).c_str (), 0, - KEY_READ, &h_subkey); + std::wstring wsubkey = sys::u8_to_wstring (subkey); + result = RegOpenKeyExW (h_rootkey, wsubkey.c_str (), 0, KEY_READ, + &h_subkey); + if (result != ERROR_SUCCESS) return result; @@ -831,18 +831,17 @@ frame.add_fcn (reg_close_key_wrapper, h_subkey); + std::wstring wname = sys::u8_to_wstring (name); DWORD length = 0; - result = RegQueryValueExW (h_subkey, - sys::u8_to_wstring (name).c_str (), - nullptr, nullptr, nullptr, &length); + result = RegQueryValueExW (h_subkey, wname.c_str (), nullptr, nullptr, + nullptr, &length); if (result != ERROR_SUCCESS) return result; DWORD type = 0; OCTAVE_LOCAL_BUFFER (BYTE, data, length); - result = RegQueryValueExW (h_subkey, - sys::u8_to_wstring (name).c_str (), - nullptr, &type, data, &length); + result = RegQueryValueExW (h_subkey, wname.c_str (), nullptr, &type, + data, &length); if (result != ERROR_SUCCESS) return result; @@ -863,9 +862,9 @@ fields.clear (); - retval = RegOpenKeyExW (h_rootkey, - sys::u8_to_wstring (subkey).c_str (), 0, - KEY_READ, &h_subkey); + std::wstring wsubkey = sys::u8_to_wstring (subkey); + retval = RegOpenKeyExW (h_rootkey, wsubkey.c_str (), 0, KEY_READ, + &h_subkey); if (retval != ERROR_SUCCESS) return retval; diff -r 65ddd575a5f5 -r b895daca20e2 liboctave/system/file-ops.cc --- a/liboctave/system/file-ops.cc Wed Mar 25 08:24:13 2020 -0700 +++ b/liboctave/system/file-ops.cc Fri Mar 27 08:52:33 2020 +0100 @@ -516,8 +516,9 @@ msg = ""; #if defined (OCTAVE_USE_WINDOWS_API) - status = _wrename (u8_to_wstring (from).c_str (), - u8_to_wstring (to).c_str ()); + std::wstring wfrom = u8_to_wstring (from); + std::wstring wto = u8_to_wstring (to); + status = _wrename (wfrom.c_str (), wto.c_str ()); #else status = std::rename (from.c_str (), to.c_str ()); #endif diff -r 65ddd575a5f5 -r b895daca20e2 liboctave/system/lo-sysdep.cc --- a/liboctave/system/lo-sysdep.cc Wed Mar 25 08:24:13 2020 -0700 +++ b/liboctave/system/lo-sysdep.cc Fri Mar 27 08:52:33 2020 +0100 @@ -102,8 +102,8 @@ path_name.append (R"(\*)"); // Find first file in directory. - HANDLE hFind = FindFirstFileW (u8_to_wstring (path_name).c_str (), - &ffd); + std::wstring wpath_name = u8_to_wstring (path_name); + HANDLE hFind = FindFirstFileW (wpath_name.c_str (), &ffd); if (INVALID_HANDLE_VALUE == hFind) { DWORD errCode = GetLastError (); @@ -153,8 +153,9 @@ fopen (const std::string& filename, const std::string& mode) { #if defined (OCTAVE_USE_WINDOWS_API) - return _wfopen (u8_to_wstring (filename).c_str (), - u8_to_wstring (mode).c_str ()); + std::wstring wfilename = u8_to_wstring (filename); + std::wstring wmode = u8_to_wstring (mode); + return _wfopen (wfilename.c_str (), wmode.c_str ()); #else return std::fopen (filename.c_str (), mode.c_str ()); #endif @@ -197,7 +198,8 @@ getenv_wrapper (const std::string& name) { #if defined (OCTAVE_USE_WINDOWS_API) - wchar_t *env = _wgetenv (u8_to_wstring (name).c_str ()); + std::wstring wname = u8_to_wstring (name); + wchar_t *env = _wgetenv (wname.c_str ()); return env ? u8_from_wstring (env) : ""; #else char *env = ::getenv (name.c_str ()); diff -r 65ddd575a5f5 -r b895daca20e2 liboctave/util/oct-shlib.cc --- a/liboctave/util/oct-shlib.cc Wed Mar 25 08:24:13 2020 -0700 +++ b/liboctave/util/oct-shlib.cc Fri Mar 27 08:52:33 2020 +0100 @@ -301,10 +301,12 @@ } std::string dir = sys::file_ops::dirname (f); + std::wstring wdir = sys::u8_to_wstring (dir); SetDllDirectoryW (dir.empty () - ? nullptr : sys::u8_to_wstring (dir).c_str ()); + ? nullptr : wdir.c_str ()); - m_handle = LoadLibraryW (sys::u8_to_wstring (m_file).c_str ()); + std::wstring wfile = sys::u8_to_wstring (m_file); + m_handle = LoadLibraryW (wfile.c_str ()); SetDllDirectoryW (nullptr);