From 83c942a512ee9d35ac12464b9799ef42736a38aa Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 8 Oct 2013 11:10:53 +1100 Subject: [PATCH] AP_Compass: prevent NULL deref on enable mag while running --- libraries/AP_Compass/AP_Compass_HMC5843.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/libraries/AP_Compass/AP_Compass_HMC5843.cpp b/libraries/AP_Compass/AP_Compass_HMC5843.cpp index 0eb4ca020b..e61244aa16 100644 --- a/libraries/AP_Compass/AP_Compass_HMC5843.cpp +++ b/libraries/AP_Compass/AP_Compass_HMC5843.cpp @@ -116,6 +116,12 @@ bool AP_Compass_HMC5843::read_raw() // accumulate a reading from the magnetometer void AP_Compass_HMC5843::accumulate(void) { + if (!_initialised) { + // someone has tried to enable a compass for the first time + // mid-flight .... we can't do that yet (especially as we won't + // have the right orientation!) + return; + } uint32_t tnow = hal.scheduler->micros(); if (healthy && _accum_count != 0 && (tnow - _last_accum_time) < 13333) { // the compass gets new data at 75Hz @@ -182,6 +188,7 @@ AP_Compass_HMC5843::init() } // determine if we are using 5843 or 5883L + _base_config = 0; if (!write_register(ConfigRegA, SampleAveraging_8<<5 | DataOutputRate_75HZ<<2 | NormalOperation) || !read_register(ConfigRegA, &_base_config)) { healthy = false;