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

fixed some -Wsuggest-attribute=returns_nonnull GCC warnings #6950

Merged
merged 3 commits into from
Nov 29, 2024

Conversation

firewave
Copy link
Collaborator

No description provided.

@firewave
Copy link
Collaborator Author

I manually checked all the function usages and in almost all cases we already used them like they were returning a non-null result.

@firewave
Copy link
Collaborator Author

I also filed llvm/llvm-project#106392 upstream about detecting the unnecessary checks.

Copy link
Owner

@danmar danmar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that clang/gcc will ensure that the functions do NOT return null if it's marked with that attribute? Otherwise removing the safety checks would look dangerous.

Copy link
Owner

@danmar danmar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm it does not seem that clang/gcc warns properly when pointer could be NULL:

daniel@laptop:~/cppchecksolutions/cppcheck$ g++ -fsyntax-only -Wall -Wextra -pedantic 1.cpp
daniel@laptop:~/cppchecksolutions/cppcheck$ clang++ -fsyntax-only -Weverything 1.cpp
1.cpp:2:39: warning: no previous prototype for function 'p' [-Wmissing-prototypes]
__attribute__((returns_nonnull)) int* p(int*p) {
                                      ^
1.cpp:2:34: note: declare 'static' if the function is not intended to be used outside of this translation unit
__attribute__((returns_nonnull)) int* p(int*p) {
                                 ^
                                 static 
1 warning generated.
daniel@laptop:~/cppchecksolutions/cppcheck$ cat 1.cpp

__attribute__((returns_nonnull)) int* p(int*p) {
    if (!p) {}
    return p;
}

So it would be a bad idea to remove the safety checks that checks the return value?

@danmar danmar self-requested a review October 26, 2024 19:21
@firewave
Copy link
Collaborator Author

hmm it does not seem that clang/gcc warns properly when pointer could be NULL

There is an upcoming static analyzer check: llvm/llvm-project@4f33e7c.

So it would be a bad idea to remove the safety checks that checks the return value?

The checks we remove should not change much - if we do not want that then we need to add 100+ checks to the code.

@firewave
Copy link
Collaborator Author

We could also detect it ourselves: https://trac.cppcheck.net/ticket/13048.

Enabling the Clang Static Analyzer is looked at in #6835.

@firewave
Copy link
Collaborator Author

No further comments in over a month - merging.

@firewave firewave merged commit 164e064 into danmar:main Nov 29, 2024
60 checks passed
@firewave firewave deleted the warn-nonnull branch November 29, 2024 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants