diff options
| author | 2025-01-08 07:14:20 -0800 | |
|---|---|---|
| committer | 2025-01-08 09:11:40 -0800 | |
| commit | 2bc155dedd1ce8c1d281d6e18d07452a98ee1832 (patch) | |
| tree | 670008d099874d6c6e6f07bf62b6e674fe08c749 | |
| parent | 086613e083b54b51c534ae3562432f34980b3fb7 (diff) | |
LocationFudger: Avoid division by zero
Following internal discussions, this CL removes a rarely-occuring bug.
At latitudes -+90°, cos() is 0, leading to a division by zero.
This exception happens only if the device location (plus offset) is exactly at the North or South pole, so I assume it never happened in practice.
Change-Id: I6e91791e1b7e87ed88e48cff13a0af9b3a680908
Test: atest FrameworksMockingServicesTests:LocationFudgerTest
Bug: 388189593
Flag: EXEMPT Bug Fix
| -rw-r--r-- | services/core/java/com/android/server/location/fudger/LocationFudger.java | 11 | 
1 files changed, 10 insertions, 1 deletions
diff --git a/services/core/java/com/android/server/location/fudger/LocationFudger.java b/services/core/java/com/android/server/location/fudger/LocationFudger.java index 88a269706470..27ad555f3701 100644 --- a/services/core/java/com/android/server/location/fudger/LocationFudger.java +++ b/services/core/java/com/android/server/location/fudger/LocationFudger.java @@ -239,6 +239,15 @@ public class LocationFudger {      // requires latitude since longitudinal distances change with distance from equator.      private static double metersToDegreesLongitude(double distance, double lat) { -        return distance / APPROXIMATE_METERS_PER_DEGREE_AT_EQUATOR / Math.cos(Math.toRadians(lat)); +        // Needed to convert from longitude distance to longitude degree. +        // X meters near the poles is more degrees than at the equator. +        double cosLat = Math.cos(Math.toRadians(lat)); +        // If we are right on top of the pole, the degree is always 0. +        // We return a very small value instead to avoid divide by zero errors +        // later on. +        if (cosLat == 0.0) { +            return 0.0001; +        } +        return distance / APPROXIMATE_METERS_PER_DEGREE_AT_EQUATOR / cosLat;      }  }  |