Prevent a segfault when parsing malformed nav.dat files
Instead of doing an illegal memory access (to fields[0]), properly report the problem when the line that is supposed to contain the format version of a nav.dat file is empty.
This commit is contained in:
parent
f014e8825d
commit
8fbbf91270
1 changed files with 15 additions and 3 deletions
|
@ -31,6 +31,7 @@
|
|||
#include <cstddef> // std::size_t
|
||||
#include <cerrno>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "navdb.hxx"
|
||||
|
||||
|
@ -446,14 +447,25 @@ void NavLoader::loadNav(const SGPath& path, std::size_t bytesReadSoFar,
|
|||
|
||||
unsigned int lineNumber;
|
||||
unsigned long version;
|
||||
vector<string> fields(simgear::strutils::split(line, 0, 1));
|
||||
|
||||
try {
|
||||
vector<string> fields(simgear::strutils::split(line, 0, 1));
|
||||
if (fields.empty()) {
|
||||
throw std::logic_error("");
|
||||
}
|
||||
version = std::stoul(fields[0]);
|
||||
} catch (const std::logic_error&) {
|
||||
std::string errMsg = utf8Path + ": unable to parse version from header";
|
||||
std::string strippedLine = simgear::strutils::stripTrailingNewlines(line);
|
||||
SG_LOG(SG_NAVAID, SG_ALERT, errMsg << ": " << strippedLine );
|
||||
std::string errMsg = utf8Path + ": ";
|
||||
|
||||
if (fields.empty()) {
|
||||
errMsg += "unable to parse version: empty line";
|
||||
SG_LOG(SG_NAVAID, SG_ALERT, errMsg);
|
||||
} else {
|
||||
errMsg += "unable to parse version from header";
|
||||
SG_LOG(SG_NAVAID, SG_ALERT, errMsg << ": " << strippedLine);
|
||||
}
|
||||
|
||||
throw sg_format_exception(errMsg, strippedLine);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue