From 2a0e4c199c4f18d80ec5a2ab452f3cf18eafc56c Mon Sep 17 00:00:00 2001 From: Jean-Marc Lasgouttes Date: Fri, 24 Mar 2017 16:44:14 +0100 Subject: [PATCH] Fix coverity issues about exceptions There a some exceptions related to the fact that BOOST_ASSERT throws an unhandled exception, which is fait enough. This is handled by uploading a modeling file to coverity. The second batch of issues are related to the use of lexical_cast in convert.cpp. We use now a wrapper around boost::lexical_cast that does not throw but return empty strings instead. I am not sure actually of when lexical_cast could fail. --- development/Makefile.am | 1 + development/coverity_modeling.cpp | 13 +++++++++++++ src/support/convert.cpp | 19 +++++++++++++++++-- 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 development/coverity_modeling.cpp diff --git a/development/Makefile.am b/development/Makefile.am index 95bde438a6..ff332aabfc 100644 --- a/development/Makefile.am +++ b/development/Makefile.am @@ -9,6 +9,7 @@ SUBDIRS = cygwin endif EXTRA_DIST = coding/Rules coding/Recommendations \ + coverity_modeling.cpp \ FORMAT lyx.rpm.README \ lyxserver lyx.spec.in lyx.spec \ LyX-Mac-binary-release.sh \ diff --git a/development/coverity_modeling.cpp b/development/coverity_modeling.cpp new file mode 100644 index 0000000000..baca85466a --- /dev/null +++ b/development/coverity_modeling.cpp @@ -0,0 +1,13 @@ +// This file is a modeling file for coverity + +namespace lyx { + +// Tell coverity that this function always exits +void doAssertWithCallstack(bool value) +{ + if (!value) { + __coverity_panic__(); + } +} + +} diff --git a/src/support/convert.cpp b/src/support/convert.cpp index 076c9d7d28..f95aa4b9d4 100644 --- a/src/support/convert.cpp +++ b/src/support/convert.cpp @@ -23,9 +23,24 @@ using namespace std; -namespace lyx { +namespace { + +// A version of lexical cast that does not throw. Useful for when we convert to string +template +To lexical_cast(From const & value, To const & defaultResult = To()) +{ + try { + return boost::lexical_cast(value); + } catch(...) { + // Ignore all exceptions and use default. + return defaultResult; + } +} -using boost::lexical_cast; +} + + +namespace lyx { template<> string convert(bool b) -- 2.39.5