关于PVS-Studio如何在用于... PVS-Studio的库中发现错误的故事

Picture 1

This is a short story about how PVS-Studio helped us find an error in the source code of the library used in PVS-Studio. And it was not a theoretical error but an actual one — the error appeared in practice when using the library in the analyzer.

这是关于PVS-Studio如何帮助我们在PVS-Studio中使用的库的源代码中发现错误的简短故事。 这不是理论错误,而是实际错误–在分析仪中使用该库时,实际上会出现该错误。

In PVS-Studio_Cmd (as well as some other utilities) we use a special library for parsing command line arguments — CommandLine.

在PVS-Studio_Cmd(以及其他一些实用程序)中,我们使用一个特殊的库来解析命令行参数-CommandLine。

Today I supported the new mode in PVS-Studio_Cmd and it so happened that I had to use this library for parsing command line arguments. While writing the code, I also debug it because I have to work with unfamiliar APIs.

今天,我在PVS-Studio_Cmd中支持新模式,碰巧我不得不使用该库来解析命令行参数。 在编写代码时,我还调试了它,因为我必须使用不熟悉的API。

So, the code is written, compiled, executed and...

因此,代码被编写,编译,执行并...

Picture 3

Code execution goes inside the library where an exception of the NullReferenceException type occurs. It's not so clear from the side — I don't pass any null references into the method.

代码执行在发生NullReferenceException类型的异常的库中进行。 从侧面看还不太清楚-我没有将任何null引用传递给该方法。

To be sure, I look at comments to the callee method. It is hardly probable that they describe the conditions of occurrence of an exception of the NullReferenceException type (as it seems to me usually exceptions of this type are not provided for).

可以肯定的是,我查看对被调用方方法的注释。 它们几乎不可能描述NullReferenceException类型的异常的发生条件(在我看来,通常不提供这种类型的异常)。

Picture 2

There is no information about NullReferenceException in the comments to the method (which, however, is expected).

在方法的注释中没有有关NullReferenceException的信息(但是,这是预期的)。

To see what exactly causes the exception (and where it occurs), I decided to download the project's source code, build it and add a reference to the debug version of the library to the analyzer. The source code of the project is available at GitHub. We need the version 1.9.71 of the library. It is the one used in the analyzer now.

为了查看导致异常的确切原因(及其发生的位置),我决定下载该项目的源代码,进行构建,然后将对库的调试版本的引用添加到分析器。 该项目的源代码可从GitHub获得 。 我们需要该库的1.9.71版本。 现在是分析仪中使用的一种。

I download the corresponding version of the source code, build the library, add a reference to the debug library to the analyzer, execute the code and see:

我下载了相应版本的源代码,构建了库,将对调试库的引用添加到了分析器,执行了代码并看到:

Picture 4

So, the place where the exception occurs is clear — helpInfo has a null value, which causes an exception of the NullReferenceException type when accessing the Left property.

因此,发生异常的地方很清楚— helpInfo具有null值,当访问Left属性时,它将导致NullReferenceException类型的异常。

I started thinking about it. Recently, PVS-Studio for C# has been well improved in various aspects, including the search for dereferencing of potentially null references. In particular, the interprocedural analysis was improved in a number of ways. That's why I was immediately interested in checking the source code to understand if PVS-Studio could find the error under discussion.

我开始考虑它。 最近,用于C#的PVS-Studio在各个方面都得到了很好的改进,包括对可能为空的引用进行解引用的搜索。 特别是,过程间分析在许多方面得到了改进。 这就是为什么我立即对检查源代码感兴趣,以了解PVS-Studio是否可以找到正在讨论的错误。

I checked the source code and among other warnings I saw exactly what I hoped for.

我检查了源代码,并在其他警告中看到了我所希望的。

PVS-Studio警告 (PVS-Studio warning)

:

:

V3080 Possible null dereference inside method at 'helpInfo.Left'. Consider inspecting the 2nd argument: helpInfo. Parser.cs 405 V3080在'helpInfo.Left'的方法内部可能存在空取消引用。 考虑检查第二个参数:helpInfo。 解析器405

Yeah, this is it! That's exactly what we need. Let's take a more detailed look at the source code.

是的,就是这样! 这正是我们所需要的。 让我们更详细地看一下源代码。

private bool DoParseArgumentsVerbs(
  string[] args, object options, ref object verbInstance)
{
  var verbs 
    = ReflectionHelper.RetrievePropertyList<VerbOptionAttribute>(options);
  var helpInfo 
    = ReflectionHelper.RetrieveMethod<HelpVerbOptionAttribute>(options);
  if (args.Length == 0)
  {
    if (helpInfo != null || _settings.HelpWriter != null)
    {
      DisplayHelpVerbText(options, helpInfo, null); // <=
    }

    return false;
  }
  ....
}

The analyzer issues a warning for calling the DisplayHelpVerbText method and warns about the second argument — helpInfo. Pay attention that this method is located in the then-branch of the if statement. The conditional expression is composed in such a way that the then-branch can be executed at the next values of the variables:

分析器会发出警告,提示调用DisplayHelpVerbText方法并警告第二个参数helpInfo 。 请注意,此方法位于if语句的then -branch中。 条件表达式的编写方式使得可以在变量的下一个值处执行then -branch:

  • helpInfo == null;

    helpInfo == null ;

  • _settings.HelpWriter != null;

    _settings.HelpWriter!= null ;

Let's see the body of the DisplayHelpVerbText method:

让我们看一下DisplayHelpVerbText方法的主体:

private void DisplayHelpVerbText(
  object options, Pair<MethodInfo, 
  HelpVerbOptionAttribute> helpInfo, string verb)
{
  string helpText;
  if (verb == null)
  {
    HelpVerbOptionAttribute.InvokeMethod(options, helpInfo, null, out helpText);
  }
  else
  {
    HelpVerbOptionAttribute.InvokeMethod(options, helpInfo, verb, out helpText);
  }

  if (_settings.HelpWriter != null)
  {
    _settings.HelpWriter.Write(helpText);
  }
}

Since verb == null (see method call) we are interested in then-branch of the if statement. Although the situation is similar with the else branch, let's consider then-branch because in our particular case the execution went through it. Remember that helpInfo may be null.

由于动词== null (请参见方法调用),因此我们对if语句的then -branch感兴趣。 尽管形势与else分支类似, 那么,让我们来考虑分枝因为在我们的特定情况下执行通过它去。 请记住, helpInfo可以为null 。

Now let's look at the body of the HelpVerbOptionAttribute.InvokeMethod method. Actually, you have already seen it on the screenshot above:

现在让我们看一下HelpVerbOptionAttribute的主体。 InvokeMethod方法。 实际上,您已经在上面的屏幕截图中看到了它:

internal static void InvokeMethod(
    object target,
    Pair<MethodInfo, HelpVerbOptionAttribute> helpInfo,
    string verb,
    out string text)
{
  text = null;
  var method = helpInfo.Left;
  if (!CheckMethodSignature(method))
  {
    throw new MemberAccessException(
      SR.MemberAccessException_BadSignatureForHelpVerbOptionAttribute
        .FormatInvariant(method.Name));
  }

  text = (string)method.Invoke(target, new object[] { verb });
}
helpInfo.Left is called unconditionally, while helpInfo.Left是无条件调用的,而 helpInfo may be helpInfo可以为 null. The analyzer warned about it, and that's what happened. null 。 分析仪对此进行了警告,事实就是这样。

结论 (Conclusion)

It is nice that we managed to find an error in the source code of the library used in PVS-Studio with the help of PVS-Studio. I think this is a kind of the answer to the question «Does PVS-Studio find errors in PVS-Studio source code?». :) The analyzer can find errors not only in PVS-Studio code but also in the code of the used libraries.

很高兴我们借助PVS-Studio设法在PVS-Studio中使用的库的源代码中发现错误。 我认为这是对“ PVS-Studio是否在PVS-Studio源代码中发现错误?”这个问题的答案。 :)分析仪不仅可以在PVS-Studio代码中发现错误,而且可以在所用库的代码中发现错误。

Finally, I suggest you download the analyzer and try to check your project — what if you can find something interesting there too?

最后,我建议您下载分析器并尝试检查您的项目-如果在那里也可以找到有趣的东西怎么办?

翻译自: https://habr.com/en/company/pvs-studio/blog/462947/

工具:PVS-Studio使用 阅读详情

相关推荐

VideoPose3d:环境搭建+制作自己的视频

论文链接:3D human pose estimation in video with temporal convolutions and semi-supervised training 代码链接:https://github.com/facebookresearch/VideoPose3D 环境搭建:ubuntu20.04+pytorch1.8+cuda10.2+python3.6+anaconda 1.启动anaconda环境 新建环境命令: conda create -n pytorc.

willbetter01的博客 4948

PVS-Studio Crack,重新编译后的自动分析

静态分析的主要价值在于它的定期使用,以便在最早的阶段识别和修复错误。所以,让我们再次指出——静态分析的主要思想不是在发布前一天发现一个隐藏的bug,而是日复一日地修复几十个bug。该工具允许您向开发人员发送关于PVS Studio在夜间运行中发现的错误的电子邮件通知。PVS Studio窗口中分析结果(日志文件)的交互式筛选:通过诊断编号、文件名和诊断文本中的关键字。保存和加载分析结果可以在夜间进行检查-分析仪在夜间进行扫描,并在早上为您提供结果。您可以将分析结果保存为具有完整源代码导航功能的HTML。

q2315702359的博客 516

antv x6 沿边图标循环动画实现

2.使用transition给边设置动画,利用complte方法实现循环动画。实现效果,如下图,边上存在两个图标,要求图标延边一直循环动画。1.注册一个自定义边,边上定义两个图标,并设置其初始位置。

lingliu0824的博客 3370

PVS Studio 7.30.81740.1020

PVS-Studio 执行静态代码分析并生成报告,帮助程序员查找和修复错误。PVS-Studio 执行广泛的代码检查,它还有助于搜索印刷错误和复制粘贴错误。此类错误的示例:V501、V517、V522、V523、V3001。

控件与插件之大全 906

避坑指南:STM32CubeMX配置SDIO4bit模式时为什么卡在HAL_SD_ConfigWideBusOperation?

本文深入分析了使用STM32CubeMX配置SDIO 4位模式时,程序在HAL_SD_ConfigWideBusOperation函数处卡死的常见问题。文章从硬件连接、CubeMX配置、初始化序列及HAL库底层实现等多个维度剖析根源,并提供了从初始化流程优化到增强代码鲁棒性等实战解决方案,帮助开发者彻底解决这一棘手难题。

pca5navigator的博客 829

DVWA靶场实战:从搭建到渗透测试的完整指南

本文详细介绍了DVWA靶场的搭建与渗透测试全流程,涵盖LAMP环境搭建、Docker部署及集成环境安装三种方式。通过SQL注入、文件上传漏洞等实战案例,帮助读者掌握Web安全测试技巧,并提供防御方案与进阶调优建议,是网络安全学习的实用指南。

weixin_28312391的博客 495

代码检测工具——PVS-Studio Analyzer

下载地址:https://www.viva64.com/en/pvs-studio/ 激活码: 用户:Ru.Board 密码:UX9G-38X9-1HNH-0B0F0 执行如下命令可以激活: pvs-studio-analyzer credentials Ru.Board UX9G-38X9-1HNH-OBOF 今天刚用,也只会用一点点。 写了一个脚本进行使用(pvs.sh)...

IT8343的博客 3001

微信PC端Multi文件夹数据库解析:语音消息导出与转换实战指南

本文提供了一份详细的微信PC端语音消息导出与转换实战指南。通过解析Multi文件夹中的MediaMSG数据库,指导用户定位、解密数据库,并利用Python脚本精准提取SILK格式语音,最终转换为通用的WAV或MP3格式,实现个人语音数据的本地备份与处理。

garlic的博客 588

PVS-Studio C/C++/C++11 静态代码分析工具

PVS-Studio C/C++/C++11 静态代码分析工具 静态代码分析仪是一种检测代码缺陷、分析对比 商业代码的工具,它分析源代码和它生成的目标 文件,但并不实际运行源代码。应用于对安全性、 稳定性要求很高的领域,比如航天、国防、工业 控制、金融等就需要严格的代码分析工具。 PVS-Studio 能发现代码中一些潜在问题,或者 针对某些潜在问题给出警告。   PVS-St

痞子龙3D编程 3322

【Davinci开发】:CS接口创建及连接

AUTOSAR SWC之间添加CS-port :详细讲解step

2534

字符串尾部空格的迷思:深入解析 ABAP 隐式转换规则与 `strlen` 意外结果

看似简单的strlen之谜,背后实则是 ABAP 隐式转换的一整套严谨规则。只要开发者在赋值、输出、接口映射等关键节点建立“尾部空格意识”,辅以CONVCONDENSE等显式手段,就能有效杜绝多余空格引发的链式故障。希望本文的拆解与案例,能让你在面对下一个“异常长度”时,从容而笃定地给出正确答案。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 440

pvs-stdio ue4_了解适用于Java的PVS-Studio静态分析器

pvs-stdio ue4 总览 (Overview) The PVS-Studio Java static code analyzer consists of 2 main parts: the kernel, which performs the analysis, and plugins for integration in build systems (Gradle, Maven) a...

cullen2012的博客 475

用 ADBC 把动态查询做得又快又稳:从 CL_SQL_STATEMENT 到安全取数的工程化实践

本文介绍了ABAP中ADBC(ABAP Database Connectivity)技术的核心用法与最佳实践。ADBC作为Open SQL的补充,适用于动态查询、数据库特性调用等场景。文章通过示例代码详细演示了使用ADBC执行查询的完整流程:创建SQL语句、执行查询获取结果集、批量装载数据到内表,以及分包读取处理大数据量。特别强调了动态SQL的安全防护措施,包括输入转义和参数绑定等防注入技术。同时对比了ADBC与Open SQL的适用场景,指出ADBC更适合需要动态访问数据库原生功能的场合。文章还分析了代码

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1782

pvs-stdio ue4_用于Visual Studio的PVS-Studio

pvs-stdio ue4 Many of our articles are focused on anything, but not the PVS-Studio tool itself. Whereas we do a lot to make its usage convenient for developers. Nevertheless, our efforts are often co...

cullen2012的博客 1042

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

pvs linux_Linux下用于C ++开发的PVS-Studio静态分析器入门

pvs linuxPVS-Studio supports analyzing projects developed in C, C++, C#, and Java. You can use the analyzer under Windows, Linux, and macOS. This small article will tell you the basics of analyzing C ...

cullen2012的博客 997

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

pvs-stdio ue4_PVS-Studio:进步的引擎

pvs-stdio ue4 A PVS-Studio team and our product make a great contribution to upgrading of software quality. Moreover, in addition to detecting errors in closed and open source projects, there is an i...

cullen2012的博客 395

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

C++代码静态检测

C++代码静态检测一,CppCheck(下载地址:http://cppcheck.net/)1,使用CppCheck客户端检测2,在vs中使用CppCheck二,TscanCode(下载地址:https://github.com/Tencent/TscanCode)三,PVS-studio(下载地址:https://www.viva64.com/en/pvs-studio/)1,安装2,破解3,使...

bajianxiaofendui的博客 1万+

PVS-Studio 静态代码分析工具推荐

PVS-Studio是一种静态代码分析工具,用于诊断C / C++/ C+11应用程序源代码中的错误。

johnsonlee86的专栏 4216

来自俄罗斯的源代码分析工具 PVS-Studio

PVS - Studio 工具由俄罗斯 OOO "Program Verification Systems" 公司开发。 官方网站:http://www.viva64.com/en/pvs-studio/ PVS-Studio静态代码分析工具作为一种工具来检测代码中的缺陷和商业代码分析对比。越来越多的领域依赖计算机,代码的质量就成了关键。比如航天、国防、工业控制、金融等对安全性,稳定性要求很高的领

软件质量优化 6088

pvs-stdio ue4_云中的PVS-Studio —在Travis CI上运行分析

pvs-stdio ue4 Why do we consider third-party clouds and don't make our own? There is a number of reasons, the main one is that the SaaS implementation is quite an expensive and difficult procedure. I...

cullen2012的博客 747

代码静态分析工具

在大型代码工程中,通常使用 Coverity 和 SonarQube 这样的工具,它们能够集成到持续集成/持续部署(CI/CD)流水线中,自动分析每次提交的代码变化,提供可视化报告,帮助开发团队及时发现和修复问题。PVS-Studio:另一种静态分析工具,支持多种编程语言(如C、C++、C#、Java),能查找编码错误和潜在的安全漏洞。静态分析是指在不运行程序的情况下,通过检查代码文本来发现潜在的问题或优化建议。通过这些工具和方法,可以在代码开发的早期发现潜在的问题,提高代码质量,减少错误发生的可能性。

jjjxxxhhh123的博客 2107

pvs-stdio ue4_PlatformIO中的PVS-Studio集成

pvs-stdio ue4 Recently, the PlatformIO development environment of embedded systems has supported PVS-Studio. In this article, you'll find out how to check your code with the static analyzer on the ex...

cullen2012的博客 769

pvs-stdio ue4_PVS-Studio对牛顿游戏动力学的第二次检查

pvs-stdio ue4 Some time ago, somewhere on the Internet, I stumbled upon a physics engine called Newton Game Dynamics. Knowing that engine projects are usually big and complex, I decided to check its ...

cullen2012的博客 1330

pvs-stdio ue4_PVS-Studio –用于C,C ++,C#和Java的静态代码分析器

pvs-stdio ue4What’s the one most required thing while writing a C or C++ or for that matter any other computer program? 编写C或C ++或其他任何计算机程序时最需要做的一件事是什么? Yes it’s the compiler, how else your machine w...

culing2941的博客 591

C++代码静态分析集成:将PVS-Studio集成到gh_mirrors/st/STL开发流程

你是否还在为C++标准库开发中的隐藏bug和安全漏洞烦恼?本文将详细介绍如何将PVS-Studio静态分析工具无缝集成到gh_mirrors/st/STL项目的开发流程中,帮助开发团队在编译前发现潜在问题,提升代码质量和安全性。读完本文,你将能够:了解PVS-Studio的基本功能,掌握在STL项目中配置和运行PVS-Studio的方法,以及如何将静态分析结果整合到CI/CD流程中。 ## PV...

gitblog_00661的博客 505

提升Stellarium代码质量:PVS-Studio静态分析工具终极指南

Stellarium是一款功能强大的开源天文模拟软件,它能够实时渲染逼真的星空效果。作为一款复杂的科学计算软件,代码质量至关重要。本文将为您详细介绍如何使用PVS-Studio静态分析工具来提升Stellarium的代码质量,帮助开发者构建更加稳定可靠的天文模拟系统。🌟 ## 什么是PVS-Studio? PVS-Studio是一款专业的静态代码分析工具,专门用于检测C、C++、C#和Jav

gitblog_00355的博客 496
上一篇: 检查Telerik UI以使用UWP作为PVS-Studio的入门方法
下一篇: Visual Studio C ++模板IntelliSense基于代码中的实例化进行填充
cullen2012
博客等级 码龄11年 38粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值