Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
chakrashim: handle Value::ToInteger overflow case
Browse files Browse the repository at this point in the history
When value exceeds int range, should not call As<>() which asserts value
range. Use direct cast to preserve the value. The code applies to Infinity,
refactor and use the same code.

Reviewed-by: @kunalspathak
  • Loading branch information
Jianchun Xu committed Dec 3, 2015
1 parent b6e38eb commit f14d337
Showing 1 changed file with 9 additions and 22 deletions.
31 changes: 9 additions & 22 deletions deps/chakrashim/src/v8value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -258,29 +258,16 @@ MaybeLocal<Integer> Value::ToInteger(Local<Context> context) const {
}
double doubleValue = maybe.FromJust();

// If this is Infinity, return number as it is.
if (isinf(doubleValue)) {
// v8::Integer::Cast hits assert since the number is not a valid Int32, but
// still want to return Integer to align with v8. Hence directly cast to
// Integer here.
return Local<Integer>(
reinterpret_cast<Integer*>(const_cast<Value*>(this)));
}

// return 0 if this is NaN.
if (doubleValue != doubleValue) {
return Integer::New(nullptr, 0);
}

int64_t value = static_cast<int64_t>(doubleValue);
double value = isfinite(doubleValue) ? trunc(doubleValue) :
(isnan(doubleValue) ? 0 : doubleValue);
int intValue = static_cast<int>(value);

if (value == static_cast<int64_t>(intValue)) {
if (value == intValue) {
return Integer::New(nullptr, intValue);
}

// does not fit int, use double
return Number::New(nullptr, value).As<Integer>();
Value* result = value == doubleValue ?
const_cast<Value*>(this) : *Number::New(nullptr, value);
return Local<Integer>(reinterpret_cast<Integer*>(result));
}

MaybeLocal<Uint32> Value::ToUint32(Local<Context> context) const {
Expand Down Expand Up @@ -367,9 +354,9 @@ Maybe<double> Value::NumberValue(Local<Context> context) const {
}

Maybe<int64_t> Value::IntegerValue(Local<Context> context) const {
Maybe<double> maybe = NumberValue(context);
return maybe.IsNothing() ?
Nothing<int64_t>() : Just(static_cast<int64_t>(maybe.FromJust()));
Maybe<double> maybe = NumberValue(context);
return maybe.IsNothing() ?
Nothing<int64_t>() : Just(static_cast<int64_t>(maybe.FromJust()));
}

Maybe<uint32_t> Value::Uint32Value(Local<Context> context) const {
Expand Down

0 comments on commit f14d337

Please sign in to comment.