From 6d9e7f669a6f03fa73abcae1ae36b529de1f6f3f Mon Sep 17 00:00:00 2001 From: Greg Kaiser Date: Wed, 17 Aug 2016 16:44:14 -0700 Subject: ContextHubService: Maintain hub result meaning We're squeezing a 4-byte signed value to 1-byte. The naive (implicit) cast we were using could flip this value from positive to negative, or vice versa, and completely change the meaning of the result. API freeze prevents us from fixing this properly at the moment, but we use a less naive approach to truncate this value, maintaining its meaning and logging when we've changed the value. Bug:30829863 Change-Id: I0e80af9b192066fdf36fee565a4587eb75a5ea7b --- ...android_hardware_location_ContextHubService.cpp | 27 +++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/core/jni/android_hardware_location_ContextHubService.cpp b/core/jni/android_hardware_location_ContextHubService.cpp index 9515a0e336aa..16a58b1c30c0 100644 --- a/core/jni/android_hardware_location_ContextHubService.cpp +++ b/core/jni/android_hardware_location_ContextHubService.cpp @@ -584,7 +584,32 @@ static void passOnOsResponse(uint32_t hubHandle, uint32_t msgType, header[HEADER_FIELD_HUB_HANDLE] = hubHandle; header[HEADER_FIELD_APP_INSTANCE] = OS_APP_ID; - msg[0] = rsp->result; + // Due to API constraints, at the moment we can't change the fact that + // we're changing our 4-byte response to a 1-byte value. But we can prevent + // the possible change in sign (and thus meaning) that would happen from + // a naive cast. Further, we can log when we're losing part of the value. + // TODO(b/30918279): Don't truncate this result. + int8_t truncatedResult; + bool neededToTruncate; + if (rsp->result < INT8_MIN) { + neededToTruncate = true; + truncatedResult = INT8_MIN; + } else if (rsp->result > INT8_MAX) { + neededToTruncate = true; + truncatedResult = INT8_MAX; + } else { + neededToTruncate = false; + // Since this value fits within an int8_t, this is a safe cast which + // won't change the value or sign. + truncatedResult = static_cast(rsp->result); + } + if (neededToTruncate) { + ALOGW("Response from Context Hub truncated. Value was %" PRId32 + ", but giving Java layer %" PRId8, + rsp->result, (int)truncatedResult); + } + + msg[0] = truncatedResult; if (additionalData) { memcpy(&msg[1], additionalData, additionalDataLen); -- cgit v1.2.3-59-g8ed1b