CWE-197: Numeric Truncation Error(数值截断错误)

 ID: 197

类型:基础
结构:简单

状态:未完成

描述

当原始数据类型转换为范围较小的数据类型,并且在转换过程中丢失数据时,会发生截断错误。

扩展描述

当一种数据类型转换到一种取值范围更形的数据类型时,转换后会舍弃高位。潜在导致不等于原值的意外值。在使用缓冲区索引、循环次数或者仅仅是必要的状态数据时候可能需要此值。在任何情况下,都不能信任这样的值,否则系统将会处于未知状态。虽然该方法可以有效地隔离值的低位,但这种用法很少,截断通常意味着发生了实际错误。

关联视图

与“研究层面”视图(CWE-1000)相关

与“开发层面”视图(CWE-699)相关

引入模式

范围

说明

实现

 

应用平台

语言

C (出现的可能性不确定)

C++ (出现的可能性不确定)

Java (出现的可能性不确定)

C# (出现的可能性不确定)

后果

范围

冲击

可能性

完整性

技术冲击: 修改内存

丢失数据的真实值,使用损坏的数据。

 

被利用的可能性:

示例

例1

此示例虽然不可利用,但显示了与截断错误相关联的值的可能损坏:

(问题代码)

Example Language:

int intPrimitive;
short shortPrimitive;
intPrimitive = (int)(~((int)0) ^ (1 << (sizeof(int)*8-1)));
shortPrimitive = intPrimitive;
printf("Int MAXINT: %d\nShort MAXINT: %d\n", intPrimitive, shortPrimitive);

在某些系统上编译和运行上述代码时,返回以下输出:

(result)

 

Int MAXINT: 2147483647
Short MAXINT: -1

当截断值用作数组索引时,此问题可能会被利用,当64位值用作索引时,可能隐式发生,因为它们被截断为32位。

例2

在下面的Java示例中,方法UpDeTestAlsFoFiod是业务应用程序类的一部分,它更新特定产品的销售信息。该方法作为参数接收产品ID和整数销售金额。产品ID用于从以整数形式返回计数的库存对象中检索总产品计数。在调用Sales对象的方法以更新Sales Count之前,整数值将转换为short,因为方法参数需要short类型。

(问题代码)

Example Language: Java 

...
// update sales database for number of product sold with product ID
public void updateSalesForProduct(String productID, int amountSold) {


// get the total number of products in inventory database
int productCount = inventory.getProductCount(productID);
// convert integer values to short, the method for the

// sales object requires the parameters to be of type short
short count = (short) productCount;
short sold = (short) amountSold;
// update sales database for product
sales.updateSalesCount(productID, count, sold);

}
...

但是,如果整数值高于short允许的最大值,则可能发生数值截断错误。这可能导致意外结果或数据丢失或损坏。在这种情况下,销售数据库可能被错误的数据损坏。应防止从取值范围更大的数据类型显式转换为取值范围更小的数据类型。下面的示例添加了一个if语句,以验证在显式强制转换和对sales方法的调用之前,整数值小于short的最大值。

 

(正确代码)

Example Language: Java 

...
// update sales database for number of product sold with product ID
public void updateSalesForProduct(String productID, int amountSold) {


// get the total number of products in inventory database
int productCount = inventory.getProductCount(productID);
// make sure that integer numbers are not greater than

// maximum value for type short before converting
if ((productCount < Short.MAX_VALUE) && (amountSold < Short.MAX_VALUE)) {


// convert integer values to short, the method for the

// sales object requires the parameters to be of type short
short count = (short) productCount;
short sold = (short) amountSold;
// update sales database for product
sales.updateSalesCount(productID, count, sold);


else {
// throw exception or perform other processing

...

}

}
...

应对措施

阶段: 实现

确保不会发生将数据转换到具有更小取值范围的数据类型的显式或者隐式强制转换。

种属

 

关系

类型

ID

名称

属于

738

CERT C Secure Coding Standard (2008) Chapter 5 - Integers (INT)

属于

848

The CERT Oracle Secure Coding Standard for Java (2011) Chapter 5 - Numeric Types and Operations (NUM)

属于

872

CERT C++ Secure Coding Section 04 - Integers (INT)

属于

998

SFP Secondary Cluster: Glitch in Computation

属于

1137

SEI CERT Oracle Secure Coding Standard for Java - Guidelines 03. Numeric Types and Operations (NUM)

属于

1158

SEI CERT C Coding Standard - Guidelines 04. Integers (INT)

属于

1159

SEI CERT C Coding Standard - Guidelines 05. Floating Point (FLP)

属于

1163

SEI CERT C Coding Standard - Guidelines 09. Input Output (FIO)

说明

研究空白

尽管2008年和2009年发布了流行软件中的漏洞,但此弱点一直处于研究不够且报道中不足的状态。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值