14

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

11
  • 5
    g++ 12.1 reports both as warnings; MSVC reports both as well. godbolt.org/z/465q6csvd Commented Oct 5, 2022 at 15:20
  • This looks like as a godbolt investigation is in good order. As does g++ 12.2. godbolt.org/z/zq9dhvWcr MSVC gives no warning(but then again it doesn't have a integer overflow check, so that is probably expected. Commented Oct 5, 2022 at 15:20
  • 1
    Poking around with godbolt.org, it appears that every version of g++ reports both overflows. This issue/question appears to be specific to clang. Commented Oct 5, 2022 at 15:24
  • 5
    @royshoo Do note that apple clang, and clang are two different compilers. Apple is pretty tight lipped about their version. Commented Oct 5, 2022 at 16:03
  • 1
    Hi @TeaAgeSolutions, don't we have a signed overflow here? Commented Oct 6, 2022 at 7:55

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.