Exiv2
Loading...
Searching...
No Matches
Classes | Functions
Safe::Internal Namespace Reference

Helper functions for providing integer overflow checks. More...

Classes

struct  enable_if
 Helper struct for SFINAE, from C++11. More...
 
struct  enable_if< true, T >
 Specialization of enable_if for the case B == true. More...
 
struct  is_signed
 Helper struct to determine whether a type is signed or unsigned. More...
 

Functions

template<typename T >
enable_if< is_signed< T >::VALUE &&sizeof(T)>=sizeof(int), bool >::type fallback_add_overflow (T summand_1, T summand_2, T &result)
 Check the addition of two numbers for overflows for signed integer types larger than int or with the same size as int.
 

Detailed Description

Helper functions for providing integer overflow checks.

This namespace contains internal helper functions fallback_$op_overflow and builtin_$op_overflow (where $op is an arithmetic operation like add, subtract, etc.). Both provide the following interface:

bool fallback/builtin_$op_overflow(T first, T second, T& result);

where T is an integer type.

Each function performs checks whether first $op second can be safely performed without overflows. If yes, the result is saved in result and false is returned. Otherwise true is returned and the contents of result are unspecified.

fallback_$op_overflow implements a portable but slower overflow check. builtin_$op_overflow uses compiler builtins (when available) and should be faster. As builtins are not available for all types, builtin_$op_overflow falls back to fallback_$op_overflow when no builtin is available.

Function Documentation

◆ fallback_add_overflow()

template<typename T >
enable_if< is_signed< T >::VALUE &&sizeof(T)>=sizeof(int), bool >::type Safe::Internal::fallback_add_overflow ( T summand_1,
T summand_2,
T & result )

Check the addition of two numbers for overflows for signed integer types larger than int or with the same size as int.

This function performs a check if summand_1 + summand_2 would overflow and returns true in that case. If no overflow occurs, the sum is saved in result and false is returned.

Returns
true on overflow, false on no overflow
Parameters
[in]summand_1,summand_2The summands with are added
[out]resultResult of the addition, only populated when no overflow occurs.

Further information: https://wiki.sei.cmu.edu/confluence/display/c/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow