单个 g++ 标志是否警告空指针解引用?

huangapple go评论69阅读模式
英文:

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 &lt;iostream&gt;

auto main([[maybe_unused]] int argc, [[maybe_unused]] char ** argv) -&gt; int {

  double *elem = new (std::nothrow) double;
  elem = nullptr;
  std::cout &lt;&lt; *elem &lt;&lt; &#39;\n&#39;;

  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

-Wnull-dereference的文档中:

仅当 -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

Working demo

As you can see using the optimization flag, you get the desired warning.

huangapple
  • 本文由 发表于 2023年5月30日 00:41:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定