From cd3eeeef140376bbf5f9aa35072313013c1f578e Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 26 Nov 2024 22:30:57 -0600 Subject: [PATCH] [ox/fs] Suppress unsafe buffer warnings --- deps/ox/src/ox/fs/CMakeLists.txt | 4 ++++ .../src/ox/fs/filestore/filestoretemplate.hpp | 4 ++++ deps/ox/src/ox/fs/filesystem/directory.hpp | 4 ++++ deps/ox/src/ox/fs/filesystem/filelocation.cpp | 2 ++ deps/ox/src/ox/fs/filesystem/filelocation.hpp | 4 ++++ deps/ox/src/ox/fs/filesystem/pathiterator.cpp | 6 ++++- deps/ox/src/ox/fs/ptrarith/nodebuffer.hpp | 4 ++++ deps/ox/src/ox/fs/ptrarith/ptr.hpp | 4 ++++ deps/ox/src/ox/fs/test/tests.cpp | 5 ++-- deps/ox/src/ox/fs/tool.cpp | 23 ++++++++++--------- 10 files changed, 46 insertions(+), 14 deletions(-) diff --git a/deps/ox/src/ox/fs/CMakeLists.txt b/deps/ox/src/ox/fs/CMakeLists.txt index 543cae86..960f7c64 100644 --- a/deps/ox/src/ox/fs/CMakeLists.txt +++ b/deps/ox/src/ox/fs/CMakeLists.txt @@ -1,3 +1,7 @@ +if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") + # enable warnings + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wunsafe-buffer-usage") +endif() add_library( OxFS diff --git a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp index 8d0505b5..12367e48 100644 --- a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp +++ b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp @@ -10,6 +10,8 @@ #include +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) + namespace ox { using InodeId_t = uint64_t; @@ -771,3 +773,5 @@ using FileStore16 = FileStoreTemplate; using FileStore32 = FileStoreTemplate; } + +OX_CLANG_NOWARN_END diff --git a/deps/ox/src/ox/fs/filesystem/directory.hpp b/deps/ox/src/ox/fs/filesystem/directory.hpp index 5adff262..2dde5603 100644 --- a/deps/ox/src/ox/fs/filesystem/directory.hpp +++ b/deps/ox/src/ox/fs/filesystem/directory.hpp @@ -15,6 +15,8 @@ #include "types.hpp" +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) + namespace ox { template @@ -333,3 +335,5 @@ using Directory16 = Directory; using Directory32 = Directory; } + +OX_CLANG_NOWARN_END diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.cpp b/deps/ox/src/ox/fs/filesystem/filelocation.cpp index 51b9f2d1..a8088928 100644 --- a/deps/ox/src/ox/fs/filesystem/filelocation.cpp +++ b/deps/ox/src/ox/fs/filesystem/filelocation.cpp @@ -32,7 +32,9 @@ FileAddress::FileAddress(ox::StringViewCR path) noexcept { auto pathSize = path.bytes(); m_data.path = new char[pathSize + 1]; memcpy(m_data.path, path.data(), pathSize); +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) m_data.path[pathSize] = 0; +OX_CLANG_NOWARN_END m_type = FileAddressType::Path; } diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.hpp b/deps/ox/src/ox/fs/filesystem/filelocation.hpp index c8f6cfa5..99944ad8 100644 --- a/deps/ox/src/ox/fs/filesystem/filelocation.hpp +++ b/deps/ox/src/ox/fs/filesystem/filelocation.hpp @@ -13,6 +13,8 @@ #include #include +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) + namespace ox { enum class FileAddressType: int8_t { @@ -180,3 +182,5 @@ constexpr Error model(T *h, CommonPtrWith auto *fa) noexcept { } } + +OX_CLANG_NOWARN_END diff --git a/deps/ox/src/ox/fs/filesystem/pathiterator.cpp b/deps/ox/src/ox/fs/filesystem/pathiterator.cpp index 6f5af207..77d40502 100644 --- a/deps/ox/src/ox/fs/filesystem/pathiterator.cpp +++ b/deps/ox/src/ox/fs/filesystem/pathiterator.cpp @@ -11,6 +11,8 @@ #include #include "pathiterator.hpp" +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) + namespace ox { PathIterator::PathIterator(const char *path, std::size_t maxSize, std::size_t iterator) { @@ -30,7 +32,7 @@ PathIterator::PathIterator(StringViewCR path): PathIterator(path.data(), path.by */ Error PathIterator::dirPath(char *out, std::size_t outSize) { const auto idx = ox::lastIndexOf(m_path, '/', m_maxSize); - const auto size = static_cast(idx + 1); + const auto size = static_cast(idx) + 1; if (idx >= 0 && size < outSize) { ox::memcpy(out, m_path, size); out[size] = 0; @@ -183,3 +185,5 @@ const char *PathIterator::fullPath() const { } } + +OX_CLANG_NOWARN_END diff --git a/deps/ox/src/ox/fs/ptrarith/nodebuffer.hpp b/deps/ox/src/ox/fs/ptrarith/nodebuffer.hpp index e09f8371..358a7985 100644 --- a/deps/ox/src/ox/fs/ptrarith/nodebuffer.hpp +++ b/deps/ox/src/ox/fs/ptrarith/nodebuffer.hpp @@ -13,6 +13,8 @@ #include "ptr.hpp" +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) + namespace ox::ptrarith { template @@ -449,3 +451,5 @@ struct OX_PACKED Item { }; } + +OX_CLANG_NOWARN_END diff --git a/deps/ox/src/ox/fs/ptrarith/ptr.hpp b/deps/ox/src/ox/fs/ptrarith/ptr.hpp index 99940050..5e0efac2 100644 --- a/deps/ox/src/ox/fs/ptrarith/ptr.hpp +++ b/deps/ox/src/ox/fs/ptrarith/ptr.hpp @@ -10,6 +10,8 @@ #include +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) + namespace ox::ptrarith { template @@ -256,3 +258,5 @@ constexpr Result> Ptr::validate( } } + +OX_CLANG_NOWARN_END diff --git a/deps/ox/src/ox/fs/test/tests.cpp b/deps/ox/src/ox/fs/test/tests.cpp index 68199eae..902790b5 100644 --- a/deps/ox/src/ox/fs/test/tests.cpp +++ b/deps/ox/src/ox/fs/test/tests.cpp @@ -226,13 +226,14 @@ const std::map> tests = }, }; -int main(int argc, const char **args) { +int main(int argc, const char **argv) { if (argc < 2) { oxError("Must specify test to run"); return -1; } + auto const args = ox::SpanView{argv, static_cast(argc)}; ox::StringView const testName = args[1]; - ox::StringView const testArg = args[2] ? args[2] : nullptr; + ox::StringView const testArg = argc >= 3 ? args[2] : nullptr; auto const func = tests.find(testName); if (func != tests.end()) { oxAssert(func->second(testArg), "Test returned Error"); diff --git a/deps/ox/src/ox/fs/tool.cpp b/deps/ox/src/ox/fs/tool.cpp index 3d52f34f..6c2767d2 100644 --- a/deps/ox/src/ox/fs/tool.cpp +++ b/deps/ox/src/ox/fs/tool.cpp @@ -39,25 +39,25 @@ static ox::Result> loadFs(const char *path) noexce return {ox::make_unique(buff.data, buff.size)}; } -static ox::Error runLs(ox::FileSystem *fs, int argc, const char **argv) noexcept { - if (argc < 2) { +static ox::Error runLs(ox::FileSystem *fs, ox::SpanView args) noexcept { + if (args.size() < 2) { oxErr("Must provide a directory to ls\n"); return OxError(1); } - oxRequire(files, fs->ls(argv[1])); + oxRequire(files, fs->ls(args[1])); for (const auto &file : files) { oxOutf("{}\n", file); } return OxError(0); } -static ox::Error runRead(ox::FileSystem *fs, int argc, const char **argv) noexcept { - if (argc < 2) { +static ox::Error runRead(ox::FileSystem *fs, ox::SpanView args) noexcept { + if (args.size() < 2) { oxErr("Must provide a path to a file to read\n"); return OxError(1); } - oxRequire(buff, fs->read(ox::StringView(argv[1]))); - fwrite(buff.data(), sizeof(decltype(buff)::value_type), buff.size(), stdout); + oxRequire(buff, fs->read(ox::StringView(args[1]))); + std::ignore = fwrite(buff.data(), sizeof(decltype(buff)::value_type), buff.size(), stdout); return OxError(0); } @@ -66,13 +66,14 @@ static ox::Error run(int argc, const char **argv) noexcept { oxErr("OxFS file and subcommand arguments are required\n"); return OxError(1); } - const auto fsPath = argv[1]; - ox::String subCmd(argv[2]); + auto const args = ox::SpanView{argv, static_cast(argc)}; + auto const fsPath = args[1]; + ox::String subCmd(args[2]); oxRequire(fs, loadFs(fsPath)); if (subCmd == "ls") { - return runLs(fs.get(), argc - 2, argv + 2); + return runLs(fs.get(), args + 2); } else if (subCmd == "read") { - return runRead(fs.get(), argc - 2, argv + 2); + return runRead(fs.get(), args + 2); } return OxError(1); }