viewing paste Unknown #6958 | C

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
int32 conv_num(struct script_state* st, struct script_data* data) {
    char* p;
    int32 num;
 
    script->get_val(st, data);
    if( data_isint(data) )
    {// nothing to convert
    }
    else if( data_isstring(data) )
    {// string -> int
        // the result does not overflow or underflow, it is capped instead
        // ex: 999999999999 is capped to INT_MAX (2147483647)
        p = data->u.str;
        errno = 0;
        num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
        if( errno == ERANGE
#if LONG_MAX > INT_MAX
            || num < INT_MIN || num > INT_MAX
#endif
            )
        {
            if( num <= INT_MIN )
            {
                num = INT_MIN;
                ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
            }
            else//if( num >= INT_MAX )
            {
                num = INT_MAX;
                ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
            }
            script_reportdata(data);
            script_reportsrc(st);
        }
        if( data->type == C_STR )
            aFree(p);
        data->type = C_INT;
        data->u.num = num;
    }
#if 0
    // FIXME this function is being used to retrieve the position of labels and
    // probably other stuff [FlavioJS]
    else
    {// unsupported data type
        ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
        script_reportdata(data);
        script_reportsrc(st);
        data->type = C_INT;
        data->u.num = 0;
    }
#endif
    return (int32)data->u.num;
}
 
Viewed 631 times, submitted by Guest.