From 522f3517e1d7f61ed2bbcafe0632f50cb3e8ae2f Mon Sep 17 00:00:00 2001 From: Jean-Marc Lasgouttes Date: Mon, 27 Mar 2017 16:08:22 +0200 Subject: [PATCH] Handle properly exception that can be thrown by to_local8bit Remove the use of this function in GuiAlert. This was spotted by coverity --- src/frontends/qt4/GuiAlert.cpp | 5 ++--- src/support/environment.cpp | 8 +++++++- src/support/os_cygwin.cpp | 10 +++++++++- src/support/os_win32.cpp | 10 +++++++++- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/frontends/qt4/GuiAlert.cpp b/src/frontends/qt4/GuiAlert.cpp index 8e12b3eb97..6bd5e615fc 100644 --- a/src/frontends/qt4/GuiAlert.cpp +++ b/src/frontends/qt4/GuiAlert.cpp @@ -211,9 +211,8 @@ void doError(docstring const & title0, docstring const & message, bool backtrace << toPlainText(message) << endl; QString details; - if (backtrace) { - details = QString::fromLocal8Bit(to_local8bit(printCallStack()).c_str()); - } + if (backtrace) + details = toqstr(printCallStack()); if (!use_gui) return; diff --git a/src/support/environment.cpp b/src/support/environment.cpp index 091fa278c0..e39fd3449e 100644 --- a/src/support/environment.cpp +++ b/src/support/environment.cpp @@ -57,7 +57,13 @@ bool setEnv(string const & name, string const & value) // CHECK Look at and fix this. // f.ex. what about error checking? - string const encoded = to_local8bit(from_utf8(value)); + string encoded; + try { + encoded = to_local8bit(from_utf8(value)); + } catch (...) { + return false; + } + #if defined (HAVE_SETENV) return ::setenv(name.c_str(), encoded.c_str(), 1) == 0; #elif defined (HAVE_PUTENV) diff --git a/src/support/os_cygwin.cpp b/src/support/os_cygwin.cpp index 3464c04d78..c2bdb387a6 100644 --- a/src/support/os_cygwin.cpp +++ b/src/support/os_cygwin.cpp @@ -456,8 +456,16 @@ bool autoOpenFile(string const & filename, auto_open_mode const mode, cygwin_internal(CW_SYNC_WINENV); } + string win_path; + try { + win_path = to_local8bit(from_utf8(convert_path(filename, PathStyle(windows)))); + } catch (...) { + LYXERR0("Cannot encode file name `" << filename << "' to local 8 bit encoding"); + return false; + } + + // reference: http://msdn.microsoft.com/en-us/library/bb762153.aspx - string const win_path = to_local8bit(from_utf8(convert_path(filename, PathStyle(windows)))); char const * action = (mode == VIEW) ? "open" : "edit"; bool success = reinterpret_cast(ShellExecute(NULL, action, win_path.c_str(), NULL, NULL, 1)) > 32; diff --git a/src/support/os_win32.cpp b/src/support/os_win32.cpp index a6e5da7c5a..e947b7f42b 100644 --- a/src/support/os_win32.cpp +++ b/src/support/os_win32.cpp @@ -567,10 +567,18 @@ bool autoOpenFile(string const & filename, auto_open_mode const mode, setEnv("TEXFONTS", newtexfonts); } + string fname8; + try { + fname8 = to_local8bit(from_utf8(filename)); + } catch (...) { + LYXERR0("Cannot encode file name `" << filename << "' to local 8 bit encoding"); + return false; + } + // reference: http://msdn.microsoft.com/en-us/library/bb762153.aspx char const * action = (mode == VIEW) ? "open" : "edit"; bool success = reinterpret_cast(ShellExecute(NULL, action, - to_local8bit(from_utf8(filename)).c_str(), NULL, NULL, 1)) > 32; + fname8.c_str(), NULL, NULL, 1)) > 32; if (!path.empty() && !lyxrc.texinputs_prefix.empty()) { setEnv("TEXINPUTS", oldtexinputs); -- 2.39.5