From 9b8a8c4e46329a1f84abaa5cec6536f85e679b57 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 26 Nov 2024 21:59:26 -0600 Subject: [PATCH] [ox/std] Enable unsafe buffer warnings --- deps/ox/src/ox/std/CMakeLists.txt | 6 + deps/ox/src/ox/std/algorithm.hpp | 8 +- deps/ox/src/ox/std/array.hpp | 9 ++ deps/ox/src/ox/std/basestringview.hpp | 7 +- deps/ox/src/ox/std/buffer.hpp | 4 + deps/ox/src/ox/std/cstrops.hpp | 5 + deps/ox/src/ox/std/def.hpp | 11 ++ deps/ox/src/ox/std/fmt.hpp | 4 + deps/ox/src/ox/std/heapmgr.cpp | 5 + deps/ox/src/ox/std/istring.hpp | 3 + deps/ox/src/ox/std/iterator.hpp | 5 + deps/ox/src/ox/std/memops.cpp | 4 + deps/ox/src/ox/std/memops.hpp | 5 + deps/ox/src/ox/std/random.hpp | 5 + deps/ox/src/ox/std/stacktrace.cpp | 5 + deps/ox/src/ox/std/string.hpp | 9 +- deps/ox/src/ox/std/stringview.hpp | 3 + deps/ox/src/ox/std/strops.cpp | 3 + deps/ox/src/ox/std/strops.hpp | 9 +- deps/ox/src/ox/std/test/tests.cpp | 4 + deps/ox/src/ox/std/uuid.hpp | 5 + deps/ox/src/ox/std/vec.cpp | 2 +- deps/ox/src/ox/std/vec.hpp | 196 +------------------------- deps/ox/src/ox/std/vector.hpp | 4 + 24 files changed, 119 insertions(+), 202 deletions(-) diff --git a/deps/ox/src/ox/std/CMakeLists.txt b/deps/ox/src/ox/std/CMakeLists.txt index 890868a4..ae2b9410 100644 --- a/deps/ox/src/ox/std/CMakeLists.txt +++ b/deps/ox/src/ox/std/CMakeLists.txt @@ -1,3 +1,9 @@ +if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") + # enable warnings + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wunsafe-buffer-usage") +endif() + + if(OX_USE_STDLIB AND OX_ENABLE_TRACEHOOK) add_library( OxTraceHook SHARED diff --git a/deps/ox/src/ox/std/algorithm.hpp b/deps/ox/src/ox/std/algorithm.hpp index bc3d96e7..c6c9bd62 100644 --- a/deps/ox/src/ox/std/algorithm.hpp +++ b/deps/ox/src/ox/std/algorithm.hpp @@ -8,6 +8,10 @@ #pragma once +#include "def.hpp" + +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) + namespace ox { template @@ -40,4 +44,6 @@ constexpr OutIt copy_n(It in, Size cnt, OutIt out) { return out; } -} \ No newline at end of file +} + +OX_CLANG_NOWARN_END diff --git a/deps/ox/src/ox/std/array.hpp b/deps/ox/src/ox/std/array.hpp index 2e28331b..996fe5a4 100644 --- a/deps/ox/src/ox/std/array.hpp +++ b/deps/ox/src/ox/std/array.hpp @@ -9,6 +9,7 @@ #pragma once #include "bit.hpp" +#include "def.hpp" #include "error.hpp" #include "initializerlist.hpp" #include "iterator.hpp" @@ -17,6 +18,8 @@ #include "types.hpp" #include "utility.hpp" +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) + namespace ox { template @@ -35,6 +38,10 @@ class Array { public: constexpr Array() noexcept = default; + template + constexpr Array(Args ...list) noexcept: m_items{std::move(list)...} { + } + constexpr Array(std::initializer_list list) noexcept; constexpr Array(const Array &other); @@ -200,3 +207,5 @@ constexpr bool Array::contains(const T &v) const { } } + +OX_CLANG_NOWARN_END diff --git a/deps/ox/src/ox/std/basestringview.hpp b/deps/ox/src/ox/std/basestringview.hpp index 45ff0628..5532a53c 100644 --- a/deps/ox/src/ox/std/basestringview.hpp +++ b/deps/ox/src/ox/std/basestringview.hpp @@ -9,9 +9,12 @@ #pragma once #include "bit.hpp" +#include "def.hpp" #include "cstrops.hpp" #include "iterator.hpp" +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) + namespace ox::detail { class BaseStringView { @@ -202,7 +205,7 @@ class BaseStringView { } [[nodiscard]] - constexpr auto operator[](std::size_t i) const noexcept { + constexpr auto &operator[](std::size_t i) const noexcept { return m_str[i]; } @@ -215,3 +218,5 @@ class BaseStringView { }; } + +OX_CLANG_NOWARN_END diff --git a/deps/ox/src/ox/std/buffer.hpp b/deps/ox/src/ox/std/buffer.hpp index 4cf0fd02..4dfef6a1 100644 --- a/deps/ox/src/ox/std/buffer.hpp +++ b/deps/ox/src/ox/std/buffer.hpp @@ -14,6 +14,8 @@ #include "vector.hpp" #include "writer.hpp" +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) + namespace ox { extern template class Vector; @@ -233,3 +235,5 @@ extern template class WriterT; extern template class WriterT; } + +OX_CLANG_NOWARN_END diff --git a/deps/ox/src/ox/std/cstrops.hpp b/deps/ox/src/ox/std/cstrops.hpp index c86cacb3..6b7820dc 100644 --- a/deps/ox/src/ox/std/cstrops.hpp +++ b/deps/ox/src/ox/std/cstrops.hpp @@ -8,9 +8,12 @@ #pragma once +#include "def.hpp" #include "types.hpp" #include "typetraits.hpp" +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) + namespace ox { template @@ -113,3 +116,5 @@ constexpr int lastIndexOf(const auto &str, int character, std::size_t maxLen = 0 } } + +OX_CLANG_NOWARN_END diff --git a/deps/ox/src/ox/std/def.hpp b/deps/ox/src/ox/std/def.hpp index 4cad13e2..9d57f385 100644 --- a/deps/ox/src/ox/std/def.hpp +++ b/deps/ox/src/ox/std/def.hpp @@ -71,6 +71,17 @@ constexpr void oxAssert(const ox::Error&, const char*) noexcept {} #define ox_alloca(size) __builtin_alloca(size) #endif +#define OX_PRAGMA(x) _Pragma(#x) +#ifdef __clang__ +#define OX_CLANG_NOWARN_BEGIN(warnoption) \ + OX_PRAGMA(clang diagnostic push) \ + OX_PRAGMA(clang diagnostic ignored #warnoption) +#define OX_CLANG_NOWARN_END OX_PRAGMA(clang diagnostic pop) +#else +#define OX_CLANG_NOWARN_BEGIN(warnoption) +#define OX_CLANG_NOWARN_END +#endif + /** * @return an ox::MallocaPtr of the given type pointing to the requested size memory allocation */ diff --git a/deps/ox/src/ox/std/fmt.hpp b/deps/ox/src/ox/std/fmt.hpp index 0ec2aa2a..4694d026 100644 --- a/deps/ox/src/ox/std/fmt.hpp +++ b/deps/ox/src/ox/std/fmt.hpp @@ -25,6 +25,8 @@ #include "types.hpp" #include "typetraits.hpp" +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) + namespace ox { namespace detail { @@ -225,3 +227,5 @@ constexpr Result join(auto const&d, auto const&list) { } } + +OX_CLANG_NOWARN_END diff --git a/deps/ox/src/ox/std/heapmgr.cpp b/deps/ox/src/ox/std/heapmgr.cpp index e7ef0552..9cdc358b 100644 --- a/deps/ox/src/ox/std/heapmgr.cpp +++ b/deps/ox/src/ox/std/heapmgr.cpp @@ -8,8 +8,11 @@ #include "assert.hpp" #include "bit.hpp" +#include "def.hpp" #include "heapmgr.hpp" +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) + namespace ox::heapmgr { static struct HeapSegment *volatile g_heapBegin = nullptr; @@ -142,3 +145,5 @@ void operator delete[](void *ptr, unsigned long int) noexcept { } #endif + +OX_CLANG_NOWARN_END diff --git a/deps/ox/src/ox/std/istring.hpp b/deps/ox/src/ox/std/istring.hpp index e2142471..6df2caab 100644 --- a/deps/ox/src/ox/std/istring.hpp +++ b/deps/ox/src/ox/std/istring.hpp @@ -10,6 +10,7 @@ #include "array.hpp" #include "concepts.hpp" +#include "def.hpp" #include "cstrops.hpp" #include "memops.hpp" #include "error.hpp" @@ -175,7 +176,9 @@ constexpr Error IString::append(const char *str, std::size_t strLen) noe strLen = cap() - currentLen; err = OxError(1, "Insufficient space for full string"); } +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) ox::strncpy(m_buff.data() + currentLen, str, strLen); +OX_CLANG_NOWARN_END // make sure last element is a null terminator m_buff[currentLen + strLen] = 0; m_size += strLen; diff --git a/deps/ox/src/ox/std/iterator.hpp b/deps/ox/src/ox/std/iterator.hpp index 0e85b643..d82e13ff 100644 --- a/deps/ox/src/ox/std/iterator.hpp +++ b/deps/ox/src/ox/std/iterator.hpp @@ -8,6 +8,7 @@ #pragma once +#include "def.hpp" #include "error.hpp" #include "math.hpp" @@ -39,6 +40,8 @@ struct contiguous_iterator_tag: public random_access_iterator_tag { #include #endif +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) + namespace ox { template @@ -56,3 +59,5 @@ void *memsetElements(T *ptr, T val, std::size_t elements) noexcept { } } + +OX_CLANG_NOWARN_END diff --git a/deps/ox/src/ox/std/random.hpp b/deps/ox/src/ox/std/random.hpp index ac6bbf6e..5f4ae4de 100644 --- a/deps/ox/src/ox/std/random.hpp +++ b/deps/ox/src/ox/std/random.hpp @@ -9,9 +9,12 @@ #pragma once #include "bit.hpp" +#include "def.hpp" #include "stddef.hpp" #include "types.hpp" +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) + namespace ox { using RandomSeed = uint64_t[2]; @@ -51,3 +54,5 @@ constexpr uint64_t Random::gen() noexcept { } } + +OX_CLANG_NOWARN_END diff --git a/deps/ox/src/ox/std/stacktrace.cpp b/deps/ox/src/ox/std/stacktrace.cpp index b4da2712..5d3271a7 100644 --- a/deps/ox/src/ox/std/stacktrace.cpp +++ b/deps/ox/src/ox/std/stacktrace.cpp @@ -18,11 +18,14 @@ #endif #endif +#include "def.hpp" #include "defines.hpp" #include "string.hpp" #include "trace.hpp" #include "vector.hpp" +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) + namespace ox { #if defined(OX_USE_STDLIB) && __has_include() @@ -100,3 +103,5 @@ void printStackTrace([[maybe_unused]]unsigned shave) noexcept { } } + +OX_CLANG_NOWARN_END diff --git a/deps/ox/src/ox/std/string.hpp b/deps/ox/src/ox/std/string.hpp index 7d6a7bac..93daefdf 100644 --- a/deps/ox/src/ox/std/string.hpp +++ b/deps/ox/src/ox/std/string.hpp @@ -13,6 +13,7 @@ #endif #include "algorithm.hpp" +#include "def.hpp" #include "ignore.hpp" #include "memops.hpp" #include "serialize.hpp" @@ -22,6 +23,8 @@ #include "strops.hpp" #include "vector.hpp" +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) + namespace ox { template @@ -496,12 +499,12 @@ constexpr char &BasicString::operator[](std::size_t i) noexce template constexpr BasicString BasicString::substr(std::size_t pos) const noexcept { - return BasicString(m_buff.data() + pos, m_buff.size() - pos - 1); + return BasicString(&m_buff[pos], m_buff.size() - pos - 1); } template constexpr BasicString BasicString::substr(std::size_t begin, std::size_t end) const noexcept { - const auto src = m_buff.data() + begin; + const auto src = &m_buff[begin]; const auto size = end - begin; BasicString out(size); const auto buff = out.data(); @@ -568,3 +571,5 @@ struct MaybeView> { }; } + +OX_CLANG_NOWARN_END diff --git a/deps/ox/src/ox/std/stringview.hpp b/deps/ox/src/ox/std/stringview.hpp index c9c6af77..b2c42940 100644 --- a/deps/ox/src/ox/std/stringview.hpp +++ b/deps/ox/src/ox/std/stringview.hpp @@ -17,6 +17,8 @@ #include "maybeview.hpp" #include "writer.hpp" +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) + namespace ox { template @@ -116,3 +118,4 @@ constexpr ox::Result atoi(ox::StringViewCR str) noexcept { } +OX_CLANG_NOWARN_END diff --git a/deps/ox/src/ox/std/strops.cpp b/deps/ox/src/ox/std/strops.cpp index 2bab557b..add17428 100644 --- a/deps/ox/src/ox/std/strops.cpp +++ b/deps/ox/src/ox/std/strops.cpp @@ -6,6 +6,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +#include "def.hpp" #include "strops.hpp" static_assert(ox::strcmp("asdf", "hijk") < 0, "asdf < hijk"); @@ -17,7 +18,9 @@ static_assert(ox::strcmp("", "") == 0, "\"\" == \"\""); static_assert([] { auto testStr = "asdf"; +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) return ox::strchr(testStr, 0, 4) == &testStr[4]; +OX_CLANG_NOWARN_END }(), "ox::strchr 0"); static_assert([] { diff --git a/deps/ox/src/ox/std/strops.hpp b/deps/ox/src/ox/std/strops.hpp index 260ec83d..65e89004 100644 --- a/deps/ox/src/ox/std/strops.hpp +++ b/deps/ox/src/ox/std/strops.hpp @@ -9,18 +9,21 @@ #pragma once #include "cstrops.hpp" +#include "def.hpp" #include "error.hpp" #include "math.hpp" #include "stringview.hpp" #include "types.hpp" #include "vector.hpp" +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) + namespace ox { [[nodiscard]] constexpr ox::StringView substr(ox::StringView const&str, std::size_t pos) noexcept { if (str.len() >= pos) { - return {str.data() + pos, str.len() - pos}; + return {&str[pos], str.len() - pos}; } return {}; } @@ -28,7 +31,7 @@ constexpr ox::StringView substr(ox::StringView const&str, std::size_t pos) noexc [[nodiscard]] constexpr ox::StringView substr(ox::StringView const&str, std::size_t start, std::size_t end) noexcept { if (str.len() >= start && end >= start) { - return {str.data() + start, end - start}; + return {&str[start], end - start}; } return {}; } @@ -113,3 +116,5 @@ constexpr ox::Result lastIndexOf(ox::StringViewCR str, int characte } } + +OX_CLANG_NOWARN_END diff --git a/deps/ox/src/ox/std/test/tests.cpp b/deps/ox/src/ox/std/test/tests.cpp index 1a470b23..b174dc64 100644 --- a/deps/ox/src/ox/std/test/tests.cpp +++ b/deps/ox/src/ox/std/test/tests.cpp @@ -121,7 +121,9 @@ static std::map tests = { auto a1 = static_cast(ox::heapmgr::malloc(5)); auto a2 = static_cast(ox::heapmgr::malloc(5)); oxAssert(a1 >= buff.front().unwrap() && a1 < buff.back().unwrap(), "malloc is broken"); +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) oxAssert(a2 >= buff.front().unwrap() && a2 < buff.back().unwrap() && a2 > a1 + 5, "malloc is broken"); +OX_CLANG_NOWARN_END ox::heapmgr::free(a1); ox::heapmgr::free(a2); return OxError(0); @@ -474,7 +476,9 @@ int main(int argc, const char **args) { if (argc < 2) { oxError("Must specify test to run"); } +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) auto const testName = args[1]; +OX_CLANG_NOWARN_END auto const func = tests.find(testName); if (func != tests.end()) { oxAssert(func->second(), "Test returned Error"); diff --git a/deps/ox/src/ox/std/uuid.hpp b/deps/ox/src/ox/std/uuid.hpp index d6aa3b40..444a856a 100644 --- a/deps/ox/src/ox/std/uuid.hpp +++ b/deps/ox/src/ox/std/uuid.hpp @@ -9,6 +9,7 @@ #pragma once #include "bit.hpp" +#include "def.hpp" #include "ignore.hpp" #include "istring.hpp" #include "buffer.hpp" @@ -18,6 +19,8 @@ #include "stringview.hpp" #include "strops.hpp" +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) + namespace ox { using UUIDStr = ox::IString<36>; @@ -220,3 +223,5 @@ constexpr Error model(T *io, ox::CommonPtrWith auto *obj) noexcept { } } + +OX_CLANG_NOWARN_END diff --git a/deps/ox/src/ox/std/vec.cpp b/deps/ox/src/ox/std/vec.cpp index 6da9c757..8220cec6 100644 --- a/deps/ox/src/ox/std/vec.cpp +++ b/deps/ox/src/ox/std/vec.cpp @@ -14,7 +14,7 @@ namespace ox { static_assert([] { Vec2 v(1, 2); - return v.x == 1 && v.y == 2 && v[0] == 1 && v[1] == 2 && v.size() == 2; + return v.x == 1 && v.y == 2 && v.size() == 2; }()); } diff --git a/deps/ox/src/ox/std/vec.hpp b/deps/ox/src/ox/std/vec.hpp index 3c443d4f..8b44ce02 100644 --- a/deps/ox/src/ox/std/vec.hpp +++ b/deps/ox/src/ox/std/vec.hpp @@ -34,112 +34,6 @@ class Vec2 { float x = 0; float y = 0; - template - struct iterator: public ox::Iterator { - private: - PtrType m_t = nullptr; - size_type m_offset = 0; - size_type m_max = 0; - - public: - constexpr iterator() noexcept = default; - - constexpr iterator(PtrType t, size_type offset, size_type max) noexcept { - m_t = t; - m_offset = offset; - m_max = max; - } - - [[nodiscard]] - constexpr auto offset() const noexcept { - return m_offset; - } - - constexpr iterator operator+(size_type s) const noexcept { - if constexpr(reverse) { - return iterator(m_t, ox::max(m_offset - s, 0), m_max); - } else { - return iterator(m_t, ox::min(m_offset + s, m_max), m_max); - } - } - - constexpr typename ox::Iterator::difference_type - operator-(const iterator &other) const noexcept { - if constexpr(reverse) { - return m_offset + other.m_offset; - } else { - return m_offset - other.m_offset; - } - } - - constexpr iterator operator-(size_type s) const noexcept { - if constexpr(reverse) { - return iterator(m_t, ox::min(m_offset + s, m_max), m_max); - } else { - return iterator(m_t, ox::max(m_offset - s, 0), m_max); - } - } - - constexpr iterator &operator+=(size_type s) noexcept { - if constexpr(reverse) { - m_offset = ox::max(m_offset - s, 0); - } else { - m_offset = ox::min(m_offset + s, m_max); - } - return *this; - } - - constexpr iterator &operator-=(size_type s) noexcept { - if constexpr(reverse) { - m_offset = ox::min(m_offset + s, m_max); - } else { - m_offset = ox::max(m_offset - s, 0); - } - return *this; - } - - constexpr iterator &operator++() noexcept { - return operator+=(1); - } - - constexpr iterator &operator--() noexcept { - return operator-=(1); - } - - constexpr RefType operator*() const noexcept { - return m_t[m_offset]; - } - - constexpr RefType operator[](size_type s) const noexcept { - return m_t[s]; - } - - constexpr bool operator<(const iterator &other) const noexcept { - return m_offset < other.m_offset; - } - - constexpr bool operator>(const iterator &other) const noexcept { - return m_offset > other.m_offset; - } - - constexpr bool operator<=(const iterator &other) const noexcept { - return m_offset <= other.m_offset; - } - - constexpr bool operator>=(const iterator &other) const noexcept { - return m_offset >= other.m_offset; - } - - constexpr bool operator==(const iterator &other) const noexcept { - return m_t == other.m_t && m_offset == other.m_offset && m_max == other.m_max; - } - - constexpr bool operator!=(const iterator &other) const noexcept { - return m_t != other.m_t || m_offset != other.m_offset || m_max != other.m_max; - } - - }; - constexpr Vec2() noexcept = default; explicit constexpr Vec2(class Point const&pt) noexcept; @@ -159,85 +53,8 @@ class Vec2 { } #endif - [[nodiscard]] - constexpr iterator<> begin() noexcept { - return {start(), 0, size()}; - } - - [[nodiscard]] - constexpr iterator<> end() noexcept { - return {start(), size(), size()}; - } - - [[nodiscard]] - constexpr iterator begin() const noexcept { - return {start(), 0, size()}; - } - - [[nodiscard]] - constexpr iterator end() const noexcept { - return {start(), size(), size()}; - } - - [[nodiscard]] - constexpr iterator rbegin() noexcept { - return {start(), size() - 1, size()}; - } - - [[nodiscard]] - constexpr iterator rend() noexcept { - return {start(), ox::MaxValue, size()}; - } - - [[nodiscard]] - constexpr iterator rbegin() const noexcept { - return {start(), size() - 1, size()}; - } - - [[nodiscard]] - constexpr iterator rend() const noexcept { - return {start(), ox::MaxValue, size()}; - } - - constexpr auto &operator[](std::size_t i) noexcept { - if (std::is_constant_evaluated()) { - switch (i) { - case 0: - return x; - case 1: - return y; - default: - oxAssert(false, "Read past end of Vec2"); - return y; - } - } else { - return start()[i]; - } - } - - constexpr const auto &operator[](std::size_t i) const noexcept { - if (std::is_constant_evaluated()) { - switch (i) { - case 0: - return x; - case 1: - return y; - default: - oxAssert(false, "Read past end of Vec2"); - return y; - } - } else { - return start()[i]; - } - } - constexpr auto operator==(const Vec2 &v) const noexcept { - for (auto i = 0u; i < v.size(); ++i) { - if ((*this)[i] != v[i]) { - return false; - } - } - return true; + return x == v.x && y == v.y; } constexpr auto operator!=(const Vec2 &v) const noexcept { @@ -292,17 +109,6 @@ class Vec2 { y /= i; return *this; } - - protected: - [[nodiscard]] - constexpr float *start() noexcept { - return&x; - } - - [[nodiscard]] - constexpr const float *start() const noexcept { - return &x; - } }; template diff --git a/deps/ox/src/ox/std/vector.hpp b/deps/ox/src/ox/std/vector.hpp index 4627bdb3..229c2d17 100644 --- a/deps/ox/src/ox/std/vector.hpp +++ b/deps/ox/src/ox/std/vector.hpp @@ -20,6 +20,8 @@ #include "types.hpp" #include "utility.hpp" +OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) + namespace ox { namespace detail { @@ -709,3 +711,5 @@ constexpr auto alignOf(const Vector&) noexcept { } } + +OX_CLANG_NOWARN_END