1
0
Fork 0

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:
Florent Rougon 2019-03-27 07:54:24 +01:00
parent f014e8825d
commit 8fbbf91270

View file

@ -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);
}