From a962c90b30f36575d01162b64471fa77473237a0 Mon Sep 17 00:00:00 2001 From: Florent Rougon Date: Sat, 15 Apr 2017 09:29:59 +0200 Subject: [PATCH] Change SGPath::pathListSep into a const char array of size 2 Previously, SGPath::pathListSep was a char in static memory, that could be followed by anything (often '\0', as it seems... but not always). This is (was) dangerous, because it is then tempting to take its address and pass it to functions expecting a char * corresponding to a null-terminated string (C-style). SGPath::pathListSep is now a static array of two const chars: the path list separator followed by a '\0'. This implies that &SGPath::pathListSep can now be reliably interpreted as a C-style string of length 1 (not counting the null terminator), containing only the path list separator. --- simgear/misc/sg_path.cxx | 8 ++++---- simgear/misc/sg_path.hxx | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/simgear/misc/sg_path.cxx b/simgear/misc/sg_path.cxx index 59f74e9d..a736fa67 100644 --- a/simgear/misc/sg_path.cxx +++ b/simgear/misc/sg_path.cxx @@ -53,9 +53,9 @@ static const char sgDirPathSep = '/'; static const char sgDirPathSepBad = '\\'; #ifdef _WIN32 -const char SGPath::pathListSep = ';'; +const char SGPath::pathListSep[] = ";"; // this is null-terminated #else -const char SGPath::pathListSep = ':'; +const char SGPath::pathListSep[] = ":"; // ditto #endif #ifdef _WIN32 @@ -332,7 +332,7 @@ SGPath SGPath::operator/( const std::string& p ) const #if defined(ENABLE_OLD_PATH_API) //add a new path component to the existing path string void SGPath::add( const string& p ) { - append( SGPath::pathListSep+p ); + append( SGPath::pathListSep[0] + p ); } #endif @@ -664,7 +664,7 @@ string_list sgPathSplit( const string &search_path ) { bool done = false; while ( !done ) { - int index = tmp.find(SGPath::pathListSep); + int index = tmp.find(SGPath::pathListSep[0]); if (index >= 0) { result.push_back( tmp.substr(0, index) ); tmp = tmp.substr( index + 1 ); diff --git a/simgear/misc/sg_path.hxx b/simgear/misc/sg_path.hxx index f874182e..9f3dfc0f 100644 --- a/simgear/misc/sg_path.hxx +++ b/simgear/misc/sg_path.hxx @@ -52,8 +52,8 @@ class SGPath { public: - // OS-dependent separator used in paths lists - static const char pathListSep; + // OS-dependent separator used in paths lists (C-style string of length 1) + static const char pathListSep[2]; struct Permissions {