From 6af00d9a2eee762fa3f19ed79bb0dc1835a326a9 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 27 Nov 2024 00:14:57 -0600 Subject: [PATCH] [nostalgia] Enable warnings for unsafe buffers --- src/nostalgia/CMakeLists.txt | 5 +++++ src/nostalgia/modules/core/src/gba/gfx.cpp | 4 ++++ src/nostalgia/modules/core/src/gba/panic.cpp | 2 ++ src/nostalgia/modules/core/src/opengl/gfx.cpp | 13 +++++++------ .../commands/deletetilescommand.cpp | 12 ++++++------ .../commands/inserttilescommand.cpp | 16 ++++++++-------- .../tilesheeteditor/tilesheeteditormodel.cpp | 6 +++--- .../tilesheeteditor/tilesheeteditormodel.hpp | 2 +- src/nostalgia/modules/core/test/tests.cpp | 3 ++- 9 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/nostalgia/CMakeLists.txt b/src/nostalgia/CMakeLists.txt index 4600ff5c..986c3f16 100644 --- a/src/nostalgia/CMakeLists.txt +++ b/src/nostalgia/CMakeLists.txt @@ -1,4 +1,9 @@ +if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") + # enable warnings + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wunsafe-buffer-usage") +endif() + project(nostalgia CXX) #project packages diff --git a/src/nostalgia/modules/core/src/gba/gfx.cpp b/src/nostalgia/modules/core/src/gba/gfx.cpp index c2dffd56..db0256bb 100644 --- a/src/nostalgia/modules/core/src/gba/gfx.cpp +++ b/src/nostalgia/modules/core/src/gba/gfx.cpp @@ -17,6 +17,8 @@ #include "context.hpp" +OX_ALLOW_UNSAFE_BUFFERS_BEGIN + namespace nostalgia::core { static constexpr auto SpriteCount = 128; @@ -283,3 +285,5 @@ uint_t spriteCount(Context&) noexcept { } } + +OX_ALLOW_UNSAFE_BUFFERS_END diff --git a/src/nostalgia/modules/core/src/gba/panic.cpp b/src/nostalgia/modules/core/src/gba/panic.cpp index 728e3b00..bc6169da 100644 --- a/src/nostalgia/modules/core/src/gba/panic.cpp +++ b/src/nostalgia/modules/core/src/gba/panic.cpp @@ -25,7 +25,9 @@ using namespace nostalgia::core; void panic(const char *file, int line, const char *panicMsg, ox::Error const&err) noexcept { // reset heap to make sure we have enough memory to allocate context data +OX_ALLOW_UNSAFE_BUFFERS_BEGIN ox::heapmgr::initHeap(HEAP_BEGIN, HEAP_END); +OX_ALLOW_UNSAFE_BUFFERS_END auto tctx = turbine::init(keel::loadRomFs("").unwrap(), "Nostalgia").unwrap(); auto ctx = init(*tctx).unwrap(); std::ignore = initGfx(*ctx, {}); diff --git a/src/nostalgia/modules/core/src/opengl/gfx.cpp b/src/nostalgia/modules/core/src/opengl/gfx.cpp index dc10de05..efe06de4 100644 --- a/src/nostalgia/modules/core/src/opengl/gfx.cpp +++ b/src/nostalgia/modules/core/src/opengl/gfx.cpp @@ -554,20 +554,21 @@ static ox::Result buildSetTsd( static void copyPixels( CompactTileSheet const&ts, - uint32_t *dst, + ox::Span dst, size_t const srcPxIdx, size_t pxlCnt) noexcept { + size_t idx{}; if (ts.bpp == 4) { for (size_t i = 0; i < pxlCnt; i += 2) { auto const [a, b] = get2Pixels4Bpp(ts, i + srcPxIdx); - *(dst++) = a; - *(dst++) = b; + dst[idx++] = a; + dst[idx++] = b; } } else if (ts.bpp == 8) { for (size_t i = 0; i < pxlCnt; i += 2) { auto const [a, b] = get2Pixels8Bpp(ts, i + srcPxIdx); - *(dst++) = a; - *(dst++) = b; + dst[idx++] = a; + dst[idx++] = b; } } } @@ -587,7 +588,7 @@ ox::Error loadBgTileSheet( if (dstPxIdx + pxlCnt >= cbbPxls.size()) { return OxError(1, "video mem dst overflow"); } - auto const dst = &cbbPxls[dstPxIdx]; + auto const dst = ox::Span{cbbPxls} + dstPxIdx; copyPixels(ts, dst, srcPxIdx, pxlCnt); auto const cbbTiles = cbbPxls.size() / bytesPerTile; int constexpr cbbWidth = 8; diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/deletetilescommand.cpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/deletetilescommand.cpp index 09e57497..9e7afd48 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/deletetilescommand.cpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/deletetilescommand.cpp @@ -22,7 +22,7 @@ core::DeleteTilesCommand::DeleteTilesCommand( auto &s = getSubSheet(m_img, m_idx); auto &p = s.pixels; auto dst = m_deletedPixels.data(); - auto src = p.data() + m_deletePos; + auto src = &p[m_deletePos]; const auto sz = m_deleteSz * sizeof(decltype(p[0])); ox::memcpy(dst, src, sz); } @@ -32,9 +32,9 @@ ox::Error core::DeleteTilesCommand::redo() noexcept { auto &s = getSubSheet(m_img, m_idx); auto &p = s.pixels; auto srcPos = m_deletePos + m_deleteSz; - const auto src = p.data() + srcPos; - const auto dst1 = p.data() + m_deletePos; - const auto dst2 = p.data() + (p.size() - m_deleteSz); + const auto src = &p[srcPos]; + const auto dst1 = &p[m_deletePos]; + const auto dst2 = &p[(p.size() - m_deleteSz)]; ox::memmove(dst1, src, p.size() - srcPos); ox::memset(dst2, 0, m_deleteSz * sizeof(decltype(p[0]))); return {}; @@ -43,8 +43,8 @@ ox::Error core::DeleteTilesCommand::redo() noexcept { ox::Error DeleteTilesCommand::undo() noexcept { auto &s = getSubSheet(m_img, m_idx); auto &p = s.pixels; - const auto src = p.data() + m_deletePos; - const auto dst1 = p.data() + m_deletePos + m_deleteSz; + const auto src = &p[m_deletePos]; + const auto dst1 = &p[m_deletePos + m_deleteSz]; const auto dst2 = src; const auto sz = p.size() - m_deletePos - m_deleteSz; ox::memmove(dst1, src, sz); diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/inserttilescommand.cpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/inserttilescommand.cpp index d4a24379..025cd3c8 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/inserttilescommand.cpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/inserttilescommand.cpp @@ -22,7 +22,7 @@ core::InsertTilesCommand::InsertTilesCommand( auto &s = getSubSheet(m_img, m_idx); auto &p = s.pixels; auto dst = m_deletedPixels.data(); - auto src = p.data() + p.size() - m_insertCnt; + auto src = &p[p.size() - m_insertCnt]; const auto sz = m_insertCnt * sizeof(decltype(p[0])); ox::memcpy(dst, src, sz); } @@ -32,8 +32,8 @@ ox::Error InsertTilesCommand::redo() noexcept { auto &s = getSubSheet(m_img, m_idx); auto &p = s.pixels; auto dstPos = m_insertPos + m_insertCnt; - const auto dst = p.data() + dstPos; - const auto src = p.data() + m_insertPos; + auto const dst = &p[dstPos]; + auto const src = &p[m_insertPos]; ox::memmove(dst, src, p.size() - dstPos); ox::memset(src, 0, m_insertCnt * sizeof(decltype(p[0]))); return {}; @@ -42,11 +42,11 @@ ox::Error InsertTilesCommand::redo() noexcept { ox::Error InsertTilesCommand::undo() noexcept { auto &s = getSubSheet(m_img, m_idx); auto &p = s.pixels; - const auto srcIdx = m_insertPos + m_insertCnt; - const auto src = p.data() + srcIdx; - const auto dst1 = p.data() + m_insertPos; - const auto dst2 = p.data() + p.size() - m_insertCnt; - const auto sz = p.size() - srcIdx; + auto const srcIdx = m_insertPos + m_insertCnt; + auto const src = &p[srcIdx]; + auto const dst1 = &p[m_insertPos]; + auto const dst2 = &p[p.size() - m_insertCnt]; + auto const sz = p.size() - srcIdx; ox::memmove(dst1, src, sz); ox::memcpy(dst2, m_deletedPixels.data(), m_deletedPixels.size()); return {}; diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditormodel.cpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditormodel.cpp index 740ea19f..15811e77 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditormodel.cpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditormodel.cpp @@ -120,7 +120,7 @@ ox::StringView TileSheetEditorModel::palPath() const noexcept { } constexpr ox::StringView uuidPrefix = "uuid://"; if (ox::beginsWith(path, uuidPrefix)) { - auto uuid = ox::StringView(path.data() + uuidPrefix.bytes(), path.bytes() - uuidPrefix.bytes()); + auto uuid = ox::StringView(&path[uuidPrefix.bytes()], path.bytes() - uuidPrefix.bytes()); auto out = keelCtx(m_tctx).uuidToPath.at(uuid); if (out.error) { return {}; @@ -197,7 +197,7 @@ void TileSheetEditorModel::fill(ox::Point const&pt, int palIdx) noexcept { if (pt.x >= s.columns * TileWidth || pt.y >= s.rows * TileHeight) { return; } - getFillPixels(updateMap.data(), pt, oldColor); + getFillPixels(updateMap, pt, oldColor); ox::Vector idxList; auto i = core::idx(s, pt) / PixelsPerTile * PixelsPerTile; for (auto u : updateMap) { @@ -281,7 +281,7 @@ bool TileSheetEditorModel::pixelSelected(std::size_t idx) const noexcept { return m_selection && m_selection->contains(pt); } -void TileSheetEditorModel::getFillPixels(bool *pixels, ox::Point const&pt, int oldColor) const noexcept { +void TileSheetEditorModel::getFillPixels(ox::Span pixels, ox::Point const&pt, int oldColor) const noexcept { const auto &activeSubSheet = this->activeSubSheet(); const auto tileIdx = [activeSubSheet](const ox::Point &pt) noexcept { return ptToIdx(pt, activeSubSheet.columns) / PixelsPerTile; diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditormodel.hpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditormodel.hpp index b3b508cc..fcceb58b 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditormodel.hpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditormodel.hpp @@ -128,7 +128,7 @@ class TileSheetEditorModel: public ox::SignalHandler { bool pixelSelected(std::size_t idx) const noexcept; private: - void getFillPixels(bool *pixels, ox::Point const&pt, int oldColor) const noexcept; + void getFillPixels(ox::Span pixels, ox::Point const&pt, int oldColor) const noexcept; void pushCommand(studio::UndoCommand *cmd) noexcept; diff --git a/src/nostalgia/modules/core/test/tests.cpp b/src/nostalgia/modules/core/test/tests.cpp index 0a8bbd5d..fbe6e228 100644 --- a/src/nostalgia/modules/core/test/tests.cpp +++ b/src/nostalgia/modules/core/test/tests.cpp @@ -24,9 +24,10 @@ static std::map tests = { }, }; -int main(int argc, const char **args) { +int main(int argc, const char **argv) { int retval = -1; if (argc > 0) { + auto const args = ox::SpanView{argv, static_cast(argc)}; auto const testName = ox::StringView(args[1]); if (tests.find(testName) != tests.end()) { retval = static_cast(tests[testName]());