From 16a00074c695e36d468d2701512f7adfec8b4b70 Mon Sep 17 00:00:00 2001 From: Justin Viiret Date: Wed, 26 Apr 2017 17:22:22 +1000 Subject: [PATCH] verify_types: throw on failure In release builds, we would like a verify_u32 (etc) failure to be more than just an assertion. --- src/util/verify_types.h | 55 ++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/src/util/verify_types.h b/src/util/verify_types.h index 98c24c99..148b4377 100644 --- a/src/util/verify_types.h +++ b/src/util/verify_types.h @@ -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: @@ -30,45 +30,50 @@ #define UTIL_VERIFY_TYPES #include "ue2common.h" +#include "util/compile_error.h" #include namespace ue2 { -template -static UNUSED u8 verify_u8(Int_T val) { - assert(val == (Int_T)((u8)val)); // there and back again - return (u8)(val); +template +To_T verify_cast(From_T val) { + To_T conv_val = static_cast(val); + if (static_cast(conv_val) != val) { + assert(0); + throw ResourceLimitError(); + } + return conv_val; } -template -static UNUSED s8 verify_s8(Int_T val) { - assert(val == (Int_T)((s8)val)); // there and back again - return (s8)(val); +template +s8 verify_s8(T val) { + return verify_cast(val); } -template -static UNUSED s16 verify_s16(Int_T val) { - assert(val == (Int_T)((s16)val)); // there and back again - return (s16)(val); +template +u8 verify_u8(T val) { + return verify_cast(val); } -template -static UNUSED u16 verify_u16(Int_T val) { - assert(val == (Int_T)((u16)val)); // there and back again - return (u16)(val); +template +s16 verify_s16(T val) { + return verify_cast(val); } -template -static UNUSED s32 verify_s32(Int_T val) { - assert(val == (Int_T)((s32)val)); // there and back again - return (s32)(val); +template +u16 verify_u16(T val) { + return verify_cast(val); } -template -static UNUSED u32 verify_u32(Int_T val) { - assert(val == (Int_T)((u32)val)); // there and back again - return (u32)(val); +template +s32 verify_s32(T val) { + return verify_cast(val); +} + +template +u32 verify_u32(T val) { + return verify_cast(val); } } // namespace ue2