Project

General

Profile

Actions

Bug #7371

closed

Fix undefined overflow checking in bigdecimal

Added by xi (Xi Wang) over 11 years ago. Updated over 10 years ago.

Status:
Closed
Target version:
-
ruby -v:
1.9.x
Backport:
[ruby-core:49411]

Description

In AddExponent() at ext/bigdecimal/bigdecimal.c:3677, the overflow checks rely on signed integer overflow, which is undefined behavior in C.

SIGNED_VALUE m = e+n;
SIGNED_VALUE eb, mb;
if(e>0) {
    if(n>0) {
        mb = m*(SIGNED_VALUE)BASE_FIG;
        eb = e*(SIGNED_VALUE)BASE_FIG;
        if(mb<eb) goto overflow;
    }

Some compilers (e.g., gcc 4.8) will optimize away such overflow checks due to undefined behavior. Ruby currently uses "-fno-strict-overflow" to disable such offending optimizations in gcc, but this workaround option is not supported by other compilers, thus not portable.

The attached patch uses unsigned multiplication for overflow checking, which is well defined in C.


Files

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0