Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No uninitialized warning for member variable and getter #78129

Open
fekir opened this issue Jan 15, 2024 · 2 comments
Open

No uninitialized warning for member variable and getter #78129

fekir opened this issue Jan 15, 2024 · 2 comments
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer compiler-rt:ubsan Undefined behavior sanitizer

Comments

@fekir
Copy link

fekir commented Jan 15, 2024

#include <cstdio>

struct a{
    int i;
    int get_i() const {
        return i;
    }
    a() : i(get_i()){}
};


int main(){
    a instance = a();
    std::printf("%d", instance.i);
}

clang does not generate any warning, even with optimizations enabled.

It generates a warning if a() : i(get_i()){} is changed to a() : i(i){}.

Example on godbolt, with comparison with GCC, which does generate a warning when optimizations are enabled

https://godbolt.org/z/6E43zroWj

@DimitryAndric DimitryAndric added clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer compiler-rt:ubsan Undefined behavior sanitizer and removed new issue labels Jan 15, 2024
@DimitryAndric
Copy link
Collaborator

I can at least confirm that clang trunk doesn't warn at all. So it created a nice "random" number generator:

$ ./pr78129
548438528
$ ./pr78129
554314800
$ ./pr78129
543163808
$ ./pr78129
547619032
$ ./pr78129
539220120
$ ./pr78129
544666152

What is also interesting is that UBSan does not warn either:

$ clang++ -O0 -g -fsanitize=undefined pr78129.cpp -o pr78129
$ ./pr78129
2354616

In any case I definitely agree that it should warn. And UBSan should catch this somehow.

@dwblaikie
Copy link
Collaborator

Generally clang's warnings are not interprocedural - it's too expensive to do at compile time. So analyzing get_i to warn in the ctor is probably out of scope/infeasible.

UBSan catching this seems like something to look into, at least.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer compiler-rt:ubsan Undefined behavior sanitizer
Projects
None yet
Development

No branches or pull requests

3 participants