当前位置: 代码迷 >> 综合 >> dotnet 使用 Infer# 自动分析代码缺陷
  详细解决方案

dotnet 使用 Infer# 自动分析代码缺陷

热度:34   发布时间:2024-01-12 08:03:00.0

本文告诉大家如何使用 Infer# 开源库配合 GitHub 的 Action 实现自动分析代码缺陷,如找到可空引用或线程安全等问题

这是一个在 GitHub 上完全开源的仓库,请看 https://github.com/microsoft/infersharp

刚好今天收到了 Infer# 发布 1.2 版本博客,请看 Infer# v1.2: Interprocedural Memory Safety Analysis For C# - .NET Blog

关于 GitHub 的 Action 的基础入门请看 dotnet 部署 github 的 Action 进行持续集成

使用的方法非常简单,只需要在 GitHub 的 Action 的配置文件里面添加如下代码

- name: Run Infer#      uses: microsoft/infersharpaction@v1.2with:binary-path: 输出二进制文件夹路径

如我在 https://github.com/dotnet-campus/AsyncWorkerCollection 开源仓库上的配置代码如下

- name: Run Infer#      uses: microsoft/infersharpaction@v1.2with:binary-path: AsyncWorkerCollection/bin/Release/netcoreapp3.1

此输出的二进制文件夹路径里面要求是包含 dll 和 pdb 文件,通过 dll 进行分析,通过 pdb 从而告诉你是哪个文件

效果如下

c8d29af2d0738159e93c05fa15940b7a.png

可以看到输出了资源没有释放和线程安全问题

Found 3 issuesIssue Type(ISSUED_TYPE_ID): #Thread Safety Violation(THREAD_SAFETY_VIOLATION): 2Dotnet Resource Leak(DOTNET_RESOURCE_LEAK): 1Analysis Result
==================================================
#0
/home/runner/work/AsyncWorkerCollection/AsyncWorkerCollection/AsyncWorkerCollection/AsyncTaskQueue_/AsyncTaskQueue.cs:72: error: Dotnet Resource LeakLeaked { n$1 -> 1 } resource(s) in method "AwaitableTask AsyncTaskQueue.GetExecutableTask(Action)" at type(s) System.Threading.Tasks.Task.#1
/home/runner/work/AsyncWorkerCollection/AsyncWorkerCollection/AsyncWorkerCollection/DoubleBuffer_/DoubleBufferLazyInitializeTask.cs:47: warning: Thread Safety ViolationUnprotected write. Non-private method `DoubleBufferLazyInitializeTask`1<T>.OnInitialized()` writes to field `this.dotnetCampus.Threading.DoubleBufferLazyInitializeTask`1<T>._isInitialized` outside of synchronization.Reporting because this access may occur on a background thread.#2
/home/runner/work/AsyncWorkerCollection/AsyncWorkerCollection/AsyncWorkerCollection/DoubleBuffer_/DoubleBufferLazyInitializeTask.cs:41: warning: Thread Safety ViolationRead/Write race. Non-private method `DoubleBufferLazyInitializeTask`1<T>.OnInitialized()` reads without synchronization from `this.dotnetCampus.Threading.DoubleBufferLazyInitializeTask`1<T>._isInitialized`. Potentially races with write in method `DoubleBufferLazyInitializeTask`1<T>.OnInitialized()`.Reporting because this access may occur on a background thread.Found 3 issuesIssue Type(ISSUED_TYPE_ID): #Thread Safety Violation(THREAD_SAFETY_VIOLATION): 2Dotnet Resource Leak(DOTNET_RESOURCE_LEAK): 1

此工具只能在 Linux 下运行,官方有制作好一个 docker 文件,可以从 https://github.com/microsoft/infersharpaction 拉到。但是问题不大,因为此工具是对输出文件进行分析的,所以可以在 Windows 平台上进行构建,只是将输出的二进制文件使用此工具

在现有的仓库加添加此工具的例子请看 https://github.com/dotnet-campus/AsyncWorkerCollection/pull/66

  相关解决方案