From ddf72b364323ed3f787fd41c5d5126c3521b96b7 Mon Sep 17 00:00:00 2001
From: Anton Tananaev <anton@traccar.org>
Date: Fri, 16 Dec 2022 20:32:01 -0800
Subject: Throw on invalid coordinates

---
 src/main/java/org/traccar/model/Position.java                | 10 ++++++----
 .../java/org/traccar/protocol/GlobalstarProtocolDecoder.java | 12 ++++--------
 2 files changed, 10 insertions(+), 12 deletions(-)

(limited to 'src/main/java/org')

diff --git a/src/main/java/org/traccar/model/Position.java b/src/main/java/org/traccar/model/Position.java
index 2b743433a..1286db5f2 100644
--- a/src/main/java/org/traccar/model/Position.java
+++ b/src/main/java/org/traccar/model/Position.java
@@ -227,9 +227,10 @@ public class Position extends Message {
     }
 
     public void setLatitude(double latitude) {
-        if (latitude >= -90 && latitude <= 90) {
-            this.latitude = latitude;
+        if (latitude < -90 || latitude > 90) {
+            throw new IllegalArgumentException("Latitude out of range");
         }
+        this.latitude = latitude;
     }
 
     private double longitude;
@@ -239,9 +240,10 @@ public class Position extends Message {
     }
 
     public void setLongitude(double longitude) {
-        if (longitude >= -180 && longitude <= 180) {
-            this.longitude = longitude;
+        if (longitude < -180 || longitude > 180) {
+            throw new IllegalArgumentException("Longitude out of range");
         }
+        this.longitude = longitude;
     }
 
     private double altitude; // value in meters
diff --git a/src/main/java/org/traccar/protocol/GlobalstarProtocolDecoder.java b/src/main/java/org/traccar/protocol/GlobalstarProtocolDecoder.java
index e537edf1d..0ddb95c14 100644
--- a/src/main/java/org/traccar/protocol/GlobalstarProtocolDecoder.java
+++ b/src/main/java/org/traccar/protocol/GlobalstarProtocolDecoder.java
@@ -151,15 +151,11 @@ public class GlobalstarProtocolDecoder extends BaseHttpProtocolDecoder {
 
                 position.setCourse(BitUtil.from(flags, 5) * 45);
 
-                position.setLatitude(buf.readUnsignedMedium() * 90.0 / (1 << 23));
-                if (position.getLatitude() > 90) {
-                    position.setLatitude(position.getLatitude() - 180);
-                }
+                double latitude = buf.readUnsignedMedium() * 90.0 / (1 << 23);
+                position.setLatitude(latitude > 90 ? latitude - 180 : latitude);
 
-                position.setLongitude(buf.readUnsignedMedium() * 180.0 / (1 << 23));
-                if (position.getLongitude() > 180) {
-                    position.setLongitude(position.getLongitude() - 360);
-                }
+                double longitude = buf.readUnsignedMedium() * 180.0 / (1 << 23);
+                position.setLongitude(longitude > 180 ? longitude - 360 : longitude);
 
                 int speed = buf.readUnsignedByte();
                 position.setSpeed(UnitsConverter.knotsFromKph(speed));
-- 
cgit v1.2.3