From 17485e8ed43d680befe23c2982b39410e29facd2 Mon Sep 17 00:00:00 2001 From: Simon Jensen Date: Tue, 24 Jul 2018 11:52:49 -0700 Subject: [PATCH] Fix conversion from double to int16_t in JSC bindings Summary: Converting from double to int16_t is undefined behavior if the value doesn't fit in int16_t. Reviewed By: tmikov Differential Revision: D8925246 fbshipit-source-id: 4cf57631686a4ce05546f8d30a46e3f9def3aee2 --- ReactAndroid/src/main/jni/react/jni/JSCPerfLogging.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ReactAndroid/src/main/jni/react/jni/JSCPerfLogging.cpp b/ReactAndroid/src/main/jni/react/jni/JSCPerfLogging.cpp index 6516932e196163..5cbc983c92f2eb 100644 --- a/ReactAndroid/src/main/jni/react/jni/JSCPerfLogging.cpp +++ b/ReactAndroid/src/main/jni/react/jni/JSCPerfLogging.cpp @@ -177,7 +177,8 @@ static JSValueRef nativeQPLMarkerEnd( if (isReady() && grabDoubles(4, targets, ctx, argumentCount, arguments, exception)) { int32_t markerId = (int32_t) targets[0]; int32_t instanceKey = (int32_t) targets[1]; - int16_t actionId = (int16_t) targets[2]; + // NOTE: avoid undefined behavior when the value does not find in int16_t. + int16_t actionId = (int16_t) (int32_t) targets[2]; int64_t timestamp = (int64_t) targets[3]; JQuickPerformanceLoggerProvider::get()->markerEnd(markerId, instanceKey, actionId, timestamp); } @@ -230,7 +231,8 @@ static JSValueRef nativeQPLMarkerNote( if (isReady() && grabDoubles(4, targets, ctx, argumentCount, arguments, exception)) { int32_t markerId = (int32_t) targets[0]; int32_t instanceKey = (int32_t) targets[1]; - int16_t actionId = (int16_t) targets[2]; + // NOTE: avoid undefined behavior when the value does not find in int16_t. + int16_t actionId = (int16_t) (int32_t) targets[2]; int64_t timestamp = (int64_t) targets[3]; JQuickPerformanceLoggerProvider::get()->markerNote(markerId, instanceKey, actionId, timestamp); }