From 4f661128ba000a481af3920355760763dbedfa52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Tue, 14 Feb 2023 00:49:41 +0100 Subject: [PATCH] Do not load into memory files that are definitely not JSON files --- src/data/geojsonparser.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/data/geojsonparser.cpp b/src/data/geojsonparser.cpp index 9fde2172..d1172c64 100644 --- a/src/data/geojsonparser.cpp +++ b/src/data/geojsonparser.cpp @@ -104,6 +104,26 @@ static void setWaypointProperties(Waypoint &waypoint, waypoint.setStyle(PointStyle(color, size)); } +static bool isWS(char c) +{ + return (c == 0x20 || c == 0x09 || c == 0x0A || c == 0x0D) ? true : false; +} + +static bool isJSONObject(QFile *file) +{ + char c; + + while (file->getChar(&c)) { + if (isWS(c)) + continue; + else if (c == '{') + return true; + else + return false; + } + + return false; +} GeoJSONParser::Type GeoJSONParser::type(const QJsonObject &json) { @@ -404,6 +424,13 @@ bool GeoJSONParser::parse(QFile *file, QList &tracks, QList &routes, QList &areas, QVector &waypoints) { Q_UNUSED(routes); + + if (!isJSONObject(file)) { + _errorString = "Not a GeoJSON file"; + return false; + } else + file->reset(); + QJsonParseError error; QJsonDocument doc(QJsonDocument::fromJson(file->readAll(), &error));