计算机科学 ›› 2025, Vol. 52 ›› Issue (6): 35-43.doi: 10.11896/jsjkx.240300195

• 计算机软件 • 上一篇    下一篇

流敏感的C/C++程序编程风格检查方法

胡梦泽1,2,3, 马旭桐1,3, 张豪1,3, 张健1,3   

  1. 1 中国科学院软件研究所计算机科学国家重点实验室 北京 100190
    2 国科大杭州高等研究院 杭州 310012
    3 中国科学院大学 北京 100049
  • 收稿日期:2024-03-31 修回日期:2024-08-17 出版日期:2025-06-15 发布日期:2025-06-11
  • 通讯作者: 张健(zj@ios.ac.cn)
  • 作者简介:(humz@ios.ac.cn)
  • 基金资助:
    国家自然科学基金(62132020)

Flow-sensitive Coding Style Checking for C/C++ Programs

HU Mengze1,2,3, MA Xutong1,3, ZHANG Hao1,3, ZHANG Jian1,3   

  1. 1 State Key Laboratory of Computer Science,Institute of Software,Chinese Academy of Science,Beijing 100190,China
    2 Hangzhou Institute for Advanced Study,UCAS,Hangzhou 310012,China
    3 University of Chinese Academy of Sciences,Beijing 100049,China
  • Received:2024-03-31 Revised:2024-08-17 Online:2025-06-15 Published:2025-06-11
  • About author:HU Mengze,born in 1999,postgradua-te,is a student member of CCF(No.Q7307G).His main research interests include static analysis of C/C++ programs and so on.
    ZHANG Jian,born in 1969,Ph.D,professor,Ph.D supervisor.His main research interests include software engineering and automated reasoning.
  • Supported by:
    National Natural Science Foundation of China(62132020).

摘要: C/C++编程语言应用于众多关键领域的软件系统中,在开发时对编程的规范性和语义的明确性有着极高的要求。为了避免不当使用C/C++语言带来潜在的安全问题,国内提出了面向C/C++语言的《C/C++语言编程安全子集》(后简称为GJB8114)。由于标准中规则较多,程序员在开发过程中难免存在不规范的写法,因此实现用自动化的规则检测工具检测相应的编码规则是必要的。而现有的编程规范检查工具对标准的检查并不全面,在针对需要理解程序上下文的规则的检查时,误报率较高甚至不支持检查。对此,将GJB8114中的规则分类并对复杂规则进行定义。通过调研Testbed工具检查GJB8114中的复杂规则的支持程度,总结得到现有工具存在流敏感分析不完善和无法进行跨文件的全局分析这两个问题。采取了结合语法树匹配的流敏感分析方法和跨文件的全局分析方法来解决这些问题。在此基础上,开发得到了CruletFS工具。实验结果表明,与常见的编程规范检查工具Cppcheck,Testbed等相比,CruletFS在检查复杂规则时有更好的效果。在针对规模较大的项目分析时,CruletFS在开销上也优于Cppcheck。这说明相较于已有的方法和策略,所提方法可以在占用开销较低的基础上有效提高复杂规则检查的准确率。

关键词: C/C++编码风格检查, 流敏感静态分析, 跨文件代码检查

Abstract: C/C++ programming languages are applied in numerous critical software systems,and there is an extremely high demand for standardization and clarity of semantics during development.To prevent potential security issues arising from improper use of C/C++ languages,aC/C++ Language Programming Security Subset(referred to as GJB8114) was proposed domestically.Given the abundance of rules within the standard,it's inevitable that programmers may deviate from these norms,thereby necessitating automated rule detection tools to identify such non-compliant coding practices.However,existing rule checking tools do not provide comprehensive checks against the standards,especially for rules that require understanding of the program's context,leading to high false positive rates or even a lack of support for certain checks.This paper categorizes the rules in GJB8114 and defines what constitutes a complex rule.Through evaluating the Testbed tool's capability to inspect complex rules within GJB8114,it identifies that current tools lack thorough flow-sensitive analysis and are unable to perform cross-file global analysis.To address these issues,this study adops a flow-sensitive analysis method combined with syntax tree matching and a cross-file global analysis approach.Based on this,the CruletFS tool is developed.Experimental results demonstrate that CruletFS performs better in checking complex rules compared to common rule checking tools,such as Cppcheck and Testbed.In analyzing large-scale projects,CruletFS also outperforms Cppcheck in terms of time and memory overhead.

Key words: C/C++ coding style check, Flow-sensitive static analysis, Cross-file code check

中图分类号: 

  • TP311
[1]TIOBE Index [EB/OL].https://www.tiobe.com/tiobe-in-dex/.
[2]Top Programming Language 2024 [EB/OL].https://elevatex.de/blog/it-insights/programming-languages-ranking-2024.
[3]safe subset of C language for space armament software [S].Commission of Science,Technology and Industry for National Defense,2005.
[4]Safe subset of C/C++language programming:GJB8114-2013 [S].General Armaments Department of the People's Liberation Army,2013.
[5]Liverpool Data Research Associates.LDRA Testbed-static and dynamic code analysis[EB/OL].http://ldra.com/aerospace-defence/products/ldra-testbed-tbvision/.
[6]GAO Q,MA S,SHAO S,et al.CoBOT:static C/C++ bug detection in the presence of incomplete code [C]//Proceedings of the 26th Conference on Program Comprehension.2018:385-388.
[7]Shanghai Nayi Technology Co.,Ltd.[EB/OL].https://naive-systems.com/
[8]MA X,YAN J,LI Y,et al.SPrinter:a static checker for finding smart pointer errors in C++ programs[C]//2019 34th IEEE/ACM International Conference on Automated Software Engineering(ASE).IEEE,2019:1122-1125.
[9]MA X,YAN J,ZHANG H,et al.Detecting Memory Errors in Python Native Code by Tracking Object Lifecycle with Reference Count[C]//2023 38th IEEE/ACM International Conference on Automated Software Engineering(ASE).IEEE,2023:1429-1440.
[10]WANG X F,ZHAO K J,TIAN Z W.Research on Key Technologies of Data Flow Analysis [J].Computer Science,2005,32(12):91-93.
[11]SCHUBERT P D,LEER R,HERMANN B,et al.Into theWoods:Experiences from Building a Dataflow Analysis Framework for C/C++ [C]//2021 IEEE 21st International Working Conference on Source Code Analysis and Manipulation(SCAM).IEEE,2021:18-23.
[12]YE S,SUI Y,XUE J.Region-based selective flow-sensitivepointer analysis[C]//International Static Analysis Symposium.Cham:Springer,2014:319-336.
[13]SUI Y,YE D,XUE J.Detecting memory leaks statically with full-sparse value-flow analysis[J].IEEE Transactions on Software Engineering,2014,40(2):107-122.
[14]BALDONI R,COPPA E,D'ELIA D C,et al.A survey of symbolic execution techniques [J].ACM Computing Surveys,2018,51(3):1-39.
[15]ZHANG J,ZHANG C,XUAN J F,et al.Recent Progress in Program Analysis[J].Journal of Software,2019,30(1):80-109.
[16]FATIMA A,BIBI S,HANIF R.Comparative study on staticcode analysis tools for C/C++[C]//2018 15th International Bhurban Conference on Applied Sciences and Technology(IBCAST).IEEE,2018:465-469.
[17]Cppcheck-a tool for static C/C++ code analysis [EB/OL].(2023).https://cppcheck.net.
[18]PEREIRA J D,VIEIRA M.On the use of open-source C/C++ static analysis tools in large projects[C]//2020 16th European Dependable Computing Conference(EDCC).IEEE,2020:97-102.
[19]KAUR A,NAYYAR R.A comparative study of static codeanalysis tools for vulnerability detectionin C/C++ and Java source code [J].Procedia Computer Science,2020,171:2023-2029.
[20]LLVM Team.Clang static analyzer[EB/OL].https://clang.llvm.org/docs/ClangStaticAnalyzer.html.
[21]Astrée runtime error analyzer [EB/OL].https://www.absint.com/astree/index.htm.
[22]BLANCHET B,COUSOT P,COUSOT R,et al.A static analyzer for large safety-critical software [C]//Proceedings of the ACM SIGPLAN 2003 Conference on Programming Language Design and Implementation.2003:196-207.
[23]MINÉ A,DELMAS D.Towards an industrial use of sound static analysis for the verification of concurrent embedded avionics software [C]//2015 International Conference on Embedded Software(EMSOFT).IEEE,2015:65-74.
[24]YANG L.The Optimization and Improvement of Software Testing for C Programs [D].Beijing:University of Chinese Academy of Sciences,2018
[25]WANG W.C/C++ Language Programming Safety StandardCompliance Check [D].Beijing:University of Chinese Academy of Sciences,2022
[26]LLVM Team.Libtooling [EB/OL].https://clang.llvm.org/docs/LibTooling.html.
[27]LLVM Team.Introduction to the clang ast [EB/OL].https://clang.llvm.org/docs/IntroductionToTheClangAST.html.
[28]Fallahi.awesome-cpp[EB/OL].https://github.com/fffaraz/awesome-cpp.
[29]LLVM Team.ASTMatch [EB/OL].https://clang.llvm.org/docs/LibASTMatchersReference.html
[30]BLACK P E.Juliet 1.3 test suite:Changes from 1.2 [M].US Department of Commerce,National Institute of Standards and Technology, 2018.
[31]ZHANG H,LUO J,HU M,et al.Detecting Exception Handling Bugs in C++ Programs[C]//2023 IEEE/ACM 45th International Conference on Software Engineering(ICSE).IEEE,2023:1084-1095.
[32]MA X,YAN J,WANG W,et al.Detecting memory-related bugs by tracking heap memory management of C++ smart pointers[C]//2021 36th IEEE/ACM International Conference on Automated Software Engineering(ASE).IEEE,2021:880-891.
Viewed
Full text


Abstract

Cited

  Shared   
  Discussed   
No Suggested Reading articles found!