英文:
Does even a single g++ flag warn about nullpointer dereferencing?
问题
I wanted to check whether a g++ compiler-flag exists that warns me about nullpointer-dereferencing. When compiling this code with g++ (GCC) 13.1.1
and these compiler flags: -Wnull-dereference, -Wnonnull, still nothing happens. No warning, no error..
我想检查是否存在一个g++编译器标志,用于警告我关于空指针解引用的问题。当使用g++ (GCC) 13.1.1
和这些编译器标志编译此代码时:-Wnull-dereference,-Wnonnull,仍然没有任何警告,没有错误。
#include <iostream>
auto main([[maybe_unused]] int argc, [[maybe_unused]] char ** argv) -> int {
double *elem = new (std::nothrow) double;
elem = nullptr;
std::cout << *elem << '\n';
delete elem;
return 0;
}
So I tried options that control static analysis
: I only came across these two: -Wanalyzer-null-argument, -Wanalyzer-null-dereference (still, no success). Is there really no way of achieving this? Note that I am aware of clang-tidy and other static analysis tools like cpplint, clazy and cppcheck that successfully report these kinds of problems. But I want to rely only on compilation flags.
所以我尝试了控制静态分析
的选项:我只找到了这两个:-Wanalyzer-null-argument,-Wanalyzer-null-dereference(仍然没有成功)。真的没有办法实现这个吗?请注意,我知道clang-tidy和其他静态分析工具如cpplint, clazy和cppcheck可以成功报告这些问题。但我想仅依赖编译标志。
英文:
I wanted to check whether a g++ compiler-flag exists that warns me about nullpointer-dereferencing. When compiling this code with g++ (GCC) 13.1.1
and these compiler flags: -Wnull-dereference, -Wnonnull, still nothing happens. No warning, no error..
#include <iostream>
auto main([[maybe_unused]] int argc, [[maybe_unused]] char ** argv) -> int {
double *elem = new (std::nothrow) double;
elem = nullptr;
std::cout << *elem << '\n';
delete elem;
return 0;
}
So I tried options that control static analysis
: I only came across these two: -Wanalyzer-null-argument, -Wanalyzer-null-dereference (still, no success). Is there really no way of achieving this? Note that I am aware of clang-tidy and other static analysis tools like cpplint, clazy and cppcheck that successfully report these kinds of problems. But I want to rely only on compilation flags.
答案1
得分: 2
仅当
-fdelete-null-pointer-checks
激活时,此选项才有效,大多数目标中的优化会启用它。警告的精确性取决于所使用的优化选项。
启用优化(-O1
足够),然后您将收到警告。
-Wnonnull
执行完全不同的操作。(还请参阅上面的文档链接。)
-Wanalyzer-*
选项仅在使用 -fanalyzer
启用静态分析器时生效,然后它们是默认的。这些选项旨在以 -Wno-*
形式使用,以禁用特定的静态分析器检查。还请参阅文档。
英文:
From the documentation of -Wnull-dereference
:
> This option is only active when -fdelete-null-pointer-checks is active, which is enabled by optimizations in most targets. The precision of the warnings depends on the optimization options used.
Enable optimizations (-O1
is enough) and you will get the warning.
-Wnonnull
does something completely different. (Also see documentation link above.)
The -Wanalyzer-*
options only take effect if you enable the static anaylzer with -fanalyzer
and then they are the default. These options are meant to be used in the -Wno-*
form to disable specific static analyzer checks. See documentation as well.
答案2
得分: 1
有办法实现这个吗?
有的,通过启用如下所示的优化来实现:
-O3 -Wnull-dereference
如您所见,使用优化标志,您会得到所需的警告。
英文:
> Is there really no way of achieving this?
There is, by enabling optimizations as shown below:
-O3 -Wnull-dereference
As you can see using the optimization flag, you get the desired warning.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论