]> OCCT Git - occt-copy.git/commitdiff
Added building option OCCT_DEBUG_SANITIZE_EXCEPTIONS for performing expensive checks
authorkgv <kgv@opencascade.com>
Fri, 15 Mar 2019 10:16:06 +0000 (13:16 +0300)
committerkgv <kgv@opencascade.com>
Sun, 24 Mar 2019 07:32:04 +0000 (10:32 +0300)
When this option is ON, opencascade::handle throws std::runtime_error() on NULL pointer dereference,
and Standard_OutOfRange_Raise_if throws std::runtime_error() instead of Standard_OutOfRange
to detect cases when broken code remains hidden by exception handling.

CMakeLists.txt
adm/cmake/vardescr.cmake
src/OSD/OSD_signal.cxx
src/Standard/Standard_Handle.hxx
src/Standard/Standard_OutOfRange.hxx

index dc64c3c2bd0f7cf589bac224f352791d7e156b01..3768892b5fb6aedffb28837148a44b449ee86cb8 100644 (file)
@@ -91,6 +91,17 @@ if (NOT DEFINED BUILD_RELEASE_DISABLE_EXCEPTIONS)
   set (BUILD_RELEASE_DISABLE_EXCEPTIONS ON CACHE BOOL "${BUILD_RELEASE_DISABLE_EXCEPTIONS_DESCR}")
 endif()
 
+# option enabling extra exceptions (OCCT_DEBUG_SANITIZE_EXCEPTIONS)
+if (NOT DEFINED BUILD_ENABLE_SANITIZE_EXCEPTIONS)
+  set (BUILD_ENABLE_SANITIZE_EXCEPTIONS OFF CACHE BOOL "${BUILD_ENABLE_SANITIZE_EXCEPTIONS_DESCR}")
+endif()
+
+if (BUILD_ENABLE_SANITIZE_EXCEPTIONS)
+  add_definitions (-DOCCT_DEBUG_SANITIZE_EXCEPTIONS)
+endif()
+# DEBUG
+add_definitions (-DOCCT_DEBUG_SANITIZE_EXCEPTIONS)
+
 # option to enable or disable use of precompiled headers
 if (NOT DEFINED BUILD_USE_PCH)
   set (BUILD_USE_PCH OFF CACHE BOOL "${BUILD_USE_PCH_DESCR}")
index 29fa030815cb3995c60bd0ac8424988effc422ef..6a8f2cf06702b0b5d738e65c63087b54f0b8aba7 100644 (file)
@@ -33,6 +33,10 @@ set (BUILD_RELEASE_DISABLE_EXCEPTIONS_DESCR
 Defines No_Exception macros for Release builds when enabled (default).
 These exceptions are always enabled in Debug builds, but disable in Release for better performance")
 
+set (BUILD_ENABLE_SANITIZE_EXCEPTIONS_DESCR
+"Enables extra exceptions for detecting broken code. Should NOT be used for production.
+Defines OCCT_DEBUG_SANITIZE_EXCEPTIONS macros when enabled (OFF by default).")
+
 set (BUILD_ENABLE_FPE_SIGNAL_HANDLER_DESCR
 "Enable/Disable the floating point exceptions (FPE) during DRAW execution only.
 Corresponding environment variable (CSF_FPE) can be changed manually
index 79031e62877eef3a4776efb47cb8e4b36f703afb..c9eba4cd486d3feaa11ae7f9e58984ca4cd8dedc 100644 (file)
@@ -93,10 +93,14 @@ static LONG _osd_debug   ( void );
 //# define _OSD_FPX ( _EM_INVALID | _EM_DENORMAL | _EM_ZERODIVIDE | _EM_OVERFLOW | _EM_UNDERFLOW )
 # define _OSD_FPX ( _EM_INVALID | _EM_DENORMAL | _EM_ZERODIVIDE | _EM_OVERFLOW )
 
+#ifdef OCCT_DEBUG_SANITIZE_EXCEPTIONS
+  #define THROW_OR_JUMP(Type,Message) throw std::runtime_error (Message)
+#else
 #ifdef OCC_CONVERT_SIGNALS
-#define THROW_OR_JUMP(Type,Message) Type::NewInstance(Message)->Jump()
+  #define THROW_OR_JUMP(Type,Message) Type::NewInstance(Message)->Jump()
 #else
-#define THROW_OR_JUMP(Type,Message) throw Type(Message)
+  #define THROW_OR_JUMP(Type,Message) throw Type(Message)
+#endif
 #endif
 
 //=======================================================================
index 9e7a3a6a5e4436b9bf60cd40d7b5d7a118771328..e22e4bc869315dd794708c4a6414d822695ed591 100644 (file)
 
 #include <type_traits>
 
+#ifdef OCCT_DEBUG_SANITIZE_EXCEPTIONS
+  #include <stdexcept>
+#endif
+
 class Standard_Transient;
 
 //! Namespace opencascade is intended for low-level template classes and functions
@@ -181,10 +185,28 @@ namespace opencascade {
     T* get() const { return static_cast<T*>(this->entity); }
 
     //! Member access operator (note non-const)
-    T* operator-> () const { return static_cast<T*>(this->entity); }
+    T* operator-> () const
+    {
+    #ifdef OCCT_DEBUG_SANITIZE_EXCEPTIONS
+      if (entity == 0)
+      {
+        throw std::runtime_error ("null pointer exception");
+      }
+    #endif
+      return static_cast<T*>(this->entity);
+    }
 
     //! Dereferencing operator (note non-const)
-    T& operator* () const { return *get(); }
+    T& operator* () const
+    {
+    #ifdef OCCT_DEBUG_SANITIZE_EXCEPTIONS
+      if (entity == 0)
+      {
+        throw std::runtime_error ("null pointer exception");
+      }
+    #endif
+      return *get();
+    }
 
     //! Check for equality
     template <class T2>
index 0d2cf80a95623ce8d7a040bfa942bfdc1cc15ad9..68ad9be015c3c6746c7d29959f6dcbafcb652b59 100644 (file)
 class Standard_OutOfRange;
 DEFINE_STANDARD_HANDLE(Standard_OutOfRange, Standard_RangeError)
 
-#if !defined No_Exception && !defined No_Standard_OutOfRange
+#ifdef OCCT_DEBUG_SANITIZE_EXCEPTIONS
+#define Standard_OutOfRange_Raise_if(CONDITION, MESSAGE) \
+  if (CONDITION) throw std::runtime_error (MESSAGE);
+#elif !defined No_Exception && !defined No_Standard_OutOfRange
 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
   // suppress false-positive warnings produced by GCC optimizer
   #define Standard_OutOfRange_Raise_if(CONDITION, MESSAGE) \