Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No integer overflow warning reported in the call to constructor

Tags:

c++

clang

I have the following snippet:

#include <iostream>

void func(uint64_t i) { std::cout<<i<<std::endl; }

class A{ 
public:
    A(uint64_t i){ std::cout<<i<<std::endl; } 
};

int main(int /*argc*/, char* /*argv*/[]) {
    func(1024 * 1024 * 1024 * 1024 * 1024ull); // clang [-Winteger-overflow]: Overflow in expression; result is 0 with type 'int'   
    A a(1024 * 1024 * 1024 * 1024 * 1024ull); // no error
    return 0; 
}

I know I should have written 1024ull * 1024 * ..., but I am wondering why my compiler (clang) only throws the overflow error at the line with func, and is not aware of the problem in the constructor

like image 331
royshoo Avatar asked Feb 14 '26 05:02

royshoo


1 Answers

This is slightly simplified your code, where there are two similar signed integer overflows (already in 1024 * 1024 * 1024 * 1024):

#include <cstdint>

void func(uint64_t) {}

struct A { 
    A(uint64_t){} 
};

int main() {
    func(1024 * 1024 * 1024 * 1024 * 1024ull); // warning, ok
    A a(1024 * 1024 * 1024 * 1024 * 1024ull); // no warning in Clang!
}

The one in calling func is detected by Clang, GCC and MSVC with appropriate command line options -Wall -Wextra for Clang/GCC and /Wall for MSVC.

But the other one in calling A::A is not detected by Clang. Online demo: https://gcc.godbolt.org/z/6vGT33q1W

This is just a Clang bug, which is being fixed right now: https://github.com/llvm/llvm-project/issues/58944

like image 77
Fedor Avatar answered Feb 16 '26 18:02

Fedor



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!