Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rlimit64 integer overflow

I'm currently trying to limit the memory resources of the process. Before to do so, I prepare the rlimit64 structure calling getrlimit64. When compiling the following code

    rlimit64 as_limit;
    if (getrlimit64(RLIMIT_AS, &as_limit) == 0)
    {
        std::cerr << as_limit.rlim_cur << std::endl;
        std::cerr << as_limit.rlim_max << std::endl;

        as_limit.rlim_cur = 0x040000000 * 16;

        std::cerr << as_limit.rlim_cur << std::endl;
        std::cerr << as_limit.rlim_max << std::endl;
    }

I'm getting a warning about integer overflow for the line with an assigment of as_limit.rlim_cur. Hovever, as an output I get:

    18446744073709551615
    18446744073709551615
    0
    18446744073709551615 

How is this possible?

like image 960
nil Avatar asked Jan 24 '26 19:01

nil


1 Answers

The hex literal is being interpretted as an int.

Try using:

0x040000000LL * 16;

So that the compiler treats the literal as a long long.

Since a long long literal (the LL suffix) is C99, you're likely better off casting to whatever the type of the struct member is (though I would imagine any recent compiler would have support for LL -- gcc definitely does). For example, you could cast to long long.

like image 182
Corbin Avatar answered Jan 27 '26 09:01

Corbin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!