Overflowcheck

mail@pastecode.io avatar
unknown
c_cpp
a month ago
6.2 kB
3
Indexable
Never
#include <iostream>
#include <typeinfo>
#include <stdio.h>
#include <limits>

/*
    TYPE_VAR    - Variable to determine returned type value (It can be the same as var1)
    var1        - First variable
    var2        - Second variable
    x           - Operation
*/

template <typename T, typename U, typename O>
void check_overflow(const T TYPE_VAR, const U var1 = 0, char x = '_', const O var2 = 0)
{
    
    if (typeid(TYPE_VAR) == typeid(float) || typeid(var1) == typeid(float) || typeid(var2) == typeid(float)) // -3.4e+30f - 3.4e+30f
    {
        //printf("\n\n\n<<< NOT HANDLING THIS TYPE YET !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n\n");

        printf("\n\nerror !!! FLOAT DETECTED -> check for yourself!!! Type min -> max (-3.4e+30f -> 3.4e+30f)");
        printf("\nerror TYPE_VALUE has type: %s",   typeid(T).name());
        printf("\nerror FIRST VALUE has type: %s",  typeid(U).name());
        printf("\nerror SECOND VALUE has type: %s", typeid(O).name());
        printf("\nerror (%%f) TYPE_VALUE: %f \nerror (%%f) FIRST VALUE: %f \nerror (%%f) SECOND VALUE: %f", TYPE_VAR,var1,var2);
        printf("\nerror (%%llu) TYPE_VALUE: %llu \nerror (%%llu) FIRST VALUE: %llu \nerror (%%llu) SECOND VALUE: %llu", TYPE_VAR,var1,var2);
        return;
    }
    else if (typeid(TYPE_VAR) == typeid(signed long long) || typeid(var1) == typeid(signed long long) || typeid(var2) == typeid(signed long long)) // -9223372036854775809 - 9223372036854775807
    {
        printf("\n\nerror (SIGNED LONG LONG) CHECK FOR OVERFLOW YOURSELF!!! Type min -> max (-9223372036854775809 -> 9223372036854775807) \n");
        printf("\nerror TYPE_VALUE has type: %s", typeid(T).name());
        printf("\nerror FIRST VALUE has type: %s", typeid(U).name());
        printf("\nerror SECOND VALUE has type: %s", typeid(O).name());
        printf("\nerror (%%lld) TYPE_VALUE: %lld \nerror (%%lld) FIRST VALUE: %lld \nerror (%%lld) SECOND VALUE: %lld", TYPE_VAR, var1, var2);
        printf("\nerror (%%llu) TYPE_VALUE: %llu \nerror (%%llu) FIRST VALUE: %llu \nerror (%%llu) SECOND VALUE: %llu", TYPE_VAR, var1, var2);
        return;

    }
    else if (typeid(TYPE_VAR) == typeid(unsigned long long) || typeid(var1) == typeid(unsigned long long) || typeid(var2) == typeid(unsigned long long)) // 0 - 18446744073709551615
    {   
        printf("\n\nerror (UNSIGNED LONG LONG) CHECK FOR OVERFLOW YOURSELF!!! (0 -> 18446744073709551615)");
        printf("\nerror TYPE_VALUE has type: %s", typeid(T).name());
        printf("\nerror FIRST VALUE has type: %s", typeid(U).name());
        printf("\nerror SECOND VALUE has type: %s", typeid(O).name());
        printf("\nerror (%%lld) TYPE_VALUE: %lld \nerror (%%lld) FIRST VALUE: %lld \nerror (%%lld) SECOND VALUE: %lld", TYPE_VAR, var1, var2);
        printf("\nerror (%%llu) TYPE_VALUE: %llu \nerror (%%llu) FIRST VALUE: %llu \nerror (%%llu) SECOND VALUE: %llu", TYPE_VAR, var1, var2);
        return;

    }

    long long TYPE_MINIMUM = std::numeric_limits<T>::min();
    long long TYPE_MAXIMUM = std::numeric_limits<T>::max();

    long long temp_var1 = var1;
    long long temp_var2 = var2;
    switch (x)
    {
        case '+':
            if ((temp_var1 + temp_var2) > TYPE_MAXIMUM)
            {
                printf("\nerror OVERFLOW !!! ");
                printf("\nerror Sum result: %lld is higher than TYPE MAXIMUM: %lld\n\n", temp_var1 + temp_var2, TYPE_MAXIMUM);
            }
            break;
        case '-':
            if ((temp_var1 - temp_var2) < TYPE_MINIMUM)
            {
                printf("\nerror UNDERFLOW !!! ");
                printf("\nerror Subtraction result: %lld is lower than TYPE MINIMUM: %lld\n\n", temp_var1 - temp_var2, TYPE_MINIMUM);
            }
            break;
        case '*':
            if ((temp_var1 * temp_var2) > TYPE_MAXIMUM)
            {
                printf("\nerror OVERFLOW !!! ");
                printf("\nerror Multiplication result: %lld is higher than TYPE MAXIMUM: %lld\n\n", temp_var1 * temp_var2, TYPE_MAXIMUM);
            }
            else if ((temp_var1 * temp_var2) < TYPE_MINIMUM)
            {
                printf("\nerror UNDERFLOW !!! ");
                printf("\nerror Multiplication result: %lld is lower than TYPE MINIMUM: %lld\n\n", temp_var1 * temp_var2, TYPE_MINIMUM);
            }
            break;
        case '/':
            if (temp_var2 == 0)
            {
                printf("\nerror DIVIDING BY 0 !!! \n\n");
            }
            else if ((temp_var1 / temp_var2) > TYPE_MAXIMUM)
            {
                printf("\nerror OVERFLOW !!! ");
                printf("\nerror Dividing result: %lld is higher than TYPE MAXIMUM: %lld\n\n", temp_var1 / temp_var2, TYPE_MAXIMUM);
            }
            else if ((temp_var1 / temp_var2) < TYPE_MINIMUM)
            {
                printf("\nerror UNDERFLOW !!! ");
                printf("\nerror Dividing result: %lld is lower than TYPE MINIMUM: %lld\n\n", temp_var1 / temp_var2, TYPE_MINIMUM);
            }
            break;
        default:
            printf("\n\n\n<<< NOT HANDLING THIS OPERATION YET !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n\n");
            break;
    }
}


int main()
{
      signed char                   char_x = 0;
    unsigned char                  uchar_x = 0;
      signed short                 short_x = 0;
    unsigned short                ushort_x = 0;
      signed int                     int_x = 0;
    unsigned int                    uint_x = 0;
      signed long                   long_x = 0;
    unsigned long                  ulong_x = 0;
      signed long long          longlong_x = 0;
    unsigned long long         ulonglong_x = 0;
      float                        float_x = 0;

    check_overflow(char_x,      0,'+',0);
    check_overflow(uchar_x,     0,'+',0);
    check_overflow(short_x,     0,'+',0);
    check_overflow(ushort_x,    0,'+',0);
    check_overflow(int_x,       0,'+',0);
    check_overflow(uint_x,      0,'+',0);
    check_overflow(long_x,      0,'+',0);
    check_overflow(ulong_x,     0,'+',0);
    check_overflow(ulong_x,     0,'+',0);
    
    check_overflow(longlong_x,  0,'+',0);
    check_overflow(ulonglong_x, 0,'+',0);
    check_overflow(float_x,     0,'+',0);

}



Leave a Comment