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
Roman Okolovich
 
PPTX
C Language (All Concept)
sachindane
 
PPTX
Introduction of c programming unit-ii ppt
JStalinAsstProfessor
 
PDF
Managing I/O operations In C- Language
RavindraSalunke3
 
PPTX
C tokens
Manu1325
 
DOCX
Uniti classnotes
Sowri Rajan
 
PDF
Learn C# programming - Program Structure & Basic Syntax
Eng Teong Cheah
 
PPT
Abap course chapter 7 abap objects and bsp
Milind Patil
 
C# XML documentation
Roman Okolovich
 
C Language (All Concept)
sachindane
 
Introduction of c programming unit-ii ppt
JStalinAsstProfessor
 
Managing I/O operations In C- Language
RavindraSalunke3
 
C tokens
Manu1325
 
Uniti classnotes
Sowri Rajan
 
Learn C# programming - Program Structure & Basic Syntax
Eng Teong Cheah
 
Abap course chapter 7 abap objects and bsp
Milind Patil
 

What's hot (20)

PDF
Book management system
SHARDA SHARAN
 
PPTX
C languaGE UNIT-1
Malikireddy Bramhananda Reddy
 
PPTX
Complete Tokens in c/c++
Shobi P P
 
PPTX
C programming
PralhadKhanal1
 
PDF
Learn C# Programming - Decision Making & Loops
Eng Teong Cheah
 
PPT
Chapter2
Anees999
 
PPTX
C language
Arafat Bin Reza
 
PDF
C programming
saniabhalla
 
PDF
Top C Language Interview Questions and Answer
Vineet Kumar Saini
 
DOC
Cnotes
Muthuganesh S
 
PDF
Assignment5
Sunita Milind Dol
 
PPTX
Introduction of C#
Eng Teong Cheah
 
PPT
9781439035665 ppt ch04
Terry Yoast
 
PPTX
Chapter3: fundamental programming
Ngeam Soly
 
PPT
oracle-reports6i
CHANDRASEKHAR REDROUTHU
 
PPT
Chap02
Terry Yoast
 
PDF
Standards For Java Coding
Rahul Bhutkar
 
PDF
Ooabap notes with_programs
Kranthi Kumar
 
PPTX
Switch case and looping
patricia Hidalgo
 
PDF
Solutions manual for c++ programming from problem analysis to program design ...
Warren0989
 
Book management system
SHARDA SHARAN
 
Complete Tokens in c/c++
Shobi P P
 
C programming
PralhadKhanal1
 
Learn C# Programming - Decision Making & Loops
Eng Teong Cheah
 
Chapter2
Anees999
 
C language
Arafat Bin Reza
 
C programming
saniabhalla
 
Top C Language Interview Questions and Answer
Vineet Kumar Saini
 
Assignment5
Sunita Milind Dol
 
Introduction of C#
Eng Teong Cheah
 
9781439035665 ppt ch04
Terry Yoast
 
Chapter3: fundamental programming
Ngeam Soly
 
oracle-reports6i
CHANDRASEKHAR REDROUTHU
 
Chap02
Terry Yoast
 
Standards For Java Coding
Rahul Bhutkar
 
Ooabap notes with_programs
Kranthi Kumar
 
Switch case and looping
patricia Hidalgo
 
Solutions manual for c++ programming from problem analysis to program design ...
Warren0989
 
Ad

Similar to code analysis for c++ (20)

PDF
c_programming.pdf
Home
 
PDF
Introduction of C++ By Pawan Thakur
Govt. P.G. College Dharamshala
 
PPTX
Programming-in-C
DrPrabakaranPerumal
 
PPTX
Programming in C
DrPrabakaranPerumal
 
PPTX
Uft Basics
Archana Krushnan
 
PPT
Ch2 introduction to c
Hattori Sidek
 
PPT
Ppt lesson 06
Linda Bodrie
 
PPT
Ppt lesson 06
Linda Bodrie
 
PPT
Ppt lesson 06
Linda Bodrie
 
PPTX
C++.pptx
Sabi995708
 
DOC
11i&r12 difference
venki_venki
 
PDF
1588147798Begining_ABUAD1.pdf
SemsemSameer1
 
PPTX
What is algorithm
mshoaib15
 
PDF
The Ring programming language version 1.10 book - Part 97 of 212
Mahmoud Samir Fayed
 
DOCX
interview questions.docx
SeoTechnoscripts
 
PDF
Introduction to C Language - Version 1.0 by Mark John Lado
Mark John Lado, MIT
 
DOC
Project 2 the second project involves/tutorialoutlet
Messanz
 
PPTX
object oriented programming part inheritance.pptx
urvashipundir04
 
PPTX
Fundamentals of computers - C Programming
MSridhar18
 
c_programming.pdf
Home
 
Introduction of C++ By Pawan Thakur
Govt. P.G. College Dharamshala
 
Programming-in-C
DrPrabakaranPerumal
 
Programming in C
DrPrabakaranPerumal
 
Uft Basics
Archana Krushnan
 
Ch2 introduction to c
Hattori Sidek
 
Ppt lesson 06
Linda Bodrie
 
Ppt lesson 06
Linda Bodrie
 
Ppt lesson 06
Linda Bodrie
 
C++.pptx
Sabi995708
 
11i&r12 difference
venki_venki
 
1588147798Begining_ABUAD1.pdf
SemsemSameer1
 
What is algorithm
mshoaib15
 
The Ring programming language version 1.10 book - Part 97 of 212
Mahmoud Samir Fayed
 
interview questions.docx
SeoTechnoscripts
 
Introduction to C Language - Version 1.0 by Mark John Lado
Mark John Lado, MIT
 
Project 2 the second project involves/tutorialoutlet
Messanz
 
object oriented programming part inheritance.pptx
urvashipundir04
 
Fundamentals of computers - C Programming
MSridhar18
 
Ad

More from Roman Okolovich (10)

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

Recently uploaded (20)

PDF
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PDF
New Download MiniTool Partition Wizard Crack Latest Version 2025
imang66g
 
PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
PPTX
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
PPTX
Presentation about Database and Database Administrator
abhishekchauhan86963
 
PDF
Summary Of Odoo 18.1 to 18.4 : The Way For Odoo 19
CandidRoot Solutions Private Limited
 
PPT
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
PDF
Protecting the Digital World Cyber Securit
dnthakkar16
 
PPTX
Can You Build Dashboards Using Open Source Visualization Tool.pptx
Varsha Nayak
 
PDF
10 posting ideas for community engagement with AI prompts
Pankaj Taneja
 
PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
PPTX
TRAVEL APIs | WHITE LABEL TRAVEL API | TOP TRAVEL APIs
philipnathen82
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PDF
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
PDF
Exploring AI Agents in Process Industries
amoreira6
 
PPTX
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
New Download MiniTool Partition Wizard Crack Latest Version 2025
imang66g
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
Presentation about Database and Database Administrator
abhishekchauhan86963
 
Summary Of Odoo 18.1 to 18.4 : The Way For Odoo 19
CandidRoot Solutions Private Limited
 
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
Protecting the Digital World Cyber Securit
dnthakkar16
 
Can You Build Dashboards Using Open Source Visualization Tool.pptx
Varsha Nayak
 
10 posting ideas for community engagement with AI prompts
Pankaj Taneja
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
TRAVEL APIs | WHITE LABEL TRAVEL API | TOP TRAVEL APIs
philipnathen82
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
Exploring AI Agents in Process Industries
amoreira6
 
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 

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