c++11 使用c++风格的cast: static_cast<type>(expression) const_cast<type> dynamic_cast reinterpret_cast
来源:互联网 发布:北京程序员过剩 编辑:程序博客网 时间:2024/05/29 09:21
c++编程中, 尽量避免使用c语言风格的 cast,
EXP05-CPP. Do not use C-style casts
C++ allows the traditional C-style casts, although it has introduced its own casts:
static_cast<type>(expression)
const_cast<type>(expression)
dynamic_cast<type>(expression)
reinterpret_cast<type>(expression)
C++ casts allow for more compiler checking and thus are considerably safer to use. They are also easier to find in source code (either by tools or by human readers).
Non-Compliant Code Example (static_cast()
)
In this example, a C-style cast is used to convert an int
to a double
:
Compliant Solution (static_cast()
)
Using the new cast, the division should be written as:
This code is safer (as the compiler can check that it really is a static type conversion), and the cast is easier to find.
Non-Compliant Code Example (const_cast()
)
In this example, a C-style cast is used to remove the const
ness of a function parameter:
Compliant Solution (const_cast()
)
Using the new cast, the function call should be written as:
Again, this is safer (as the compiler can check that the only conversion is to remove the const
ness), and it is easier to find.
Note that this code runs afoul of EXP55-CPP. Do not access a cv-qualified object through a cv-unqualified type.
The const_cast
may also be used to cast away volatility, but that is forbidden by VOID EXP32-CPP. Do not access a volatile object through a non-volatile reference.
Non-Compliant Code Example (dynamic_cast()
)
In this example, a C-style cast is used to convert a type in an inheritance heirarchy:
Compliant Solution (dynamic_cast()
)
Using the new cast, the function call should be written as:
In this case, the compiler can check that it really is a conversion between two types in the same inheritance heirarchy.
Non-Compliant Code Example (reinterpret_cast()
)
In this example, a C-style cast is used to convert a double
function pointer to an int
function pointer:
Compliant Solution (reinterpret_cast()
)
Using the new cast, the assignment should be written as:
Once again, the compliant code has the advantage that the cast is much more visible than if a C-style cast is used (although the compiler is not able to check much in the case of a reinterpret_cast
).
Risk Assessment
Using C-style casts can lead to type errors because the compiler is unable to apply the checking that is possible when using the more restrictive C++ casts. Type errors could lead to an attacker being able to execute arbitrary code.
Rule
Severity
Likelihood
Remediation Cost
Priority
Level
EXP05-CPP
high
probable
medium
P12
L1
Automated Detection
Tool
Version
Checker
Description
ECLAIR
1.2CP1.EXP05
Fully implemented
PRQA QA-C++v3.23080,3082- c++11 使用c++风格的cast: static_cast<type>(expression) const_cast<type> dynamic_cast reinterpret_cast
- static_cast,dynamic_cast,reinterpret_cast const_cast于c语言cast的区别
- bad_cast,const_cast,dynamic_cast,reinterpret_cast,static_cast(C++)
- 【C++】static_cast, dynamic_cast, reinterpret_cast, const_cast区别比较
- [C++]const_cast,dynamic_cast,reinterpret_cast,static_cast转型
- <C++> dynamic_cast、const_cast、static_cast、reinterpret_cast总结
- static_cast,dynamic_cast,reinterpret_cast,const_cast的使用
- C语言拾遗:static_cast, dynamic_cast, reinterpret_cast, const_cast区别比较
- 【C++】强制类型转换(static_cast,reinterpret_cast,const_cast,dynamic_cast,explicit)
- [C++] 强制类型转换static_cast、dynamic_cast、reinterpret_cast和const_cast
- c++dynamic_cast、const_cast 、static_cast、reinterpret_cast强制类型转换
- dynamic_cast、const_cast、static_cast、reinterpret_cast
- static_cast, dynamic_cast, const_cast, reinterpret_cast
- const_cast,reinterpret_cast,static_cast,dynamic_cast
- const_cast、static_cast、reinterpret_cast、dynamic_cast
- static_cast,const_cast,dynamic_cast,reinterpret_cast
- dynamic_cast;const_cast;static_cast;reinterpret_cast;
- dynamic_cast 、reinterpret_cast 、 static_cast 、 const_cast
- Mac下OpenCV3.0和OpenCV2.x自由切换
- Golang for循环
- ATS日志格式中的Squid-和Netscape-格式的缓存结果码
- 调用有道词典查词
- java基础入门-多线程同步浅析-以银行转账为例子
- c++11 使用c++风格的cast: static_cast<type>(expression) const_cast<type> dynamic_cast reinterpret_cast
- NBA史上薪水最高球员排行榜
- 安卓手机开发的学习资料
- C++的内存模型
- 深度解读:都是顶薪为什么浓眉远超卡哇伊?
- vim编辑器颜色主题color scheme
- SQL Server中的DBlink的使用
- 《编程导论(Java)·3.2.4 循环语句》
- 《JAVA程序设计》实训第一天——《猜猜看》游戏