altoslib: Check for null state.gps before accessing it in eeprom records
authorKeith Packard <keithp@keithp.com>
Tue, 16 Apr 2013 06:14:22 +0000 (23:14 -0700)
committerKeith Packard <keithp@keithp.com>
Tue, 16 Apr 2013 06:14:22 +0000 (23:14 -0700)
Used to be we'd set state.gps to garbage before seeing the first GPS
record; now we leave it null, which will cause crashes for code that
doesn't expect it. The code for reading and replaying eeprom data was
not checking and was nicely crashing as a result.

Signed-off-by: Keith Packard <keithp@keithp.com>
altoslib/AltosEepromIterable.java

index bc698c8064a50723bf0d279f148b44c2043e6342..7a8bbdea2f4a055d90350c456afa33dbd8fbeba6 100644 (file)
@@ -131,24 +131,34 @@ public class AltosEepromIterable extends AltosRecordIterable {
                case AltosLib.AO_LOG_GPS_LAT:
                        eeprom.seen |= AltosRecord.seen_gps_lat;
                        int lat32 = record.a | (record.b << 16);
+                       if (state.gps == null)
+                               state.gps = new AltosGPS();
                        state.gps.lat = (double) lat32 / 1e7;
                        break;
                case AltosLib.AO_LOG_GPS_LON:
                        eeprom.seen |= AltosRecord.seen_gps_lon;
                        int lon32 = record.a | (record.b << 16);
+                       if (state.gps == null)
+                               state.gps = new AltosGPS();
                        state.gps.lon = (double) lon32 / 1e7;
                        break;
                case AltosLib.AO_LOG_GPS_ALT:
+                       if (state.gps == null)
+                               state.gps = new AltosGPS();
                        state.gps.alt = record.a;
                        break;
                case AltosLib.AO_LOG_GPS_SAT:
                        if (state.tick == eeprom.gps_tick) {
                                int svid = record.a;
                                int c_n0 = record.b >> 8;
+                               if (state.gps == null)
+                                       state.gps = new AltosGPS();
                                state.gps.add_sat(svid, c_n0);
                        }
                        break;
                case AltosLib.AO_LOG_GPS_DATE:
+                       if (state.gps == null)
+                               state.gps = new AltosGPS();
                        state.gps.year = (record.a & 0xff) + 2000;
                        state.gps.month = record.a >> 8;
                        state.gps.day = record.b & 0xff;