diff options
author | Eric S. Raymond <esr@thyrsus.com> | 2011-07-06 13:29:44 -0400 |
---|---|---|
committer | Eric S. Raymond <esr@thyrsus.com> | 2011-07-06 13:29:44 -0400 |
commit | ce15eae5b1170acebdbe88dd9a8a011a6d6957fb (patch) | |
tree | d4cf0ce08c1f457d2823630b4ebedee8f109b5e9 | |
parent | 4ac0042e1c4aee1206435728d4c68d195a1964e2 (diff) | |
download | gpsd-ce15eae5b1170acebdbe88dd9a8a011a6d6957fb.tar.gz |
Prevent the C++ test client from segfaulting when GPSD is not running.
C++ clients should test the return from the stream method to see if the
open succeeded.
-rw-r--r-- | libgpsmm.cpp | 77 | ||||
-rw-r--r-- | test_gpsmm.cpp | 5 |
2 files changed, 44 insertions, 38 deletions
diff --git a/libgpsmm.cpp b/libgpsmm.cpp index 0dc20562..8df9af98 100644 --- a/libgpsmm.cpp +++ b/libgpsmm.cpp @@ -13,76 +13,79 @@ struct gps_data_t* gpsmm::gps_inner_open(const char *host, const char *port) { - const bool err = (gps_open(host, port, gps_state()) != 0); - if ( err ) { - return NULL; - } - else { // connection successfully opened - to_user= new struct gps_data_t; - return backup(); //we return the backup of our internal structure - } + const bool err = (gps_open(host, port, gps_state()) != 0); + if ( err ) { + to_user = NULL; + return NULL; + } + else { // connection successfully opened + to_user= new struct gps_data_t; + return backup(); //we return the backup of our internal structure + } } struct gps_data_t* gpsmm::stream(int flags) { - if (gps_stream(gps_state(),flags, NULL)==-1) { - return NULL; - } - else { - return backup(); - } + if (to_user == NULL) + return NULL; + else if (gps_stream(gps_state(),flags, NULL)==-1) { + return NULL; + } + else { + return backup(); + } } struct gps_data_t* gpsmm::send(const char *request) { - if (gps_send(gps_state(),request)==-1) { - return NULL; - } - else { - return backup(); - } + if (gps_send(gps_state(),request)==-1) { + return NULL; + } + else { + return backup(); + } } struct gps_data_t* gpsmm::read(void) { - if (gps_read(gps_state())<=0) { - // we return null if there was a read() error, if no - // data was ready in POLL_NOBLOCK mode, or if the - // connection is closed by gpsd - return NULL; - } - else { - return backup(); - } + if (gps_read(gps_state())<=0) { + // we return null if there was a read() error, if no + // data was ready in POLL_NOBLOCK mode, or if the + // connection is closed by gpsd + return NULL; + } + else { + return backup(); + } } bool gpsmm::waiting(int timeout) { - return gps_waiting(gps_state(), timeout); + return gps_waiting(gps_state(), timeout); } const char *gpsmm::data(void) { - return gps_data(gps_state()); + return gps_data(gps_state()); } void gpsmm::clear_fix(void) { - gps_clear_fix(&(gps_state()->fix)); + gps_clear_fix(&(gps_state()->fix)); } void gpsmm::enable_debug(int level, FILE *fp) { #ifdef CLIENTDEBUG_ENABLE - gps_enable_debug(level, fp); + gps_enable_debug(level, fp); #endif /* CLIENTDEBUG_ENABLE */ } gpsmm::~gpsmm() { - if ( to_user != NULL ) { - gps_close(gps_state()); - delete to_user; - } + if ( to_user != NULL ) { + gps_close(gps_state()); + delete to_user; + } } #endif /* S_SPLINT_S */ diff --git a/test_gpsmm.cpp b/test_gpsmm.cpp index f12de562..c95bf2b0 100644 --- a/test_gpsmm.cpp +++ b/test_gpsmm.cpp @@ -98,7 +98,10 @@ int main(void) { gpsmm gps_rec("localhost", DEFAULT_GPSD_PORT); - gps_rec.stream(WATCH_ENABLE|WATCH_JSON); + if (gps_rec.stream(WATCH_ENABLE|WATCH_JSON) == NULL) { + cerr << "No GPSD running.\n"; + return 1; + } for (;;) { struct gps_data_t* newdata; |