Skip to content

Commit

Permalink
[BUGFIX] prevent crash in getCenter, check all loaded sectors (#257)
Browse files Browse the repository at this point in the history
This checks all loaded sectors for having at least 3 points and prevents
a potential crash in getCenter().
  • Loading branch information
jonaseberle committed May 29, 2023
1 parent 1c89de4 commit 106711b
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 20 deletions.
3 changes: 3 additions & 0 deletions data/_notes.txt
Expand Up @@ -83,6 +83,9 @@ DISPLAY_LIST_186
43.99:27.95
(etc)

The last line in the file must be:
DISPLAY_LIST_

To create new shapes, you can use
the sconv.exe in from Dolomynum \tools that can convert sector files of ATC
clients (IvAc, ASRC, VRC, ProController), or Servinfo / IvAe data files to
Expand Down
2 changes: 1 addition & 1 deletion data/dataversions.txt
Expand Up @@ -4,5 +4,5 @@ coastline.dat%%118
controllerAirportsMapping.dat%%6
countries.dat%%120
countrycodes.dat%%119
firdisplay.dat%%331
firdisplay.dat%%332
firlist.dat%%337
1 change: 1 addition & 0 deletions data/firdisplay.dat
Expand Up @@ -48794,3 +48794,4 @@ DISPLAY_LIST_3204
20.74:-85.35
22:-86
24:-86
DISPLAY_LIST_
6 changes: 4 additions & 2 deletions src/Controller.cpp
Expand Up @@ -72,8 +72,10 @@ Controller::Controller(const QJsonObject& json, const WhazzupData* whazzup):
if (sector != 0) {
// We determine lat/lon from the sector
QPair<double, double> center = this->sector->getCenter();
lat = center.first;
lon = center.second;
if (center.first > -180.) {
lat = center.first;
lon = center.second;
}

break;
}
Expand Down
10 changes: 6 additions & 4 deletions src/GLWidget.cpp
Expand Up @@ -1404,10 +1404,12 @@ void GLWidget::renderLabels() {
qglColor(Settings::firFontColor());
foreach (const Sector *sector, NavData::instance()->sectors) {
QPair<double, double> center = sector->getCenter();
double lat = center.first;
double lon = center.second;
renderText(SX(lat, lon), SY(lat, lon),
SZ(lat, lon), sector->icao, Settings::firFont());
if (center.first > -180.) {
double lat = center.first;
double lon = center.second;
renderText(SX(lat, lon), SY(lat, lon),
SZ(lat, lon), sector->icao, Settings::firFont());
}
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/SectorReader.cpp
Expand Up @@ -60,6 +60,16 @@ void SectorReader::loadSectordisplay(QMultiMap<QString, Sector*>& sectors) {

if(line.startsWith("DISPLAY_LIST_")) {
if(!workingSectorId.isEmpty()) {
if(pointList.size() < 3) {
auto msg = QString("While processing line #%1 from %2: Sector %3 doesn't contain enough points (%4, expected 3+)")
.arg(count)
.arg(filePath, workingSectorId)
.arg(pointList.size());
qCritical() << "SectorReader::loadSectordisplay()" << msg;
QTextStream(stdout) << "CRITICAL: " << msg << Qt::endl;
exit(EXIT_FAILURE);
}

QList<Sector*> sectorsWithMatchingId;
foreach(auto *sector, sectors) {
if (sector->id == workingSectorId) {
Expand All @@ -78,16 +88,6 @@ void SectorReader::loadSectordisplay(QMultiMap<QString, Sector*>& sectors) {

foreach(auto sector, sectorsWithMatchingId) {
if(sector != 0) {
if(pointList.size() < 3) {
auto msg = QString("While processing line #%1 '%2' from %3: Sector %4 (%5) doesn't contain enough points (%3)")
.arg(count)
.arg(line, filePath)
.arg(sector->name, sector->icao).arg(pointList.size());
qCritical() << "Sector::getCenter()" << msg;
QTextStream(stdout) << "CRITICAL: " << msg << Qt::endl;
exit(EXIT_FAILURE);
}

sector->setPoints(pointList);
}
}
Expand All @@ -104,7 +104,7 @@ void SectorReader::loadSectordisplay(QMultiMap<QString, Sector*>& sectors) {
.arg(count)
.arg(line, filePath)
.arg(workingSectorId).arg(lat).arg(lon);
qCritical() << "Sector::getCenter()" << msg;
qCritical() << "SectorReader::loadSectordisplay()" << msg;
QTextStream(stdout) << "CRITICAL: " << msg << Qt::endl;
exit(EXIT_FAILURE);
}
Expand Down
10 changes: 8 additions & 2 deletions src/helpers.h
Expand Up @@ -38,9 +38,16 @@ class Helpers {
b.second += ((diff > 0) - (diff < 0)) * 360;
}

/*
* @return (-360., -360.) on error
*/
static DoublePair polygonCenter(const QList<DoublePair> points) {
// https://en.wikipedia.org/wiki/Centroid#Of_a_polygon

if (points.size() == 0) {
return DoublePair(-360., -360.);
}

double A = 0;
DoublePair runningTotal;
runningTotal.first = 0;
Expand All @@ -52,8 +59,7 @@ class Helpers {
for(int i = 0; i < count; ++i) {
DoublePair current = points[i];
DoublePair next = points[(i + 1) % count];
if(i > 0)
adjustPoint(previous, current);
if(i > 0) adjustPoint(previous, current);
adjustPoint(current, next);
previous = current;

Expand Down

0 comments on commit 106711b

Please sign in to comment.