博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++11 使用c++风格的cast: static_cast<type>(expression) const_cast<type> dynamic_cast reinterpret_cast
阅读量:2182 次
发布时间:2019-05-01

本文共 3327 字,大约阅读时间需要 11 分钟。

c++编程中, 尽量避免使用c语言风格的 cast,

具体实例如下:

Icon

This guideline has not been reviewed recently and may be outdated. Please review it and comment to reflect any newly available information.

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:

int 
dividend, divisor;
// ...
double 
result = ((
double
)dividend)/divisor;

Compliant Solution (static_cast())

Using the new cast, the division should be written as:

double 
result = 
static_cast
<
double
>(dividend)/divisor;

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 constness of a function parameter:

void 
doit(SomeType *pst);
// ...
SomeType st;
SomeType 
const 
& cst = st;
// ...
doit((SomeType*)&cst);

Compliant Solution (const_cast())

Using the new cast, the function call should be written as:

doit(
const_cast
<SomeType*>(&cst));

Again, this is safer (as the compiler can check that the only conversion is to remove the constness), and it is easier to find.

Note that this code runs afoul of .

The const_cast may also be used to cast away volatility, but that is forbidden by .

Non-Compliant Code Example (dynamic_cast())

In this example, a C-style cast is used to convert a type in an inheritance heirarchy:

class 
BaseType {
// ...};
class 
DerivedType: 
public 
BaseType {
// ...};
// ...
void 
doit(DerivedType *pdt);
// ...
BaseType *apbt[3];
// ...
apbt[1] = 
new 
DerivedType;
// ...
doit((DerivedType*)apbt[1]);

Compliant Solution (dynamic_cast())

Using the new cast, the function call should be written as:

doit(
dynamic_cast
<DerivedType*>(apbt[1]));

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:

typedef 
int 
(*IntFuncPtr)();
// ...
IntFuncPtr ifpArray[4];
// ...
double 
doit();
// ...
ifpArray[2] = (IntFuncPtr)&doit;

Compliant Solution (reinterpret_cast())

Using the new cast, the assignment should be written as:

ifpArray[
2
] = reinterpret_cast<IntFuncPtr>(&doit);

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

1.2

CP1.EXP05

Fully implemented

v3.2 3080,3082  

转载地址:http://lttkb.baihongyu.com/

你可能感兴趣的文章
探索Redis设计与实现9:数据库redisDb与键过期删除策略
查看>>
探索Redis设计与实现10:Redis的事件驱动模型与命令执行过程
查看>>
分布式系统理论基础1: 一致性、2PC和3PC
查看>>
分布式系统理论基础2 :CAP
查看>>
分布式系统理论基础3: 时间、时钟和事件顺序
查看>>
分布式系统理论基础4:Paxos
查看>>
分布式系统理论基础5:选举、多数派和租约
查看>>
分布式系统理论基础6:Raft、Zab
查看>>
分布式系统理论进阶7:Paxos变种和优化
查看>>
分布式系统理论基础8:zookeeper分布式协调服务
查看>>
搞懂分布式技术1:分布式系统的一些基本概念
查看>>
搞懂分布式技术2:分布式一致性协议与Paxos,Raft算法
查看>>
搞懂分布式技术3:初探分布式协调服务zookeeper
查看>>
搞懂分布式技术4:ZAB协议概述与选主流程详解
查看>>
搞懂分布式技术5:Zookeeper的配置与集群管理实战
查看>>
搞懂分布式技术6:Zookeeper典型应用场景及实践
查看>>
搞懂分布式技术10:LVS实现负载均衡的原理与实践
查看>>
搞懂分布式技术11:分布式session解决方案与一致性hash
查看>>
搞懂分布式技术12:分布式ID生成方案
查看>>
搞懂分布式技术13:缓存的那些事
查看>>