From 94b81d419e12162e040c45b1fbb701127225990a Mon Sep 17 00:00:00 2001 From: Bruno de Lacheisserie Date: Sun, 29 Sep 2024 23:53:06 +0200 Subject: [PATCH] LocalPath : update to fix possible string overflow --- Common/Header/Utils.h | 11 -- Common/Source/AirfieldDetails.cpp | 1 + Common/Source/Airspace/LKAirspace.cpp | 1 + Common/Source/Bitmaps.cpp | 1 + Common/Source/Calc/DoRecent.cpp | 1 + Common/Source/Comm/FilePort.cpp | 1 + Common/Source/Devices/DeviceSettings.cpp | 1 + Common/Source/Devices/devLXNano3.cpp | 1 + Common/Source/Dialogs/dlgChecklist.cpp | 1 + Common/Source/Dialogs/dlgEOSIGCDownload.cpp | 7 +- Common/Source/Dialogs/dlgFlarmIGCDownload.cpp | 7 +- Common/Source/Dialogs/dlgIgcFile.cpp | 1 + Common/Source/Dialogs/dlgLXIGCDownload.cpp | 6 +- Common/Source/Dialogs/dlgProfiles.cpp | 1 + Common/Source/Dialogs/dlgStartup.cpp | 1 + Common/Source/Dialogs/dlgTaskOverview.cpp | 1 + Common/Source/Draw/LoadSplash.cpp | 1 + Common/Source/FlarmIdFile.cpp | 1 + Common/Source/FlarmTools.cpp | 1 + Common/Source/InputEvents.cpp | 1 + Common/Source/LKInstall.cpp | 1 + Common/Source/LKLanguage.cpp | 1 + Common/Source/LKProfileLoad.cpp | 1 + Common/Source/LKUtils.cpp | 1 + Common/Source/Library/DirectoryFunctions.cpp | 1 + Common/Source/LocalPath.cpp | 155 ++++++++++++------ Common/Source/LocalPath.h | 36 ++++ Common/Source/Logger/FlightDataRec.cpp | 1 + Common/Source/Logger/LogBook.cpp | 1 + Common/Source/Logger/Logger.cpp | 1 + Common/Source/Logger/NMEAlogger.cpp | 1 + Common/Source/Logger/ReplayLogger.cpp | 1 + Common/Source/MapDraw/MarkLocation.cpp | 1 + Common/Source/MapDraw/OpenCloseTopology.cpp | 2 +- Common/Source/MessageLog.cpp | 1 + Common/Source/Polar.cpp | 1 + .../Source/SaveLoadTask/CTaskFileHelper.cpp | 1 + .../Source/SaveLoadTask/SaveDefaultTask.cpp | 1 + Common/Source/Sound/Win32/Sound.cpp | 11 +- Common/Source/Sound/alsa/Sound.cpp | 1 + Common/Source/Terrain/OpenCreateClose.cpp | 1 + Common/Source/Utils.cpp | 1 + Common/Source/Waypoints/Read.cpp | 1 + Common/Source/Waypoints/Write.cpp | 1 + Common/Source/WindowControls.cpp | 1 + Common/Source/lk8000.cpp | 1 + 46 files changed, 196 insertions(+), 77 deletions(-) create mode 100644 Common/Source/LocalPath.h diff --git a/Common/Header/Utils.h b/Common/Header/Utils.h index 48035cedb..5f48caf56 100755 --- a/Common/Header/Utils.h +++ b/Common/Header/Utils.h @@ -43,17 +43,6 @@ void TrimRight(TCHAR* str); // Parse string (new lines etc) and malloc the string TCHAR* StringMallocParse(const TCHAR* old_string); -void LocalPath(TCHAR* buf, const TCHAR* file = TEXT("")) gcc_nonnull_all; -void LocalPath(TCHAR* buffer, const TCHAR* SubPath, const TCHAR* file) gcc_nonnull_all; - -void SystemPath(TCHAR* buf, const TCHAR* file = TEXT("")) gcc_nonnull_all; -void SystemPath(TCHAR* buffer, const TCHAR* SubPath, const TCHAR* file) gcc_nonnull_all; - -void RemoveFilePathPrefix(const TCHAR* szPrefix, TCHAR* szFilePath) gcc_nonnull_all; - -const TCHAR *LKGetLocalPath(); -const TCHAR *LKGetSystemPath(); - void LK_tsplitpath(const TCHAR* path, TCHAR* drv, TCHAR* dir, TCHAR* name, TCHAR* ext); void propGetFontSettingsFromString(const TCHAR *Buffer, LOGFONT* lplf); diff --git a/Common/Source/AirfieldDetails.cpp b/Common/Source/AirfieldDetails.cpp index cab2c7d54..570583025 100644 --- a/Common/Source/AirfieldDetails.cpp +++ b/Common/Source/AirfieldDetails.cpp @@ -12,6 +12,7 @@ #include "AirfieldDetails.h" #include "Waypointparser.h" #include "utils/zzip_stream.h" +#include "LocalPath.h" void LookupAirfieldDetail(TCHAR *Name, TCHAR *Details) { TCHAR UName[NAME_SIZE + 1]; diff --git a/Common/Source/Airspace/LKAirspace.cpp b/Common/Source/Airspace/LKAirspace.cpp index f4578a69c..703dd10ef 100755 --- a/Common/Source/Airspace/LKAirspace.cpp +++ b/Common/Source/Airspace/LKAirspace.cpp @@ -33,6 +33,7 @@ #include "Library/TimeFunctions.h" #include "Baro.h" #include "utils/lookup_table.h" +#include "LocalPath.h" using xml_document = rapidxml::xml_document; using xml_attribute = rapidxml::xml_attribute; diff --git a/Common/Source/Bitmaps.cpp b/Common/Source/Bitmaps.cpp index 0f23faa5b..594f2b0ce 100755 --- a/Common/Source/Bitmaps.cpp +++ b/Common/Source/Bitmaps.cpp @@ -15,6 +15,7 @@ #include "Bitmaps.h" #include "resource_data.h" #include "Asset.hpp" +#include "LocalPath.h" using std::placeholders::_1; diff --git a/Common/Source/Calc/DoRecent.cpp b/Common/Source/Calc/DoRecent.cpp index 1a4060009..ea30b1a2a 100644 --- a/Common/Source/Calc/DoRecent.cpp +++ b/Common/Source/Calc/DoRecent.cpp @@ -7,6 +7,7 @@ */ #include "externs.h" +#include "LocalPath.h" namespace { diff --git a/Common/Source/Comm/FilePort.cpp b/Common/Source/Comm/FilePort.cpp index 999a35a52..1a03a7165 100644 --- a/Common/Source/Comm/FilePort.cpp +++ b/Common/Source/Comm/FilePort.cpp @@ -13,6 +13,7 @@ #include #include "utils/stringext.h" #include "OS/Sleep.h" +#include "LocalPath.h" using namespace std::placeholders; diff --git a/Common/Source/Devices/DeviceSettings.cpp b/Common/Source/Devices/DeviceSettings.cpp index 0a63925c5..de698df86 100644 --- a/Common/Source/Devices/DeviceSettings.cpp +++ b/Common/Source/Devices/DeviceSettings.cpp @@ -11,6 +11,7 @@ #include "externs.h" #include "Utils.h" #include +#include "LocalPath.h" namespace { diff --git a/Common/Source/Devices/devLXNano3.cpp b/Common/Source/Devices/devLXNano3.cpp index f01669f2c..88bacab8f 100644 --- a/Common/Source/Devices/devLXNano3.cpp +++ b/Common/Source/Devices/devLXNano3.cpp @@ -36,6 +36,7 @@ #include "utils/printf.h" #include "Comm/UpdateQNH.h" #include "Comm/ExternalWind.h" +#include "LocalPath.h" #define NANO_PROGRESS_DLG #define BLOCK_SIZE 32 diff --git a/Common/Source/Dialogs/dlgChecklist.cpp b/Common/Source/Dialogs/dlgChecklist.cpp index a9adb113a..686990932 100644 --- a/Common/Source/Dialogs/dlgChecklist.cpp +++ b/Common/Source/Dialogs/dlgChecklist.cpp @@ -16,6 +16,7 @@ #include "resource.h" #include "utils/zzip_stream.h" #include +#include "LocalPath.h" #define MAXNOTETITLE 200 // max number of characters in a title note diff --git a/Common/Source/Dialogs/dlgEOSIGCDownload.cpp b/Common/Source/Dialogs/dlgEOSIGCDownload.cpp index a4a60847b..927a7ef41 100644 --- a/Common/Source/Dialogs/dlgEOSIGCDownload.cpp +++ b/Common/Source/Dialogs/dlgEOSIGCDownload.cpp @@ -17,6 +17,7 @@ #include "dlgIGCProgress.h" #include "utils/tokenizer.h" #include "utils/printf.h" +#include "LocalPath.h" #define EOS_PRPGRESS_DLG @@ -201,10 +202,8 @@ static void OnMultiSelectListListInfo(WndListFrame *Sender, WndListFrame::ListIn } } - -bool GetEOSIGCFilename(TCHAR *IGCFilename, TCHAR *InFilename) { - if (IGCFilename == NULL) - return false; +static +bool GetEOSIGCFilename(TCHAR (&IGCFilename)[MAX_PATH], TCHAR *InFilename) { TCHAR Tmp[MAX_PATH]; _tcscpy(Tmp, InFilename); diff --git a/Common/Source/Dialogs/dlgFlarmIGCDownload.cpp b/Common/Source/Dialogs/dlgFlarmIGCDownload.cpp index e00ebed9f..be0b31ce9 100644 --- a/Common/Source/Dialogs/dlgFlarmIGCDownload.cpp +++ b/Common/Source/Dialogs/dlgFlarmIGCDownload.cpp @@ -17,6 +17,7 @@ #include "dlgFlarmIGCDownload.h" #include "utils/tokenizer.h" #include "utils/printf.h" +#include "LocalPath.h" #define MAX_FLARM_ANSWER_LEN 640 // FLARM Docu does not tell the max. answer len @@ -367,11 +368,9 @@ static void OnMultiSelectListListInfo(WndListFrame *Sender, } } -bool GetIGCFilename(TCHAR *IGCFilename, int Idx) { - if (IGCFilename == NULL) - return false; +static +bool GetIGCFilename(TCHAR (&IGCFilename)[MAX_PATH], int Idx) { TCHAR Tmp[MAX_PATH]; - _tcscpy(Tmp, IGCFileList.at(Idx).Line1); TCHAR *Filename = lk::tokenizer(Tmp).Next(TEXT(" ")); if(Filename) { diff --git a/Common/Source/Dialogs/dlgIgcFile.cpp b/Common/Source/Dialogs/dlgIgcFile.cpp index 34f1b5eb0..23bd61e88 100644 --- a/Common/Source/Dialogs/dlgIgcFile.cpp +++ b/Common/Source/Dialogs/dlgIgcFile.cpp @@ -17,6 +17,7 @@ #include #include "utils/stl_utils.h" #include "resource.h" +#include "LocalPath.h" #ifdef ANDROID #include "Android/LK8000Activity.h" diff --git a/Common/Source/Dialogs/dlgLXIGCDownload.cpp b/Common/Source/Dialogs/dlgLXIGCDownload.cpp index c37c432c6..23dbd49a1 100644 --- a/Common/Source/Dialogs/dlgLXIGCDownload.cpp +++ b/Common/Source/Dialogs/dlgLXIGCDownload.cpp @@ -15,6 +15,7 @@ #include "dlgLXIGCDownload.h" #include "utils/tokenizer.h" #include "utils/printf.h" +#include "LocalPath.h" #define LST_STRG_LEN 100 @@ -167,9 +168,8 @@ static void OnMultiSelectListListInfo(WndListFrame *Sender, WndListFrame::ListIn } -bool GetLXIGCFilename(TCHAR *IGCFilename, TCHAR *InFilename) { - if (IGCFilename == NULL) - return false; +static +bool GetLXIGCFilename(TCHAR (&IGCFilename)[MAX_PATH], TCHAR *InFilename) { TCHAR Tmp[MAX_PATH]; _tcscpy(Tmp, InFilename); diff --git a/Common/Source/Dialogs/dlgProfiles.cpp b/Common/Source/Dialogs/dlgProfiles.cpp index 8e3fcc61a..ed0ed5124 100644 --- a/Common/Source/Dialogs/dlgProfiles.cpp +++ b/Common/Source/Dialogs/dlgProfiles.cpp @@ -13,6 +13,7 @@ #include "WindowControls.h" #include "resource.h" #include "utils/printf.h" +#include "LocalPath.h" static WndForm *wf=NULL; static short profilemode=0; diff --git a/Common/Source/Dialogs/dlgStartup.cpp b/Common/Source/Dialogs/dlgStartup.cpp index a1efd4f07..a43e07ec7 100644 --- a/Common/Source/Dialogs/dlgStartup.cpp +++ b/Common/Source/Dialogs/dlgStartup.cpp @@ -22,6 +22,7 @@ #include "InfoBoxLayout.h" #include "LKInterface.h" #include "utils/printf.h" +#include "LocalPath.h" #ifdef KOBO #include "Kobo/Kernel.hpp" diff --git a/Common/Source/Dialogs/dlgTaskOverview.cpp b/Common/Source/Dialogs/dlgTaskOverview.cpp index 93214ba98..3a1e9c0b3 100644 --- a/Common/Source/Dialogs/dlgTaskOverview.cpp +++ b/Common/Source/Dialogs/dlgTaskOverview.cpp @@ -17,6 +17,7 @@ #include "resource.h" #include "utils/printf.h" #include "Waypoints/SetHome.h" +#include "LocalPath.h" #ifdef ANDROID #include "Android/LK8000Activity.h" diff --git a/Common/Source/Draw/LoadSplash.cpp b/Common/Source/Draw/LoadSplash.cpp index 9d9cf3910..4da27f774 100644 --- a/Common/Source/Draw/LoadSplash.cpp +++ b/Common/Source/Draw/LoadSplash.cpp @@ -9,6 +9,7 @@ #include "externs.h" #include "resource.h" #include "ScreenGeometry.h" +#include "LocalPath.h" #ifdef USE_GDI #define IMG_EXT "BMP" diff --git a/Common/Source/FlarmIdFile.cpp b/Common/Source/FlarmIdFile.cpp index 50e648aab..cf78433c9 100644 --- a/Common/Source/FlarmIdFile.cpp +++ b/Common/Source/FlarmIdFile.cpp @@ -10,6 +10,7 @@ #include "utils/zzip_stream.h" #include "utils/charset_helper.h" #include +#include "LocalPath.h" namespace { diff --git a/Common/Source/FlarmTools.cpp b/Common/Source/FlarmTools.cpp index 0ed9f77e0..07aa4d553 100644 --- a/Common/Source/FlarmTools.cpp +++ b/Common/Source/FlarmTools.cpp @@ -11,6 +11,7 @@ #include "utils/zzip_stream.h" #include #include +#include "LocalPath.h" namespace { diff --git a/Common/Source/InputEvents.cpp b/Common/Source/InputEvents.cpp index 5cf2e6d0f..26d7b608d 100755 --- a/Common/Source/InputEvents.cpp +++ b/Common/Source/InputEvents.cpp @@ -39,6 +39,7 @@ #include "utils/lookup_table.h" #include #include "Waypoints/SetHome.h" +#include "LocalPath.h" // uncomment for show all menu button with id as Label. //#define TEST_MENU_LAYOUT diff --git a/Common/Source/LKInstall.cpp b/Common/Source/LKInstall.cpp index ff373c70a..2afedce7f 100644 --- a/Common/Source/LKInstall.cpp +++ b/Common/Source/LKInstall.cpp @@ -16,6 +16,7 @@ #endif #endif #include "utils/openzip.h" +#include "LocalPath.h" diff --git a/Common/Source/LKLanguage.cpp b/Common/Source/LKLanguage.cpp index 4455987bf..8ec9b745e 100644 --- a/Common/Source/LKLanguage.cpp +++ b/Common/Source/LKLanguage.cpp @@ -11,6 +11,7 @@ #include "utils/zzip_stream.h" #include "picojson.h" #include +#include "LocalPath.h" #ifdef ANDROID #include "Android/Main.hpp" #include "Android/NativeView.hpp" diff --git a/Common/Source/LKProfileLoad.cpp b/Common/Source/LKProfileLoad.cpp index de4744c39..9375ba270 100755 --- a/Common/Source/LKProfileLoad.cpp +++ b/Common/Source/LKProfileLoad.cpp @@ -19,6 +19,7 @@ #include #include "utils/printf.h" #include "utils/unique_file_ptr.h" +#include "LocalPath.h" #ifdef ANDROID #include "Android/BluetoothHelper.hpp" diff --git a/Common/Source/LKUtils.cpp b/Common/Source/LKUtils.cpp index 31e24fe0d..aa94b0945 100644 --- a/Common/Source/LKUtils.cpp +++ b/Common/Source/LKUtils.cpp @@ -16,6 +16,7 @@ #include "Library/TimeFunctions.h" #include "Baro.h" #include "InputEvents.h" +#include "LocalPath.h" extern TCHAR LastTaskFileName[MAX_PATH]; diff --git a/Common/Source/Library/DirectoryFunctions.cpp b/Common/Source/Library/DirectoryFunctions.cpp index 1ea70e9ba..1d4d91794 100644 --- a/Common/Source/Library/DirectoryFunctions.cpp +++ b/Common/Source/Library/DirectoryFunctions.cpp @@ -7,6 +7,7 @@ */ #include "externs.h" +#include "LocalPath.h" void CreateDirectoryIfAbsent(const TCHAR *filename) { TCHAR fullname[MAX_PATH]; diff --git a/Common/Source/LocalPath.cpp b/Common/Source/LocalPath.cpp index aca140ac4..69f7a916e 100644 --- a/Common/Source/LocalPath.cpp +++ b/Common/Source/LocalPath.cpp @@ -7,8 +7,10 @@ */ #include "externs.h" +#include "LocalPath.h" #include "DoInits.h" #include "Asset.hpp" +#include "utils/printf.h" #ifdef ANDROID @@ -148,8 +150,8 @@ BOOL GetFontPath(TCHAR *pPos) pPos[0]= '\0'; hRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\Microsoft\\FontPath"), 0, KEY_READ /*KEY_ALL_ACCESS*/, &hKey); if (hRes != ERROR_SUCCESS) { - RegCloseKey(hKey); - return FALSE; + RegCloseKey(hKey); + return FALSE; } dwSize *= 2; // BUGFIX 100913 ?? to remove? check @@ -162,54 +164,62 @@ BOOL GetFontPath(TCHAR *pPos) } #endif -static gcc_nonnull_all -void GetPath(TCHAR* buffer, const TCHAR* subpath, const TCHAR* file, const TCHAR* lkPath) { - // remove leading directory separator from file. - const TCHAR* pfile = file; - while( (*pfile) == _T('\\') && (*pfile) == _T('/') ) { - ++pfile; - } +namespace { + +bool IsDirectorySeparator(TCHAR c) { + return (c == _T('\\')) || (c == _T('/')); +} - // remove leading directory separator from subpath. - const TCHAR* psub = subpath; - while( (*psub) == _T('\\') || (*psub) == _T('/')) { - ++psub; +const TCHAR* RemoveLeadingSeparator(const TCHAR* path) { + while (IsDirectorySeparator(*path)) { + ++path; } + return path; +} - _tcscpy(buffer,lkPath); +} // namespace - if( _tcslen(psub) > 0) { - _tcscat(buffer,psub); - // remove trailing directory separator; - size_t len = _tcslen(buffer); - while(len > 1 && ( buffer[len-1] == _T('\\') || buffer[len-1] == _T('/'))) { - buffer[--len] = _T('\0'); - } - // add right (platform depends) trailing separator - _tcscat(buffer, _T(DIRSEP)); +static gcc_nonnull_all +void GetPath(TCHAR* buffer, size_t size, const TCHAR* subpath, const TCHAR* file, const TCHAR* lkPath) { + // remove leading directory separator from file and subpath. + const TCHAR* pfile = RemoveLeadingSeparator(file); + const TCHAR* psub = RemoveLeadingSeparator(subpath); + + size_t len = lk::snprintf(buffer, size, _T("%s"), lkPath); + while (IsDirectorySeparator(buffer[len - 1])) { + --len; } + len += lk::snprintf(buffer + len, size - len, _T(DIRSEP)); - if (_tcslen(pfile)>0) { - _tcscat(buffer,pfile); + if (psub[0] != _T('\0')) { + len += lk::snprintf(buffer + len, size - len, _T("%s"), psub); + while (IsDirectorySeparator(buffer[len - 1])) { + --len; + } + len += lk::snprintf(buffer + len, size - len, _T(DIRSEP)); + } + + if (pfile[0] != _T('\0')) { + len += lk::snprintf(buffer + len, size - len, _T("%s"), pfile); } lk::filesystem::fixPath(buffer); } -void SystemPath(TCHAR* buffer, const TCHAR* SubPath, const TCHAR* file) { - GetPath(buffer, SubPath, file, LKGetSystemPath()); +void SystemPath(TCHAR* buffer, size_t size, const TCHAR* SubPath, const TCHAR* file) { + GetPath(buffer, size, SubPath, file, LKGetSystemPath()); } -void LocalPath(TCHAR* buffer, const TCHAR* SubPath, const TCHAR* file) { - GetPath(buffer, SubPath, file, LKGetLocalPath()); +void LocalPath(TCHAR* buffer, size_t size, const TCHAR* SubPath, const TCHAR* file) { + GetPath(buffer, size, SubPath, file, LKGetLocalPath()); } -void SystemPath(TCHAR* buffer, const TCHAR* file) { - GetPath(buffer, _T(""), file, LKGetSystemPath()); +void SystemPath(TCHAR* buffer, size_t size, const TCHAR* file) { + GetPath(buffer, size, _T(""), file, LKGetSystemPath()); } -void LocalPath(TCHAR* buffer, const TCHAR* file) { - GetPath(buffer, _T(""), file, LKGetLocalPath()); +void LocalPath(TCHAR* buffer, size_t size, const TCHAR* file) { + GetPath(buffer, size, _T(""), file, LKGetLocalPath()); } /** @@ -220,20 +230,69 @@ void LocalPath(TCHAR* buffer, const TCHAR* file) { * required for compatibility with old config file */ void RemoveFilePathPrefix(const TCHAR* szPrefix, TCHAR* szFilePath) { - const TCHAR* ptr = _tcsstr(szFilePath, szPrefix); - if(ptr && szFilePath == ptr) { - ptr += _tcslen(szPrefix); - } else { - ptr = nullptr; - } + const TCHAR* ptr = _tcsstr(szFilePath, szPrefix); + if(ptr && szFilePath == ptr) { + ptr += _tcslen(szPrefix); + } else { + ptr = nullptr; + } - // remove prefix only if followed by directory separator - bool found = false; - while (ptr && ((*ptr) == '\\' || (*ptr) == '/')) { - ++ptr; - found = true; - } - if(found && ptr) { - _tcscpy(szFilePath, ptr); - } + // remove prefix only if followed by directory separator + bool found = false; + + while (ptr && IsDirectorySeparator(*ptr)) { + ++ptr; + found = true; + } + if(found && ptr) { + // memmove is required for overlapping src and dst + std::memmove(szFilePath, ptr, (_tcslen(ptr) + 1) * sizeof(TCHAR)); + } } + +#ifndef DOCTEST_CONFIG_DISABLE +#include +#include "Util/tstring.hpp" + +TEST_CASE("LocalPath") { + + SUBCASE("RemoveFilePathPrefix") { + TCHAR path[MAX_PATH] = { _T("prefix/file") }; + RemoveFilePathPrefix(_T("prefix"), path); + CHECK(tstring_view(_T("file")) == path); + } + + const tstring_view result(_T("path" DIRSEP "sub" DIRSEP "file")); + SUBCASE("GetPath test 1") { + TCHAR path[MAX_PATH]; + GetPath(path, MAX_PATH, _T("sub"), _T("file"), _T("path")); + CHECK(result == path); + } + SUBCASE("GetPath test 2") { + TCHAR path[MAX_PATH]; + GetPath(path, MAX_PATH, _T("/sub/"), _T("/file"), _T("path/")); + CHECK(result == path); + } + SUBCASE("GetPath test 3") { + TCHAR path[MAX_PATH]; + GetPath(path, MAX_PATH, _T("\\sub\\"), _T("\\file"), _T("path\\")); + CHECK(result == path); + } + SUBCASE("GetPath test 4") { + TCHAR path[MAX_PATH]; + GetPath(path, MAX_PATH, _T(""), _T("file"), _T("path")); + CHECK(tstring_view(_T("path" DIRSEP "file")) == path); + } + SUBCASE("GetPath test 5") { + TCHAR path[MAX_PATH]; + GetPath(path, MAX_PATH, _T("sub"), _T(""), _T("path")); + CHECK(tstring_view(_T("path" DIRSEP "sub" DIRSEP)) == path); + } + SUBCASE("GetPath test 6") { + TCHAR path[MAX_PATH]; + GetPath(path, MAX_PATH, _T(""), _T(""), _T("path")); + CHECK(tstring_view(_T("path" DIRSEP)) == path); + } +} + +#endif diff --git a/Common/Source/LocalPath.h b/Common/Source/LocalPath.h new file mode 100644 index 000000000..9ba8ee3b8 --- /dev/null +++ b/Common/Source/LocalPath.h @@ -0,0 +1,36 @@ +#include "Compiler.h" +#include "tchar.h" +#include + +const TCHAR *LKGetLocalPath(); +const TCHAR *LKGetSystemPath(); + +void LocalPath(TCHAR* buffer, size_t size, const TCHAR* file = _T("")) gcc_nonnull_all; + +template +void LocalPath(TCHAR (&buffer)[size], const TCHAR* file = _T("")) { + LocalPath(buffer, size, file); +} + +void LocalPath(TCHAR* buffer, size_t size, const TCHAR* SubPath, const TCHAR* file) gcc_nonnull_all; + +template +void LocalPath(TCHAR (&buffer)[size], const TCHAR* SubPath, const TCHAR* file) { + LocalPath(buffer, size, SubPath, file); +} + +void SystemPath(TCHAR* buffer, size_t size, const TCHAR* file = _T("")) gcc_nonnull_all; + +template +void SystemPath(TCHAR (&buffer)[size], const TCHAR* file = _T("")) { + SystemPath(buffer, size, file); +} + +void SystemPath(TCHAR* buffer, size_t size, const TCHAR* SubPath, const TCHAR* file) gcc_nonnull_all; + +template +void SystemPath(TCHAR (&buffer)[size], const TCHAR* SubPath, const TCHAR* file) { + SystemPath(buffer, size, SubPath, file); +} + +void RemoveFilePathPrefix(const TCHAR* szPrefix, TCHAR* szFilePath) gcc_nonnull_all; diff --git a/Common/Source/Logger/FlightDataRec.cpp b/Common/Source/Logger/FlightDataRec.cpp index a9a515c2d..d5bd381f0 100644 --- a/Common/Source/Logger/FlightDataRec.cpp +++ b/Common/Source/Logger/FlightDataRec.cpp @@ -10,6 +10,7 @@ #include "FlightDataRec.h" #include "utils/stringext.h" #include +#include "LocalPath.h" #define NO_ENTRYS 26 diff --git a/Common/Source/Logger/LogBook.cpp b/Common/Source/Logger/LogBook.cpp index 7e0be3d83..349225dba 100644 --- a/Common/Source/Logger/LogBook.cpp +++ b/Common/Source/Logger/LogBook.cpp @@ -12,6 +12,7 @@ #include "utils/fileext.h" #include "utils/printf.h" #include "Library/TimeFunctions.h" +#include "LocalPath.h" // // Called by Calculations at landing detection (not flying anymore) diff --git a/Common/Source/Logger/Logger.cpp b/Common/Source/Logger/Logger.cpp index 28a68c9bd..439b1ac22 100644 --- a/Common/Source/Logger/Logger.cpp +++ b/Common/Source/Logger/Logger.cpp @@ -22,6 +22,7 @@ #include #include #include "Baro.h" +#include "LocalPath.h" #ifdef ANDROID #include "Android/AndroidFileUtils.h" #endif diff --git a/Common/Source/Logger/NMEAlogger.cpp b/Common/Source/Logger/NMEAlogger.cpp index 88da8a1cb..d9d392618 100755 --- a/Common/Source/Logger/NMEAlogger.cpp +++ b/Common/Source/Logger/NMEAlogger.cpp @@ -9,6 +9,7 @@ #include "externs.h" #include "utils/printf.h" +#include "LocalPath.h" bool EnableLogNMEA = false; diff --git a/Common/Source/Logger/ReplayLogger.cpp b/Common/Source/Logger/ReplayLogger.cpp index 7cc237361..72789452a 100644 --- a/Common/Source/Logger/ReplayLogger.cpp +++ b/Common/Source/Logger/ReplayLogger.cpp @@ -12,6 +12,7 @@ #include "Dialogs.h" #include #include "NavFunctions.h" +#include "LocalPath.h" extern int NumLoggerBuffered; diff --git a/Common/Source/MapDraw/MarkLocation.cpp b/Common/Source/MapDraw/MarkLocation.cpp index 7102cb566..fffc8a895 100644 --- a/Common/Source/MapDraw/MarkLocation.cpp +++ b/Common/Source/MapDraw/MarkLocation.cpp @@ -12,6 +12,7 @@ #include "utils/stringext.h" #include "utils/printf.h" #include "Library/TimeFunctions.h" +#include "LocalPath.h" extern int GetVirtualWaypointMarkerSlot(void); diff --git a/Common/Source/MapDraw/OpenCloseTopology.cpp b/Common/Source/MapDraw/OpenCloseTopology.cpp index 5273ce6a2..7fc4ea3ce 100644 --- a/Common/Source/MapDraw/OpenCloseTopology.cpp +++ b/Common/Source/MapDraw/OpenCloseTopology.cpp @@ -13,7 +13,7 @@ #include "LKProfiles.h" #include "Dialogs/dlgProgress.h" #include "utils/zzip_stream.h" - +#include "LocalPath.h" extern Topology* TopoStore[MAXTOPOLOGY]; diff --git a/Common/Source/MessageLog.cpp b/Common/Source/MessageLog.cpp index db73ae8e7..c56ab68eb 100644 --- a/Common/Source/MessageLog.cpp +++ b/Common/Source/MessageLog.cpp @@ -12,6 +12,7 @@ #include "utils/stringext.h" #include "utils/unique_file_ptr.h" #include "OS/Memory.h" +#include "LocalPath.h" #ifdef ANDROID #include diff --git a/Common/Source/Polar.cpp b/Common/Source/Polar.cpp index 916bd5ea6..67619df6a 100644 --- a/Common/Source/Polar.cpp +++ b/Common/Source/Polar.cpp @@ -12,6 +12,7 @@ #include "Dialogs.h" #include "utils/zzip_stream.h" #include "Utils.h" +#include "LocalPath.h" // This is calculating the weight difference for the chosen wingloading // Remember that polar files have a weight indicated that includes the pilot.. diff --git a/Common/Source/SaveLoadTask/CTaskFileHelper.cpp b/Common/Source/SaveLoadTask/CTaskFileHelper.cpp index 8c19026a1..8c33502fa 100644 --- a/Common/Source/SaveLoadTask/CTaskFileHelper.cpp +++ b/Common/Source/SaveLoadTask/CTaskFileHelper.cpp @@ -18,6 +18,7 @@ #include "Waypointparser.h" #include "Waypoints/SetHome.h" #include "utils/zzip_stream.h" +#include "LocalPath.h" #include #include "Library/rapidxml/rapidxml.hpp" diff --git a/Common/Source/SaveLoadTask/SaveDefaultTask.cpp b/Common/Source/SaveLoadTask/SaveDefaultTask.cpp index f8efa422f..248f6af6c 100644 --- a/Common/Source/SaveLoadTask/SaveDefaultTask.cpp +++ b/Common/Source/SaveLoadTask/SaveDefaultTask.cpp @@ -7,6 +7,7 @@ */ #include "externs.h" +#include "LocalPath.h" void SaveDefaultTask(void) { diff --git a/Common/Source/Sound/Win32/Sound.cpp b/Common/Source/Sound/Win32/Sound.cpp index 0a8ec6656..27c738ea0 100644 --- a/Common/Source/Sound/Win32/Sound.cpp +++ b/Common/Source/Sound/Win32/Sound.cpp @@ -8,6 +8,7 @@ #include "../Sound.h" #include "externs.h" +#include "LocalPath.h" #if defined(PNA) && defined(UNDER_CE) #include "Modeltype.h" #include "Devices/LKHolux.h" @@ -19,14 +20,12 @@ static bool bSoundFile = false; // this is true only if "_System/_Sounds" direct static TCHAR szSoundPath[MAX_PATH] = {}; // path of Sound file, initialized by #SoundGlobalInit end never change; SoundGlobalInit::SoundGlobalInit() { - TCHAR srcfile[MAX_PATH]; - SystemPath(szSoundPath,TEXT(LKD_SOUNDS)); - _stprintf(srcfile,TEXT("%s%s_SOUNDS"), szSoundPath, _T(DIRSEP)); - if ( lk::filesystem::exist(srcfile) ) { + SystemPath(szSoundPath, _T(LKD_SOUNDS), _T("_SOUNDS")); + if ( lk::filesystem::exist(szSoundPath) ) { bSoundFile = true; } else { - StartupStore(_T("ERROR NO SOUNDS DIRECTORY CHECKFILE <%s>%s"),srcfile,NEWLINE); - StartupStore(_T("------ LK8000 SOUNDS NOT WORKING!%s"),NEWLINE); + StartupStore(_T("ERROR NO SOUNDS DIRECTORY CHECKFILE <%s>"), szSoundPath); + StartupStore(_T("------ LK8000 SOUNDS NOT WORKING!")); } } diff --git a/Common/Source/Sound/alsa/Sound.cpp b/Common/Source/Sound/alsa/Sound.cpp index 9b5be9ba3..883f058a3 100644 --- a/Common/Source/Sound/alsa/Sound.cpp +++ b/Common/Source/Sound/alsa/Sound.cpp @@ -18,6 +18,7 @@ #include #include "Thread/Thread.hpp" #include "Thread/Cond.hpp" +#include "LocalPath.h" #define PCM_DEVICE "default" diff --git a/Common/Source/Terrain/OpenCreateClose.cpp b/Common/Source/Terrain/OpenCreateClose.cpp index 1e1d18f18..d25843e5b 100644 --- a/Common/Source/Terrain/OpenCreateClose.cpp +++ b/Common/Source/Terrain/OpenCreateClose.cpp @@ -10,6 +10,7 @@ #include "RasterTerrain.h" #include "Dialogs/dlgProgress.h" #include "Message.h" +#include "LocalPath.h" std::unique_ptr RasterTerrain::TerrainMap; Mutex RasterTerrain::mutex; diff --git a/Common/Source/Utils.cpp b/Common/Source/Utils.cpp index 6b3ee6d99..df38aa714 100644 --- a/Common/Source/Utils.cpp +++ b/Common/Source/Utils.cpp @@ -12,6 +12,7 @@ #include "Modeltype.h" #include "Dialogs.h" #include "Util/Clamp.hpp" +#include "LocalPath.h" int GetUTCOffset() { return UTCOffset; diff --git a/Common/Source/Waypoints/Read.cpp b/Common/Source/Waypoints/Read.cpp index c36ef1647..ebabb7a9b 100644 --- a/Common/Source/Waypoints/Read.cpp +++ b/Common/Source/Waypoints/Read.cpp @@ -9,6 +9,7 @@ #include "externs.h" #include "Waypointparser.h" #include "utils/zzip_stream.h" +#include "LocalPath.h" int globalFileNum = 0; diff --git a/Common/Source/Waypoints/Write.cpp b/Common/Source/Waypoints/Write.cpp index 661ea2d72..55ee6c460 100644 --- a/Common/Source/Waypoints/Write.cpp +++ b/Common/Source/Waypoints/Write.cpp @@ -8,6 +8,7 @@ #include "externs.h" #include "Waypointparser.h" +#include "LocalPath.h" #ifdef _UNICODE #define PF_ASCIISTR "%S" diff --git a/Common/Source/WindowControls.cpp b/Common/Source/WindowControls.cpp index d72e8c6a0..ad43e6c1b 100644 --- a/Common/Source/WindowControls.cpp +++ b/Common/Source/WindowControls.cpp @@ -30,6 +30,7 @@ #include "Util/Clamp.hpp" #include "utils/array_adaptor.h" #include "utils/stringext.h" +#include "LocalPath.h" #ifndef USE_GDI #include "Screen/SubCanvas.hpp" diff --git a/Common/Source/lk8000.cpp b/Common/Source/lk8000.cpp index 88c05ca15..7e3bc296c 100755 --- a/Common/Source/lk8000.cpp +++ b/Common/Source/lk8000.cpp @@ -58,6 +58,7 @@ #include "Baro.h" #include "OS/Sleep.h" #include "Comm/ExternalWind.h" +#include "LocalPath.h" #ifdef __linux__ #include