From 1fd5502e9b61aaff1b25e375123c0a2c0ea77ea4 Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 4 Sep 2020 10:56:18 +0100 Subject: [PATCH 1/4] Nasal removecommand: use a return value Change removecommand() to indicate success or failure via a return value: 1 for success, 0 for failure. --- src/Scripting/NasalSys.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Scripting/NasalSys.cxx b/src/Scripting/NasalSys.cxx index 5af03c492..b6eb6f7f5 100644 --- a/src/Scripting/NasalSys.cxx +++ b/src/Scripting/NasalSys.cxx @@ -838,9 +838,9 @@ static naRef f_removeCommand(naContext c, naRef me, int argc, naRef* args) const string commandName(naStr_data(args[0])); bool ok = nasalSys->removeCommand(commandName); if (!ok) { - naRuntimeError(c, "Failed to remove command:%s", commandName.c_str()); + return naNum(0); } - return naNil(); + return naNum(1); } static naRef f_open(naContext c, naRef me, int argc, naRef* args) From 4402d7b81d0a0183e569da995dc4bcb9bfd42619 Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 4 Sep 2020 12:29:45 +0100 Subject: [PATCH 2/4] NavCache: only remove if the file exists. Otherwise we block trying to re-create the cache, which is dumb. This showed up as Sentry issue: https://sentry.io/organizations/flightgear/issues/1875854826 Will back-port to 2020.2 once verified. --- src/Navaids/NavDataCache.cxx | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Navaids/NavDataCache.cxx b/src/Navaids/NavDataCache.cxx index c6b93f635..f45bee087 100644 --- a/src/Navaids/NavDataCache.cxx +++ b/src/Navaids/NavDataCache.cxx @@ -1212,14 +1212,13 @@ NavDataCache::NavDataCache() os << "navdata_" << versionParts[0] << "_" << versionParts[1] << ".cache"; } - homePath.append(os.str()); - // permit additional DB connections from the same process sqlite3_config(SQLITE_CONFIG_MULTITHREAD); for (int t=0; t < MAX_TRIES; ++t) { + SGPath cachePath = homePath / os.str(); try { - d.reset(new NavDataCachePrivate(homePath, this)); + d.reset(new NavDataCachePrivate(cachePath, this)); d->init(); //d->checkCacheFile(); // reached this point with no exception, success @@ -1239,14 +1238,14 @@ NavDataCache::NavDataCache() d.reset(); // only wipe the existing if not readonly - if (!fgGetBool("/sim/fghome-readonly", false)) { - bool ok = homePath.remove(); + if (cachePath.exists() && !fgGetBool("/sim/fghome-readonly", false)) { + bool ok = cachePath.remove(); if (!ok) { SG_LOG(SG_NAVCACHE, SG_ALERT, "NavCache: failed to remove previous cache file"); flightgear::fatalMessageBoxThenExit( "Unable to re-create navigation cache", "Attempting to remove the old cache failed.", - "Location: " + homePath.utf8Str()); + "Location: " + cachePath.utf8Str()); } } } From d2fb16e071f7fd627bcee9f1f63e460d5caaf849 Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 4 Sep 2020 12:41:56 +0100 Subject: [PATCH 3/4] Fix mismatched new/strdup/delete[] froub by ASan See: https://sourceforge.net/p/flightgear/codetickets/2367/ for the issue. Switch to using std::string instead of char*, and hence any need to manually free the memory. --- src/FDM/YASim/FGFDM.cpp | 8 +++----- src/FDM/YASim/FGFDM.hpp | 4 ++-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/FDM/YASim/FGFDM.cpp b/src/FDM/YASim/FGFDM.cpp index 63fb20b95..f191fc316 100644 --- a/src/FDM/YASim/FGFDM.cpp +++ b/src/FDM/YASim/FGFDM.cpp @@ -47,14 +47,12 @@ FGFDM::~FGFDM() { for(int i=0; i<_thrusters.size(); i++) { EngRec* er = (EngRec*)_thrusters.get(i); - delete[] er->prefix; delete er->eng; delete er; } for(int i=0; i<_weights.size(); i++) { WeightRec* wr = (WeightRec*)_weights.get(i); - delete[] wr->prop; delete wr; } @@ -365,7 +363,7 @@ void FGFDM::getExternalInput(float dt) if(t->getPropEngine()) { PropEngine* p = t->getPropEngine(); - sprintf(buf, "%s/rpm", er->prefix); + sprintf(buf, "%s/rpm", er->prefix.c_str()); p->setOmega(fgGetFloat(buf, 500) * RPM2RAD); } } @@ -373,7 +371,7 @@ void FGFDM::getExternalInput(float dt) // Linearly "seeks" a property by the specified fraction of the way to // the target value. Used to emulate "slowly changing" output values. -static void moveprop(SGPropertyNode* node, const char* prop, +static void moveprop(SGPropertyNode* node, const std::string& prop, float target, float frac) { float val = node->getFloatValue(prop); @@ -1135,7 +1133,7 @@ void FGFDM::parseWeight(const XMLAttributes* a) float v[3]; attrf_xyz(a, v); - wr->prop = strdup(a->getValue("mass-prop")); + wr->prop = std::string{a->getValue("mass-prop")}; wr->size = attrf(a, "size", 0); wr->handle = _airplane.addWeight(v, wr->size); _weights.add(wr); diff --git a/src/FDM/YASim/FGFDM.hpp b/src/FDM/YASim/FGFDM.hpp index 17ea3d8a9..30663721a 100644 --- a/src/FDM/YASim/FGFDM.hpp +++ b/src/FDM/YASim/FGFDM.hpp @@ -32,11 +32,11 @@ public: private: struct EngRec { - char* prefix {nullptr}; + std::string prefix; Thruster* eng {nullptr}; }; struct WeightRec { - char* prop {nullptr}; + std::string prop; float size {0}; int handle {0}; }; From 2147eb8da9eed928478fcb06cd307b35be4a584b Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 4 Sep 2020 13:05:11 +0100 Subject: [PATCH 4/4] Missed fixes from previous Yasim commit. --- src/FDM/YASim/FGFDM.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/FDM/YASim/FGFDM.cpp b/src/FDM/YASim/FGFDM.cpp index f191fc316..2fbff83f8 100644 --- a/src/FDM/YASim/FGFDM.cpp +++ b/src/FDM/YASim/FGFDM.cpp @@ -873,7 +873,7 @@ void FGFDM::parsePropeller(const XMLAttributes* a) sprintf(buf, "/engines/engine[%d]", _nextEngine++); EngRec* er = new EngRec(); er->eng = thruster; - er->prefix = strdup(buf); + er->prefix = buf; _thrusters.add(er); _currObj = thruster; @@ -931,7 +931,7 @@ void FGFDM::parseJet(const XMLAttributes* a) sprintf(buf, "/engines/engine[%d]", _nextEngine++); EngRec* er = new EngRec(); er->eng = j; - er->prefix = strdup(buf); + er->prefix = buf; _thrusters.add(er); }