Code Analysis
for C/C++
Overview
 The C/C++ Code Analysis tool provides information
to developers about possible defects in their C/C++
source code. Common coding errors reported by the
tool include buffer overruns, un-initialized memory,
null pointer dereferences, and memory and resource
leaks.
5.1.2016Roman Okolovich2
Source-code Annotation Language (SAL)
 The Microsoft source-code annotation language (SAL)
provides a set of annotations that can be used to
describe how a function uses its parameters, the
assumptions that it makes about them, and the
guarantees that it makes when it finishes. The
annotations are defined in the header file <sal.h>.
Visual Studio code analysis for C++ uses SAL
annotations to modify its analysis of functions.
 Natively, C and C++ provide only limited ways for
developers to consistently express intent and
invariance.
 By using SAL annotations, you can describe your
functions in greater detail so that developers who are
consuming them can better understand how to use
them.
5.1.2016Roman Okolovich3
SAL makes code more valuable
void* memcpy(
void* dest,
const void* src,
size_t count
);
 Without SAL annotations, you'd have to rely on
documentation or code comments.
void * memcpy(
_Out_writes_bytes_all_(count) void *dest,
_In_reads_bytes_(count) const void *src,
size_t count
);
 Notice that these annotations resemble the
information in the MSDN documentation, but
they are more concise and they follow a
semantic pattern. When you read this code,
you can quickly understand the properties of
this function and how to avoid buffer overrun
security issues.
5.1.2016Roman Okolovich4
Find potential bugs
wchar_t * wmemcpy(
_Out_writes_all_(count) wchar_t *dest,
_In_reads_(count) const wchar_t *src,
size_t count)
{
size_t i;
for (i = 0; i <= count; i++) { // BUG: off-by-one error
dest[i] = src[i];
}
return dest;
}
 This implementation contains a common off-by-one error.
Fortunately, the code author included the SAL buffer size
annotation—a code analysis tool could catch the bug by
analyzing this function alone.
5.1.2016Roman Okolovich5
 Annotating Function Parameters and Return Values
 _In_, _Out_, _Inout_, _In_z_, etc
 When a pointer parameter annotation includes _opt_, it indicates that
the parameter may be null
 _In_opt_, _Out_opt_, _Inout_opt_, _In_opt_z_, etc
 Return values
 _Ret_z_, _Ret_maybenull_, _Ret_writes_to_(s,c), _Ret_notnull_, etc
 Annotating Function Behavior
 A function can fail, and when it does, its results may be incomplete or
differ from the results when the function succeeds.
 _Check_return_ - annotates a return value and states that the caller should
inspect it.
 _Always_(anno_list), _Success_(expr), etc
 Example: annotate formal parameters and return value of the
function by using the Pre and Post conditions:
[returnvalue:SA_Post(Null=SA_Maybe)]
LinkedList* AddTail([SA_Pre(Null=SA_Maybe)] LinkedList* node, int value)
5.1.2016Roman Okolovich6
Specify Additional Code Information
It’s possible to provide hints to
the code analysis tool for
C/C++ code that will help the
analysis process and reduce
warnings.
__analysis_assume( expr )
expr - any expression that is
assumed to evaluate to true.
#include <windows.h>
#include <codeanalysissourceannotations.h>
using namespace vc_attributes;
// calls free and sets ch to null
void FreeAndNull(char* ch);
//requires pc to be null
void f([Pre(Null=Yes)] char* pc);
void test( )
{
char *pc = (char*)malloc(5);
FreeAndNull(pc);
__analysis_assume(pc == NULL);
f(pc);
}
5.1.2016Roman Okolovich
When do I Annotate?
 Annotate all pointer parameters.
 Annotate value-range annotations so that Code
Analysis can ensure buffer and pointer safety.
 Annotate locking rules and locking side effects.
 Annotate driver properties and other domain-specific
properties.
 In new code, you can use SAL-based specifications
by design throughout; in older code, you can add
annotations incrementally and thereby increase the
benefits every time you update.
5.1.2016Roman Okolovich8
Links
 Analyzing C/C++ Code Quality by Using Code
Analysis
 How to: Set Code Analysis Properties for C/C++
Projects
 Understanding SAL
 Annotating Function Parameters and Return Values
 Annotating Locking Behavior
5.1.2016Roman Okolovich9

More Related Content

PPTX
C# XML documentation
PPTX
C Language (All Concept)
PPTX
Introduction of c programming unit-ii ppt
PDF
Managing I/O operations In C- Language
PPTX
C tokens
DOCX
Uniti classnotes
PDF
Learn C# programming - Program Structure & Basic Syntax
PPT
Abap course chapter 7 abap objects and bsp
C# XML documentation
C Language (All Concept)
Introduction of c programming unit-ii ppt
Managing I/O operations In C- Language
C tokens
Uniti classnotes
Learn C# programming - Program Structure & Basic Syntax
Abap course chapter 7 abap objects and bsp

What's hot (20)

PDF
Book management system
PPTX
PPTX
Complete Tokens in c/c++
PPTX
C programming
PDF
Learn C# Programming - Decision Making & Loops
PPT
Chapter2
PPTX
C language
PDF
C programming
PDF
Top C Language Interview Questions and Answer
DOC
PDF
Assignment5
PPTX
Introduction of C#
PPT
9781439035665 ppt ch04
PPTX
Chapter3: fundamental programming
PPT
oracle-reports6i
PPT
Chap02
PDF
Standards For Java Coding
PDF
Ooabap notes with_programs
PPTX
Switch case and looping
PDF
Solutions manual for c++ programming from problem analysis to program design ...
Book management system
Complete Tokens in c/c++
C programming
Learn C# Programming - Decision Making & Loops
Chapter2
C language
C programming
Top C Language Interview Questions and Answer
Assignment5
Introduction of C#
9781439035665 ppt ch04
Chapter3: fundamental programming
oracle-reports6i
Chap02
Standards For Java Coding
Ooabap notes with_programs
Switch case and looping
Solutions manual for c++ programming from problem analysis to program design ...

Similar to code analysis for c++ (20)

PPTX
Story of static code analyzer development
PDF
Technologies used in the PVS-Studio code analyzer for finding bugs and potent...
PDF
Static Code Analysis and Cppcheck
PPTX
How Data Flow analysis works in a static code analyzer
PPTX
Update on C++ Core Guidelines Lifetime Analysis. Gábor Horváth. CoreHard Spri...
PPT
Code Analysis-run time error prediction
PDF
talk_2015_07_01_Imperial_College_slides
PPTX
Detection of errors and potential vulnerabilities in C and C++ code using the...
PPTX
The Great and Mighty C++
PPT
Handling Exceptions In C &amp; C++ [Part B] Ver 2
PDF
How to Improve Visual C++ 2017 Libraries Using PVS-Studio
PDF
Cppcheck
PDF
RAII and ScopeGuard
PPTX
PVS-Studio features overview (2020)
PDF
Static Analysis: From Getting Started to Integration
DOCX
CS 112 PA #4Like the previous programming assignment, this assignm.docx
PDF
How to make fewer errors at the stage of code writing. Part N4.
PPTX
Static analysis and writing C/C++ of high quality code for embedded systems
PPT
Security related security analyst ppt.ppt
PPTX
The Use of Static Code Analysis When Teaching or Developing Open-Source Software
Story of static code analyzer development
Technologies used in the PVS-Studio code analyzer for finding bugs and potent...
Static Code Analysis and Cppcheck
How Data Flow analysis works in a static code analyzer
Update on C++ Core Guidelines Lifetime Analysis. Gábor Horváth. CoreHard Spri...
Code Analysis-run time error prediction
talk_2015_07_01_Imperial_College_slides
Detection of errors and potential vulnerabilities in C and C++ code using the...
The Great and Mighty C++
Handling Exceptions In C &amp; C++ [Part B] Ver 2
How to Improve Visual C++ 2017 Libraries Using PVS-Studio
Cppcheck
RAII and ScopeGuard
PVS-Studio features overview (2020)
Static Analysis: From Getting Started to Integration
CS 112 PA #4Like the previous programming assignment, this assignm.docx
How to make fewer errors at the stage of code writing. Part N4.
Static analysis and writing C/C++ of high quality code for embedded systems
Security related security analyst ppt.ppt
The Use of Static Code Analysis When Teaching or Developing Open-Source Software

More from Roman Okolovich (10)

PPTX
Unit tests and TDD
PPT
Using QString effectively
PDF
Ram Disk
PDF
64 bits for developers
PDF
Virtual Functions
PDF
Visual Studio 2008 Overview
PDF
State Machine Framework
PDF
The Big Three
PDF
Parallel Programming
PDF
Smart Pointers
Unit tests and TDD
Using QString effectively
Ram Disk
64 bits for developers
Virtual Functions
Visual Studio 2008 Overview
State Machine Framework
The Big Three
Parallel Programming
Smart Pointers

Recently uploaded (20)

PPTX
Hexagone difital twin solution in the desgining
PDF
IDM Crack Activation Key 2025 Free Download
PPTX
AI Tools Revolutionizing Software Development Workflows
PPTX
Relevance Tuning with Genetic Algorithms
PPTX
Independent Consultants’ Biggest Challenges in ERP Projects – and How Apagen ...
PPTX
oracle_ebs_12.2_project_cutoveroutage.pptx
PPTX
Phoenix Marketo User Group: Building Nurtures that Work for Your Audience. An...
PDF
OpenAssetIO Virtual Town Hall - August 2025.pdf
PPTX
Greedy best-first search algorithm always selects the path which appears best...
PDF
Top AI Tools for Project Managers: My 2025 AI Stack
PPTX
MCP empowers AI Agents from Zero to Production
PDF
Canva Desktop App With Crack Free Download 2025?
PDF
SBOM Document Quality Guide - OpenChain SBOM Study Group
PDF
Mobile App for Guard Tour and Reporting.pdf
PDF
Streamlining Project Management in Microsoft Project, Planner, and Teams with...
PDF
OpenColorIO Virtual Town Hall - August 2025
PPTX
SAP Business AI_L1 Overview_EXTERNAL.pptx
PDF
DOWNLOAD—IOBit Uninstaller Pro Crack Download Free
PDF
MaterialX Virtual Town Hall - August 2025
PPTX
Beige and Black Minimalist Project Deck Presentation (1).pptx
Hexagone difital twin solution in the desgining
IDM Crack Activation Key 2025 Free Download
AI Tools Revolutionizing Software Development Workflows
Relevance Tuning with Genetic Algorithms
Independent Consultants’ Biggest Challenges in ERP Projects – and How Apagen ...
oracle_ebs_12.2_project_cutoveroutage.pptx
Phoenix Marketo User Group: Building Nurtures that Work for Your Audience. An...
OpenAssetIO Virtual Town Hall - August 2025.pdf
Greedy best-first search algorithm always selects the path which appears best...
Top AI Tools for Project Managers: My 2025 AI Stack
MCP empowers AI Agents from Zero to Production
Canva Desktop App With Crack Free Download 2025?
SBOM Document Quality Guide - OpenChain SBOM Study Group
Mobile App for Guard Tour and Reporting.pdf
Streamlining Project Management in Microsoft Project, Planner, and Teams with...
OpenColorIO Virtual Town Hall - August 2025
SAP Business AI_L1 Overview_EXTERNAL.pptx
DOWNLOAD—IOBit Uninstaller Pro Crack Download Free
MaterialX Virtual Town Hall - August 2025
Beige and Black Minimalist Project Deck Presentation (1).pptx

code analysis for c++

  • 2. Overview  The C/C++ Code Analysis tool provides information to developers about possible defects in their C/C++ source code. Common coding errors reported by the tool include buffer overruns, un-initialized memory, null pointer dereferences, and memory and resource leaks. 5.1.2016Roman Okolovich2
  • 3. Source-code Annotation Language (SAL)  The Microsoft source-code annotation language (SAL) provides a set of annotations that can be used to describe how a function uses its parameters, the assumptions that it makes about them, and the guarantees that it makes when it finishes. The annotations are defined in the header file <sal.h>. Visual Studio code analysis for C++ uses SAL annotations to modify its analysis of functions.  Natively, C and C++ provide only limited ways for developers to consistently express intent and invariance.  By using SAL annotations, you can describe your functions in greater detail so that developers who are consuming them can better understand how to use them. 5.1.2016Roman Okolovich3
  • 4. SAL makes code more valuable void* memcpy( void* dest, const void* src, size_t count );  Without SAL annotations, you'd have to rely on documentation or code comments. void * memcpy( _Out_writes_bytes_all_(count) void *dest, _In_reads_bytes_(count) const void *src, size_t count );  Notice that these annotations resemble the information in the MSDN documentation, but they are more concise and they follow a semantic pattern. When you read this code, you can quickly understand the properties of this function and how to avoid buffer overrun security issues. 5.1.2016Roman Okolovich4
  • 5. Find potential bugs wchar_t * wmemcpy( _Out_writes_all_(count) wchar_t *dest, _In_reads_(count) const wchar_t *src, size_t count) { size_t i; for (i = 0; i <= count; i++) { // BUG: off-by-one error dest[i] = src[i]; } return dest; }  This implementation contains a common off-by-one error. Fortunately, the code author included the SAL buffer size annotation—a code analysis tool could catch the bug by analyzing this function alone. 5.1.2016Roman Okolovich5
  • 6.  Annotating Function Parameters and Return Values  _In_, _Out_, _Inout_, _In_z_, etc  When a pointer parameter annotation includes _opt_, it indicates that the parameter may be null  _In_opt_, _Out_opt_, _Inout_opt_, _In_opt_z_, etc  Return values  _Ret_z_, _Ret_maybenull_, _Ret_writes_to_(s,c), _Ret_notnull_, etc  Annotating Function Behavior  A function can fail, and when it does, its results may be incomplete or differ from the results when the function succeeds.  _Check_return_ - annotates a return value and states that the caller should inspect it.  _Always_(anno_list), _Success_(expr), etc  Example: annotate formal parameters and return value of the function by using the Pre and Post conditions: [returnvalue:SA_Post(Null=SA_Maybe)] LinkedList* AddTail([SA_Pre(Null=SA_Maybe)] LinkedList* node, int value) 5.1.2016Roman Okolovich6
  • 7. Specify Additional Code Information It’s possible to provide hints to the code analysis tool for C/C++ code that will help the analysis process and reduce warnings. __analysis_assume( expr ) expr - any expression that is assumed to evaluate to true. #include <windows.h> #include <codeanalysissourceannotations.h> using namespace vc_attributes; // calls free and sets ch to null void FreeAndNull(char* ch); //requires pc to be null void f([Pre(Null=Yes)] char* pc); void test( ) { char *pc = (char*)malloc(5); FreeAndNull(pc); __analysis_assume(pc == NULL); f(pc); } 5.1.2016Roman Okolovich
  • 8. When do I Annotate?  Annotate all pointer parameters.  Annotate value-range annotations so that Code Analysis can ensure buffer and pointer safety.  Annotate locking rules and locking side effects.  Annotate driver properties and other domain-specific properties.  In new code, you can use SAL-based specifications by design throughout; in older code, you can add annotations incrementally and thereby increase the benefits every time you update. 5.1.2016Roman Okolovich8
  • 9. Links  Analyzing C/C++ Code Quality by Using Code Analysis  How to: Set Code Analysis Properties for C/C++ Projects  Understanding SAL  Annotating Function Parameters and Return Values  Annotating Locking Behavior 5.1.2016Roman Okolovich9