OSDN Git Service

Prevent overflow in LocationRequest.setExpireIn()
authorLaurent Tu <laurentt@google.com>
Thu, 4 Oct 2012 20:40:08 +0000 (13:40 -0700)
committerLaurent Tu <laurentt@google.com>
Thu, 4 Oct 2012 20:40:08 +0000 (13:40 -0700)
Prevent overflow in LocationRequest.setExpireIn(), for example,
when we pass in Long.MAX_VALUE.

Bug: 7047435
Change-Id: Ie56928a59fb387173fbd3887c0ef9aede00f8152

location/java/android/location/LocationRequest.java

index f4f7b09..cb291ea 100644 (file)
@@ -369,7 +369,15 @@ public final class LocationRequest implements Parcelable {
      * @return the same object, so that setters can be chained
      */
     public LocationRequest setExpireIn(long millis) {
-        mExpireAt = millis + SystemClock.elapsedRealtime();
+        long elapsedRealtime = SystemClock.elapsedRealtime();
+
+        // Check for > Long.MAX_VALUE overflow (elapsedRealtime > 0):
+        if (millis > Long.MAX_VALUE - elapsedRealtime) {
+          mExpireAt = Long.MAX_VALUE;
+        } else {
+          mExpireAt = millis + elapsedRealtime;
+        }
+
         if (mExpireAt < 0) mExpireAt = 0;
         return this;
     }