SlideShare a Scribd company logo
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 ...
Ad

Similar to code analysis for c++ (20)

PDF
c_programming.pdf
PDF
Introduction of C++ By Pawan Thakur
PPTX
Programming-in-C
PPTX
Programming in C
PPTX
Uft Basics
PPT
Ch2 introduction to c
PPT
Ppt lesson 06
PPT
Ppt lesson 06
PPT
Ppt lesson 06
PPTX
C++.pptx
DOC
11i&r12 difference
PDF
1588147798Begining_ABUAD1.pdf
PPTX
What is algorithm
PDF
The Ring programming language version 1.10 book - Part 97 of 212
DOCX
interview questions.docx
PDF
Introduction to C Language - Version 1.0 by Mark John Lado
DOC
Project 2 the second project involves/tutorialoutlet
PPTX
object oriented programming part inheritance.pptx
PPTX
Fundamentals of computers - C Programming
c_programming.pdf
Introduction of C++ By Pawan Thakur
Programming-in-C
Programming in C
Uft Basics
Ch2 introduction to c
Ppt lesson 06
Ppt lesson 06
Ppt lesson 06
C++.pptx
11i&r12 difference
1588147798Begining_ABUAD1.pdf
What is algorithm
The Ring programming language version 1.10 book - Part 97 of 212
interview questions.docx
Introduction to C Language - Version 1.0 by Mark John Lado
Project 2 the second project involves/tutorialoutlet
object oriented programming part inheritance.pptx
Fundamentals of computers - C Programming
Ad

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)

PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Introduction to Artificial Intelligence
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
history of c programming in notes for students .pptx
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
AI in Product Development-omnex systems
PDF
top salesforce developer skills in 2025.pdf
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
System and Network Administraation Chapter 3
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
System and Network Administration Chapter 2
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Introduction to Artificial Intelligence
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Navsoft: AI-Powered Business Solutions & Custom Software Development
Odoo Companies in India – Driving Business Transformation.pdf
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Reimagine Home Health with the Power of Agentic AI​
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
history of c programming in notes for students .pptx
Upgrade and Innovation Strategies for SAP ERP Customers
AI in Product Development-omnex systems
top salesforce developer skills in 2025.pdf
Design an Analysis of Algorithms I-SECS-1021-03
System and Network Administraation Chapter 3
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Understanding Forklifts - TECH EHS Solution
Wondershare Filmora 15 Crack With Activation Key [2025
How to Migrate SBCGlobal Email to Yahoo Easily
System and Network Administration Chapter 2

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