mirror of
https://github.com/zeek/zeek.git
synced 2025-10-06 16:48:19 +00:00
factoring out logic to check for overflows during coercions
This commit is contained in:
parent
9757d37332
commit
a67897135e
8 changed files with 89 additions and 37 deletions
39
src/Overflow.cc
Normal file
39
src/Overflow.cc
Normal file
|
@ -0,0 +1,39 @@
|
|||
// See the file "COPYING" in the main distribution directory for copyright.
|
||||
|
||||
#include "zeek/Overflow.h"
|
||||
#include "zeek/Val.h"
|
||||
|
||||
namespace zeek::detail {
|
||||
|
||||
bool would_overflow(const zeek::Type* from_type, const zeek::Type* to_type,
|
||||
const Val* val)
|
||||
{
|
||||
if ( ! to_type || ! from_type )
|
||||
return true;
|
||||
|
||||
if ( same_type(to_type, from_type) )
|
||||
return false;
|
||||
|
||||
if ( to_type->InternalType() == TYPE_INTERNAL_DOUBLE )
|
||||
return false;
|
||||
|
||||
if ( to_type->InternalType() == TYPE_INTERNAL_UNSIGNED )
|
||||
{
|
||||
if ( from_type->InternalType() == TYPE_INTERNAL_DOUBLE )
|
||||
return double_to_count_would_overflow(val->InternalDouble());
|
||||
if ( from_type->InternalType() == TYPE_INTERNAL_INT )
|
||||
return int_to_count_would_overflow(val->InternalInt());
|
||||
}
|
||||
|
||||
if ( to_type->InternalType() == TYPE_INTERNAL_INT )
|
||||
{
|
||||
if ( from_type->InternalType() == TYPE_INTERNAL_DOUBLE )
|
||||
return double_to_int_would_overflow(val->InternalDouble());
|
||||
if ( from_type->InternalType() == TYPE_INTERNAL_UNSIGNED )
|
||||
return count_to_int_would_overflow(val->InternalUnsigned());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue