diff --git a/Sources/Jazz2/Compatibility/JJ2Level.cpp b/Sources/Jazz2/Compatibility/JJ2Level.cpp index 825c2dfe..8f4e1dc0 100644 --- a/Sources/Jazz2/Compatibility/JJ2Level.cpp +++ b/Sources/Jazz2/Compatibility/JJ2Level.cpp @@ -431,7 +431,7 @@ namespace Jazz2::Compatibility i32tos(i / 8, numberBuffer); StringView foundDot = path.findLastOr('.', path.end()); - String extraLayersPath = fs::FindPathCaseInsensitive(String(path.prefix(foundDot.begin()) + "-MLLE-Data-"_s + numberBuffer + ".j2l"_s)); + String extraLayersPath = fs::FindPathCaseInsensitive(path.prefix(foundDot.begin()) + "-MLLE-Data-"_s + numberBuffer + ".j2l"_s); JJ2Level extraLayersFile; if (extraLayersFile.Open(extraLayersPath, strictParser)) { diff --git a/Sources/Jazz2/UI/Menu/HighscoresSection.cpp b/Sources/Jazz2/UI/Menu/HighscoresSection.cpp index 2777f5b3..13b9ef0c 100644 --- a/Sources/Jazz2/UI/Menu/HighscoresSection.cpp +++ b/Sources/Jazz2/UI/Menu/HighscoresSection.cpp @@ -14,6 +14,7 @@ # include #elif defined(DEATH_TARGET_UNIX) # include +# include #endif using namespace Death::IO::Compression; diff --git a/Sources/Shared/Containers/StringConcatenable.h b/Sources/Shared/Containers/StringConcatenable.h index ba704399..9bcc02c2 100644 --- a/Sources/Shared/Containers/StringConcatenable.h +++ b/Sources/Shared/Containers/StringConcatenable.h @@ -140,11 +140,7 @@ namespace Death { namespace Containers { typedef String ConvertTo; static std::size_t size(const char a[N]) { - std::size_t n = N; - while (n > 0 && a[n - 1] == '\0') { - n--; - } - return n; + return strnlen(a, N); } static inline void appendTo(const char a[N], char*& out) { while (*a != '\0') { @@ -169,9 +165,6 @@ namespace Death { namespace Containers { return std::strlen(a); } static inline void appendTo(const char* a, char*& out) { - if (a == nullptr) { - return; - } while (*a != '\0') { *out++ = *a++; } diff --git a/Sources/Shared/IO/FileSystem.cpp b/Sources/Shared/IO/FileSystem.cpp index 8277765e..e2300640 100644 --- a/Sources/Shared/IO/FileSystem.cpp +++ b/Sources/Shared/IO/FileSystem.cpp @@ -600,12 +600,17 @@ namespace Death { namespace IO { goto Retry; # endif std::size_t charsLeft = sizeof(_path) - (_fileNamePart - _path) - 1; - std::size_t fileLength = strlen(entry->d_name); - if (fileLength > charsLeft) { +# if defined(__FreeBSD__) + std::size_t fileNameLength = entry->d_namlen; +# else + std::size_t fileNameLength = strlen(entry->d_name); +# endif + if (fileNameLength > charsLeft) { // Path is too long, skip this file goto Retry; } - strcpy(_fileNamePart, entry->d_name); + std::memcpy(_fileNamePart, entry->d_name, fileNameLength); + _fileNamePart[fileNameLength] = '\0'; } else { _path[0] = '\0'; } @@ -744,8 +749,13 @@ namespace Death { namespace IO { struct dirent* entry = ::readdir(d); while (entry != nullptr) { if (::strcasecmp(c, entry->d_name) == 0) { - strcpy(&result[rl], entry->d_name); - rl += strlen(entry->d_name); +# if defined(__FreeBSD__) + std::size_t fileNameLength = entry->d_namlen; +# else + std::size_t fileNameLength = std::strlen(entry->d_name); +# endif + std::memcpy(&result[rl], entry->d_name, fileNameLength); + rl += fileNameLength; ::closedir(d); d = ::opendir(result.data()); diff --git a/Sources/Shared/IO/FileSystem.h b/Sources/Shared/IO/FileSystem.h index 56137fc6..a9f1b98d 100644 --- a/Sources/Shared/IO/FileSystem.h +++ b/Sources/Shared/IO/FileSystem.h @@ -141,6 +141,10 @@ namespace Death { namespace IO { } #else static Containers::String FindPathCaseInsensitive(const Containers::StringView path); + + DEATH_ALWAYS_INLINE static Containers::String FindPathCaseInsensitive(Containers::String&& path) { + return FindPathCaseInsensitive(StringView{path}); + } #endif /** @brief Combines together specified path components */