]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
verify_types: throw on failure
authorJustin Viiret <justin.viiret@intel.com>
Wed, 26 Apr 2017 07:22:22 +0000 (17:22 +1000)
committerMatthew Barr <matthew.barr@intel.com>
Tue, 30 May 2017 03:58:59 +0000 (13:58 +1000)
In release builds, we would like a verify_u32 (etc) failure to be more
than just an assertion.

src/util/verify_types.h

index 98c24c997869be900afcd22a82778be34cc39618..148b437716b18bc54fb27432089495d59ff4aabf 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Intel Corporation
+ * Copyright (c) 2015-2017, Intel Corporation
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
 #define UTIL_VERIFY_TYPES
 
 #include "ue2common.h"
+#include "util/compile_error.h"
 
 #include <cassert>
 
 namespace ue2 {
 
-template<typename Int_T>
-static UNUSED u8 verify_u8(Int_T val) {
-    assert(val == (Int_T)((u8)val)); // there and back again
-    return (u8)(val);
+template<typename To_T, typename From_T>
+To_T verify_cast(From_T val) {
+    To_T conv_val = static_cast<To_T>(val);
+    if (static_cast<From_T>(conv_val) != val) {
+        assert(0);
+        throw ResourceLimitError();
+    }
+    return conv_val;
 }
 
-template<typename Int_T>
-static UNUSED s8 verify_s8(Int_T val) {
-    assert(val == (Int_T)((s8)val)); // there and back again
-    return (s8)(val);
+template<typename T>
+s8 verify_s8(T val) {
+    return verify_cast<s8>(val);
 }
 
-template<typename Int_T>
-static UNUSED s16 verify_s16(Int_T val) {
-    assert(val == (Int_T)((s16)val)); // there and back again
-    return (s16)(val);
+template<typename T>
+u8 verify_u8(T val) {
+    return verify_cast<u8>(val);
 }
 
-template<typename Int_T>
-static UNUSED u16 verify_u16(Int_T val) {
-    assert(val == (Int_T)((u16)val)); // there and back again
-    return (u16)(val);
+template<typename T>
+s16 verify_s16(T val) {
+    return verify_cast<s16>(val);
 }
 
-template<typename Int_T>
-static UNUSED s32 verify_s32(Int_T val) {
-    assert(val == (Int_T)((s32)val)); // there and back again
-    return (s32)(val);
+template<typename T>
+u16 verify_u16(T val) {
+    return verify_cast<u16>(val);
 }
 
-template<typename Int_T>
-static UNUSED u32 verify_u32(Int_T val) {
-    assert(val == (Int_T)((u32)val)); // there and back again
-    return (u32)(val);
+template<typename T>
+s32 verify_s32(T val) {
+    return verify_cast<s32>(val);
+}
+
+template<typename T>
+u32 verify_u32(T val) {
+    return verify_cast<u32>(val);
 }
 
 } // namespace ue2