[ox/fs] Suppress unsafe buffer warnings

This commit is contained in:
Gary Talent 2024-11-26 22:30:57 -06:00
parent 287d42f2b9
commit cd3eeeef14
10 changed files with 46 additions and 14 deletions

View File

@ -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( add_library(
OxFS OxFS

View File

@ -10,6 +10,8 @@
#include <ox/fs/ptrarith/nodebuffer.hpp> #include <ox/fs/ptrarith/nodebuffer.hpp>
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox { namespace ox {
using InodeId_t = uint64_t; using InodeId_t = uint64_t;
@ -771,3 +773,5 @@ using FileStore16 = FileStoreTemplate<uint16_t>;
using FileStore32 = FileStoreTemplate<uint32_t>; using FileStore32 = FileStoreTemplate<uint32_t>;
} }
OX_CLANG_NOWARN_END

View File

@ -15,6 +15,8 @@
#include "types.hpp" #include "types.hpp"
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox { namespace ox {
template<typename InodeId_t> template<typename InodeId_t>
@ -333,3 +335,5 @@ using Directory16 = Directory<FileStore16, uint16_t>;
using Directory32 = Directory<FileStore32, uint32_t>; using Directory32 = Directory<FileStore32, uint32_t>;
} }
OX_CLANG_NOWARN_END

View File

@ -32,7 +32,9 @@ FileAddress::FileAddress(ox::StringViewCR path) noexcept {
auto pathSize = path.bytes(); auto pathSize = path.bytes();
m_data.path = new char[pathSize + 1]; m_data.path = new char[pathSize + 1];
memcpy(m_data.path, path.data(), pathSize); memcpy(m_data.path, path.data(), pathSize);
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
m_data.path[pathSize] = 0; m_data.path[pathSize] = 0;
OX_CLANG_NOWARN_END
m_type = FileAddressType::Path; m_type = FileAddressType::Path;
} }

View File

@ -13,6 +13,8 @@
#include <ox/model/typenamecatcher.hpp> #include <ox/model/typenamecatcher.hpp>
#include <ox/model/types.hpp> #include <ox/model/types.hpp>
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox { namespace ox {
enum class FileAddressType: int8_t { enum class FileAddressType: int8_t {
@ -180,3 +182,5 @@ constexpr Error model(T *h, CommonPtrWith<FileAddress> auto *fa) noexcept {
} }
} }
OX_CLANG_NOWARN_END

View File

@ -11,6 +11,8 @@
#include <ox/std/trace.hpp> #include <ox/std/trace.hpp>
#include "pathiterator.hpp" #include "pathiterator.hpp"
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox { namespace ox {
PathIterator::PathIterator(const char *path, std::size_t maxSize, std::size_t iterator) { 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) { Error PathIterator::dirPath(char *out, std::size_t outSize) {
const auto idx = ox::lastIndexOf(m_path, '/', m_maxSize); const auto idx = ox::lastIndexOf(m_path, '/', m_maxSize);
const auto size = static_cast<std::size_t>(idx + 1); const auto size = static_cast<std::size_t>(idx) + 1;
if (idx >= 0 && size < outSize) { if (idx >= 0 && size < outSize) {
ox::memcpy(out, m_path, size); ox::memcpy(out, m_path, size);
out[size] = 0; out[size] = 0;
@ -183,3 +185,5 @@ const char *PathIterator::fullPath() const {
} }
} }
OX_CLANG_NOWARN_END

View File

@ -13,6 +13,8 @@
#include "ptr.hpp" #include "ptr.hpp"
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox::ptrarith { namespace ox::ptrarith {
template<typename size_t, typename Item> template<typename size_t, typename Item>
@ -449,3 +451,5 @@ struct OX_PACKED Item {
}; };
} }
OX_CLANG_NOWARN_END

View File

@ -10,6 +10,8 @@
#include <ox/std/std.hpp> #include <ox/std/std.hpp>
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox::ptrarith { namespace ox::ptrarith {
template<typename T, typename size_t, size_t minOffset = 1> template<typename T, typename size_t, size_t minOffset = 1>
@ -256,3 +258,5 @@ constexpr Result<Ptr<T, size_t, minOffset>> Ptr<T, size_t, minOffset>::validate(
} }
} }
OX_CLANG_NOWARN_END

View File

@ -226,13 +226,14 @@ const std::map<ox::StringView, std::function<ox::Error(ox::StringView)>> tests =
}, },
}; };
int main(int argc, const char **args) { int main(int argc, const char **argv) {
if (argc < 2) { if (argc < 2) {
oxError("Must specify test to run"); oxError("Must specify test to run");
return -1; return -1;
} }
auto const args = ox::SpanView{argv, static_cast<size_t>(argc)};
ox::StringView const testName = args[1]; 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); auto const func = tests.find(testName);
if (func != tests.end()) { if (func != tests.end()) {
oxAssert(func->second(testArg), "Test returned Error"); oxAssert(func->second(testArg), "Test returned Error");

View File

@ -39,25 +39,25 @@ static ox::Result<ox::UniquePtr<ox::FileSystem>> loadFs(const char *path) noexce
return {ox::make_unique<ox::FileSystem32>(buff.data, buff.size)}; return {ox::make_unique<ox::FileSystem32>(buff.data, buff.size)};
} }
static ox::Error runLs(ox::FileSystem *fs, int argc, const char **argv) noexcept { static ox::Error runLs(ox::FileSystem *fs, ox::SpanView<const char*> args) noexcept {
if (argc < 2) { if (args.size() < 2) {
oxErr("Must provide a directory to ls\n"); oxErr("Must provide a directory to ls\n");
return OxError(1); return OxError(1);
} }
oxRequire(files, fs->ls(argv[1])); oxRequire(files, fs->ls(args[1]));
for (const auto &file : files) { for (const auto &file : files) {
oxOutf("{}\n", file); oxOutf("{}\n", file);
} }
return OxError(0); return OxError(0);
} }
static ox::Error runRead(ox::FileSystem *fs, int argc, const char **argv) noexcept { static ox::Error runRead(ox::FileSystem *fs, ox::SpanView<const char*> args) noexcept {
if (argc < 2) { if (args.size() < 2) {
oxErr("Must provide a path to a file to read\n"); oxErr("Must provide a path to a file to read\n");
return OxError(1); return OxError(1);
} }
oxRequire(buff, fs->read(ox::StringView(argv[1]))); oxRequire(buff, fs->read(ox::StringView(args[1])));
fwrite(buff.data(), sizeof(decltype(buff)::value_type), buff.size(), stdout); std::ignore = fwrite(buff.data(), sizeof(decltype(buff)::value_type), buff.size(), stdout);
return OxError(0); 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"); oxErr("OxFS file and subcommand arguments are required\n");
return OxError(1); return OxError(1);
} }
const auto fsPath = argv[1]; auto const args = ox::SpanView{argv, static_cast<size_t>(argc)};
ox::String subCmd(argv[2]); auto const fsPath = args[1];
ox::String subCmd(args[2]);
oxRequire(fs, loadFs(fsPath)); oxRequire(fs, loadFs(fsPath));
if (subCmd == "ls") { if (subCmd == "ls") {
return runLs(fs.get(), argc - 2, argv + 2); return runLs(fs.get(), args + 2);
} else if (subCmd == "read") { } else if (subCmd == "read") {
return runRead(fs.get(), argc - 2, argv + 2); return runRead(fs.get(), args + 2);
} }
return OxError(1); return OxError(1);
} }