From b0b81bcd3e633dee63fa3f87543bb830e6281486 Mon Sep 17 00:00:00 2001 From: Florent Rougon Date: Sun, 24 Apr 2016 19:33:41 +0200 Subject: [PATCH] apt.dat parser: better input stream handling - use ( in.getline(...) ) as the main loop condition instead of ( ! in.eof() ). This should behave better (see ); - check in.bad() after exiting from each reading loop; if the exit was caused by an error, log an appropriate message and throw an exception. --- src/Airports/apt_loader.cxx | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Airports/apt_loader.cxx b/src/Airports/apt_loader.cxx index a7ad198ac..9545dddfa 100644 --- a/src/Airports/apt_loader.cxx +++ b/src/Airports/apt_loader.cxx @@ -128,8 +128,9 @@ public: } } // end of the apt.dat header - while ( ! in.eof() ) { - in.getline(tmp, 2048); + throwExceptionIfStreamError(in, "apt.dat", apt_dat); + + while ( in.getline(tmp, 2048) ) { line = tmp; // string copy, ack line_num++; @@ -203,6 +204,7 @@ public: } } + throwExceptionIfStreamError(in, "apt.dat", apt_dat); finishAirport(); } @@ -225,7 +227,19 @@ private: NavDataCache* cache; PositionedID currentAirportID; - + + void throwExceptionIfStreamError(const sg_gzifstream& input_stream, + const std::string& short_name, + const std::string& full_path) + { + if ( input_stream.bad() ) { + // strerror() isn't thread-safe, unfortunately... + SG_LOG( SG_GENERAL, SG_ALERT, "error while reading " << full_path ); + throw sg_io_exception("error while reading " + short_name, + full_path.c_str()); + } + } + void finishAirport() { if (currentAirportID == 0) { -- 2.39.5