如果函数中使用了 _Nullable 参数,如何使用 clang 分析器获取有关 NullDereference 的警告

问题描述 投票:0回答:0

我想通过向我的函数添加 _Nullable 来检测 NullDereference 错误。 我写了 my_function();它是一个函数,如果 ptr 为 NULL,将导致 NullDerenference 错误。为了给 clang 分析器一个提示,我使用 _Nullable ptr 作为我的函数参数。

int my_function(int * _Nullable ptr)
{
   printf("read: %d\n", ptr[1]);
   return 1;
}

我尝试在启用所有可空性检查器的情况下使用扫描构建

scan-build-17 --use-cc=clang -enable-checker nullability.NullableDereferenced -enable-checker nullability.NullablePassedToNonnull  -enable-checker nullability.NullablePassedToNonnull -enable-checker nullability.NullableReturnedFromNonnull -o ./report -v make

但是,它没有捕捉到错误。

然后我尝试了 facebook infer

infer -- make

它给了我一个很好的检测

func.c:10: error: Null Dereference
  pointer `ptr` could be null and is dereferenced at line 10, column 23.
   8.   //      return 0;
   9.
  10.   printf("read: %d\n", ptr[1]);
                            ^
  11.
  12.

我是否错过了 clang 分析器捕获此 NullDerefence 错误的内容?

objective-c clang static-analysis clang-tidy scan-build
© www.soinside.com 2019 - 2024. All rights reserved.