From d8195d300ddce205e398e827820c2393cc5b75a7 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 6 May 2025 22:25:36 -0500 Subject: [PATCH] [olympic,nostalgia] Address unsafe buffer warnings --- .../modules/gfx/include/nostalgia/gfx/gfx.hpp | 2 +- src/nostalgia/modules/gfx/src/gba/panic.cpp | 10 +++---- src/nostalgia/modules/gfx/src/gfx.cpp | 2 +- .../paletteeditor/paletteeditor-imgui.cpp | 4 +-- .../commands/deletetilescommand.cpp | 4 +++ .../commands/inserttilescommand.cpp | 4 +++ .../tilesheeteditor/tilesheetpixelgrid.cpp | 26 +++++++++++-------- .../tilesheeteditor/tilesheetpixelgrid.hpp | 3 +-- .../tilesheeteditor/tilesheetpixels.cpp | 18 ++++++------- .../tilesheeteditor/tilesheetpixels.hpp | 14 +++++----- src/nostalgia/player/app.cpp | 2 +- .../studio/modlib/src/filedialog_nfd.cpp | 2 ++ src/olympic/turbine/src/gba/turbine.cpp | 2 +- src/olympic/turbine/src/glfw/clipboard.cpp | 2 ++ src/olympic/turbine/src/glfw/gfx.cpp | 2 ++ 15 files changed, 55 insertions(+), 42 deletions(-) diff --git a/src/nostalgia/modules/gfx/include/nostalgia/gfx/gfx.hpp b/src/nostalgia/modules/gfx/include/nostalgia/gfx/gfx.hpp index 86516107..2850bb82 100644 --- a/src/nostalgia/modules/gfx/include/nostalgia/gfx/gfx.hpp +++ b/src/nostalgia/modules/gfx/include/nostalgia/gfx/gfx.hpp @@ -238,7 +238,7 @@ uint_t spriteCount(Context &ctx) noexcept; ox::Error initConsole(Context &ctx) noexcept; -void puts(Context &ctx, int column, int row, ox::StringViewCR str) noexcept; +void consoleWrite(Context &ctx, int column, int row, ox::StringViewCR str) noexcept; } diff --git a/src/nostalgia/modules/gfx/src/gba/panic.cpp b/src/nostalgia/modules/gfx/src/gba/panic.cpp index 106bd383..1b886e27 100644 --- a/src/nostalgia/modules/gfx/src/gba/panic.cpp +++ b/src/nostalgia/modules/gfx/src/gba/panic.cpp @@ -36,13 +36,13 @@ OX_ALLOW_UNSAFE_BUFFERS_END setBgStatus(*ctx, 0, true); clearBg(*ctx, 0); auto const serr = ox::sfmt>("Error code: {}", static_cast(err)); - puts(*ctx, 32 + 1, 1, "SADNESS..."); - puts(*ctx, 32 + 1, 4, "UNEXPECTED STATE:"); - puts(*ctx, 32 + 2, 6, panicMsg); + consoleWrite(*ctx, 32 + 1, 1, "SADNESS..."); + consoleWrite(*ctx, 32 + 1, 4, "UNEXPECTED STATE:"); + consoleWrite(*ctx, 32 + 2, 6, panicMsg); if (err) { - puts(*ctx, 32 + 2, 8, serr); + consoleWrite(*ctx, 32 + 2, 8, serr); } - puts(*ctx, 32 + 1, 15, "PLEASE RESTART THE SYSTEM"); + consoleWrite(*ctx, 32 + 1, 15, "PLEASE RESTART THE SYSTEM"); // print to terminal if in mGBA oxErrf("\033[31;1;1mPANIC:\033[0m [{}:{}]: {}\n", file, line, panicMsg); if (err.msg) { diff --git a/src/nostalgia/modules/gfx/src/gfx.cpp b/src/nostalgia/modules/gfx/src/gfx.cpp index 8509fa8f..cc3d97e6 100644 --- a/src/nostalgia/modules/gfx/src/gfx.cpp +++ b/src/nostalgia/modules/gfx/src/gfx.cpp @@ -251,7 +251,7 @@ ox::Error initConsole(Context &ctx) noexcept { return loadBgPalette(ctx, 0, PaletteAddr); } -void puts( +void consoleWrite( Context &ctx, int const column, int const row, diff --git a/src/nostalgia/modules/gfx/src/studio/paletteeditor/paletteeditor-imgui.cpp b/src/nostalgia/modules/gfx/src/studio/paletteeditor/paletteeditor-imgui.cpp index a0c24ab3..e54b7ffc 100644 --- a/src/nostalgia/modules/gfx/src/studio/paletteeditor/paletteeditor-imgui.cpp +++ b/src/nostalgia/modules/gfx/src/studio/paletteeditor/paletteeditor-imgui.cpp @@ -105,13 +105,13 @@ void PaletteEditorImGui::navigateTo(ox::StringViewCR arg) noexcept { auto const &color = args[0]; auto const &page = args[1]; { - auto const [c, err] = atoi(color); + auto const [c, err] = strToInt(color); if (!err && static_cast(c) < colorCnt(m_pal)) { m_selectedColorRow = static_cast(c); } } { - auto const [pg, err] = atoi(page); + auto const [pg, err] = strToInt(page); if (!err && static_cast(pg) < m_pal.pages.size()) { m_page = static_cast(pg); } diff --git a/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/commands/deletetilescommand.cpp b/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/commands/deletetilescommand.cpp index 9be5a29c..e7cf50ae 100644 --- a/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/commands/deletetilescommand.cpp +++ b/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/commands/deletetilescommand.cpp @@ -35,8 +35,10 @@ ox::Error gfx::DeleteTilesCommand::redo() noexcept { auto const src = &p[srcPos]; auto const dst1 = &p[m_deletePos]; auto const dst2 = &p[(p.size() - m_deleteSz)]; + OX_ALLOW_UNSAFE_BUFFERS_BEGIN ox::memmove(dst1, src, p.size() - srcPos); ox::memset(dst2, 0, m_deleteSz * sizeof(decltype(p[0]))); + OX_ALLOW_UNSAFE_BUFFERS_END return {}; } @@ -47,8 +49,10 @@ ox::Error DeleteTilesCommand::undo() noexcept { auto const dst1 = &p[m_deletePos + m_deleteSz]; auto const dst2 = src; auto const sz = p.size() - m_deletePos - m_deleteSz; + OX_ALLOW_UNSAFE_BUFFERS_BEGIN ox::memmove(dst1, src, sz); ox::memcpy(dst2, m_deletedPixels.data(), m_deletedPixels.size()); + OX_ALLOW_UNSAFE_BUFFERS_END return {}; } diff --git a/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/commands/inserttilescommand.cpp b/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/commands/inserttilescommand.cpp index 1c9ce146..9b861873 100644 --- a/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/commands/inserttilescommand.cpp +++ b/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/commands/inserttilescommand.cpp @@ -27,6 +27,8 @@ InsertTilesCommand::InsertTilesCommand( } } +OX_ALLOW_UNSAFE_BUFFERS_BEGIN + ox::Error InsertTilesCommand::redo() noexcept { auto &s = getSubSheet(m_img, m_idx); auto &p = s.pixels; @@ -55,6 +57,8 @@ ox::Error InsertTilesCommand::undo() noexcept { return {}; } +OX_ALLOW_UNSAFE_BUFFERS_END + int InsertTilesCommand::commandId() const noexcept { return static_cast(CommandId::InsertTile); } diff --git a/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheetpixelgrid.cpp b/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheetpixelgrid.cpp index 5abb0b21..914c7c6a 100644 --- a/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheetpixelgrid.cpp +++ b/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheetpixelgrid.cpp @@ -5,12 +5,13 @@ #include #include +#include #include "tilesheetpixelgrid.hpp" namespace nostalgia::gfx { -void TileSheetGrid::setPixelSizeMod(float sm) noexcept { +void TileSheetGrid::setPixelSizeMod(float const sm) noexcept { m_pixelSizeMod = sm; } @@ -18,10 +19,11 @@ ox::Error TileSheetGrid::buildShader() noexcept { auto const pixelLineVshad = ox::sfmt(VShad, gl::GlslVersion); auto const pixelLineFshad = ox::sfmt(FShad, gl::GlslVersion); auto const pixelLineGshad = ox::sfmt(GShad, gl::GlslVersion); - return glutils::buildShaderProgram(pixelLineVshad, pixelLineFshad, pixelLineGshad).moveTo(m_shader); + return glutils::buildShaderProgram( + pixelLineVshad, pixelLineFshad, pixelLineGshad).moveTo(m_shader); } -void TileSheetGrid::draw(bool update, ox::Vec2 const&scroll) noexcept { +void TileSheetGrid::draw(bool const update, ox::Vec2 const&scroll) noexcept { // the lines just show up bigger on Windows for some reason if constexpr(ox::defines::OS == ox::OS::Windows) { glLineWidth(3 * m_pixelSizeMod * 0.25f); @@ -51,7 +53,8 @@ void TileSheetGrid::initBufferSet(ox::Vec2 const&paneSize, TileSheet::SubSheet c // vbo layout auto const pt1Attr = static_cast(glGetAttribLocation(m_shader, "vPt1")); glEnableVertexAttribArray(pt1Attr); - glVertexAttribPointer(pt1Attr, 2, GL_FLOAT, GL_FALSE, VertexVboRowLength * sizeof(float), nullptr); + glVertexAttribPointer( + pt1Attr, 2, GL_FLOAT, GL_FALSE, VertexVboRowLength * sizeof(float), nullptr); auto const pt2Attr = static_cast(glGetAttribLocation(m_shader, "vPt2")); glEnableVertexAttribArray(pt2Attr); glVertexAttribPointer(pt2Attr, 2, GL_FLOAT, GL_FALSE, VertexVboRowLength * sizeof(float), @@ -70,18 +73,18 @@ void TileSheetGrid::update(ox::Vec2 const&paneSize, TileSheet::SubSheet const&su } void TileSheetGrid::setBufferObject( - ox::Point pt1, - ox::Point pt2, - Color32 c, - float *vbo, + ox::Point const pt1, + ox::Point const pt2, + Color32 const c, + ox::Span const vbo, ox::Vec2 const&pixSize) noexcept { auto const x1 = static_cast(pt1.x) * pixSize.x - 1.f; auto const y1 = 1.f - static_cast(pt1.y) * pixSize.y; auto const x2 = static_cast(pt2.x) * pixSize.x - 1.f; auto const y2 = 1.f - static_cast(pt2.y) * pixSize.y; - // don't worry, this memcpy gets optimized to something much more ideal + // don't worry, this gets optimized to something much more ideal ox::Array const vertices = {x1, y1, x2, y2, redf(c), greenf(c), bluef(c)}; - memcpy(vbo, vertices.data(), sizeof(vertices)); + ox::spancpy(vbo, ox::SpanView{vertices}); } void TileSheetGrid::setBufferObjects(ox::Vec2 const&paneSize, TileSheet::SubSheet const&subsheet) noexcept { @@ -92,7 +95,8 @@ void TileSheetGrid::setBufferObjects(ox::Vec2 const&paneSize, TileSheet::SubShee } auto const pixSize = pixelSize(paneSize); auto const set = [&](std::size_t i, ox::Point pt1, ox::Point pt2, Color32 c) { - auto const vbo = &m_bufferSet.vertices[i * VertexVboLength]; + auto const idx = i * VertexVboLength; + auto const vbo = ox::Span{m_bufferSet.vertices} + idx; setBufferObject(pt1, pt2, c, vbo, pixSize); }; // set buffer length diff --git a/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheetpixelgrid.hpp b/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheetpixelgrid.hpp index 6a08895a..828518e2 100644 --- a/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheetpixelgrid.hpp +++ b/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheetpixelgrid.hpp @@ -7,7 +7,6 @@ #include #include -#include #include namespace nostalgia::gfx { @@ -74,7 +73,7 @@ class TileSheetGrid { void update(ox::Vec2 const&paneSize, TileSheet::SubSheet const&subsheet) noexcept; private: - static void setBufferObject(ox::Point pt1, ox::Point pt2, Color32 c, float *vbo, ox::Vec2 const&pixSize) noexcept; + static void setBufferObject(ox::Point pt1, ox::Point pt2, Color32 c, ox::Span vbo, ox::Vec2 const&pixSize) noexcept; void setBufferObjects(ox::Vec2 const&paneSize, TileSheet::SubSheet const&subsheet) noexcept; diff --git a/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheetpixels.cpp b/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheetpixels.cpp index 22a01943..2388c318 100644 --- a/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheetpixels.cpp +++ b/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheetpixels.cpp @@ -2,8 +2,6 @@ * Copyright 2016 - 2025 Gary Talent (gary@drinkingtea.net). All rights reserved. */ -#include -#include #include "tilesheeteditormodel.hpp" #include "tilesheetpixels.hpp" @@ -88,11 +86,11 @@ ox::Vec2 TileSheetPixels::pixelSize(ox::Vec2 const&paneSize) const noexcept { void TileSheetPixels::setPixelBufferObject( ox::Vec2 const&paneSize, - unsigned vertexRow, + unsigned const vertexRow, float x, float y, - Color16 color, - float *vbo, - GLuint *ebo) const noexcept { + Color16 const color, + ox::Span const vbo, + ox::Span const ebo) const noexcept { auto const [xmod, ymod] = pixelSize(paneSize); x *= xmod; y *= -ymod; @@ -106,12 +104,12 @@ void TileSheetPixels::setPixelBufferObject( x + xmod, y + ymod, r, g, b, // top right x, y + ymod, r, g, b, // top left }; - memcpy(vbo, vertices.data(), sizeof(vertices)); + ox::spancpy(vbo, ox::SpanView{vertices}); std::array const elms = { vertexRow + 0, vertexRow + 1, vertexRow + 2, vertexRow + 2, vertexRow + 3, vertexRow + 0, }; - memcpy(ebo, elms.data(), sizeof(elms)); + ox::spancpy(ebo, ox::SpanView{elms}); } void TileSheetPixels::setBufferObjects(ox::Vec2 const&paneSize) noexcept { @@ -137,8 +135,8 @@ void TileSheetPixels::setBufferObjects(ox::Vec2 const&paneSize) noexcept { auto const pt = idxToPt(static_cast(i), subSheet.columns); auto const fx = static_cast(pt.x); auto const fy = static_cast(pt.y); - auto const vbo = &m_bufferSet.vertices[i * vboLen]; - auto const ebo = &m_bufferSet.elements[i * VertexEboLength]; + auto const vbo = ox::Span{m_bufferSet.vertices} + i * vboLen; + auto const ebo = ox::Span{m_bufferSet.elements} + i * VertexEboLength; if (i * vboLen + vboLen > m_bufferSet.vertices.size()) { return; } diff --git a/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheetpixels.hpp b/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheetpixels.hpp index 117b2a90..9d79087b 100644 --- a/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheetpixels.hpp +++ b/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheetpixels.hpp @@ -6,11 +6,9 @@ #include -#include -#include - #include -#include + +#include namespace nostalgia::gfx { @@ -44,15 +42,15 @@ class TileSheetPixels { private: void setPixelBufferObject( - ox::Vec2 const&paneS, + ox::Vec2 const&paneSize, unsigned vertexRow, float x, float y, Color16 color, - float *vbo, - GLuint *ebo) const noexcept; + ox::Span vbo, + ox::Span ebo) const noexcept; - void setBufferObjects(ox::Vec2 const&paneS) noexcept; + void setBufferObjects(ox::Vec2 const&paneSize) noexcept; }; diff --git a/src/nostalgia/player/app.cpp b/src/nostalgia/player/app.cpp index f43a6bf3..7caef452 100644 --- a/src/nostalgia/player/app.cpp +++ b/src/nostalgia/player/app.cpp @@ -69,7 +69,7 @@ static ox::Error runTest(turbine::Context &tctx) { OX_RETURN_ERROR(gfx::loadSpriteTileSheet(*cctx, "/TileSheets/Charset.nts")); OX_RETURN_ERROR(gfx::loadSpritePalette(*cctx, "/Palettes/Chester.npal")); OX_RETURN_ERROR(gfx::initConsole(*cctx)); - gfx::puts(*cctx, 10, 9, "DOPENESS!!!"); + gfx::consoleWrite(*cctx, 10, 9, "DOPENESS!!!"); turbine::setUpdateHandler(tctx, testUpdateHandler); turbine::setKeyEventHandler(tctx, testKeyEventHandler); return turbine::run(tctx); diff --git a/src/olympic/studio/modlib/src/filedialog_nfd.cpp b/src/olympic/studio/modlib/src/filedialog_nfd.cpp index ac9707e6..0869761f 100644 --- a/src/olympic/studio/modlib/src/filedialog_nfd.cpp +++ b/src/olympic/studio/modlib/src/filedialog_nfd.cpp @@ -12,10 +12,12 @@ namespace studio { FDFilterItem::FDFilterItem(ox::StringViewCR pName, ox::StringViewCR pSpec) noexcept { + OX_ALLOW_UNSAFE_BUFFERS_BEGIN name.resize(pName.len() + 1); ox::strncpy(name.data(), pName.data(), pName.len()); spec.resize(pSpec.len() + 1); ox::strncpy(spec.data(), pSpec.data(), pSpec.len()); + OX_ALLOW_UNSAFE_BUFFERS_END } static ox::Result toResult(nfdresult_t r, NFD::UniquePathN const&path) noexcept { diff --git a/src/olympic/turbine/src/gba/turbine.cpp b/src/olympic/turbine/src/gba/turbine.cpp index 6caf007f..6c3cd24f 100644 --- a/src/olympic/turbine/src/gba/turbine.cpp +++ b/src/olympic/turbine/src/gba/turbine.cpp @@ -44,10 +44,10 @@ static ox::Result findPreloadSection() noexcept { // media section constexpr auto headerP2 = "DER_____________"; constexpr auto headerP1 = "KEEL_PRELOAD_HEA"; +OX_ALLOW_UNSAFE_BUFFERS_BEGIN constexpr auto headerP1Len = ox::strlen(headerP2); constexpr auto headerP2Len = ox::strlen(headerP1); constexpr auto headerLen = headerP1Len + headerP2Len; -OX_ALLOW_UNSAFE_BUFFERS_BEGIN for (auto current = MEM_ROM; current < reinterpret_cast(0x0a000000); current += headerLen) { if (memcmp(current, headerP1, headerP1Len) == 0 && memcmp(current + headerP1Len, headerP2, headerP2Len) == 0) { diff --git a/src/olympic/turbine/src/glfw/clipboard.cpp b/src/olympic/turbine/src/glfw/clipboard.cpp index 88e5238a..edad904e 100644 --- a/src/olympic/turbine/src/glfw/clipboard.cpp +++ b/src/olympic/turbine/src/glfw/clipboard.cpp @@ -18,7 +18,9 @@ ox::String getClipboardText(Context &ctx) noexcept { void setClipboardText(Context &ctx, ox::StringViewCR text) noexcept { auto cstr = ox_malloca(text.bytes() + 1, char); + OX_ALLOW_UNSAFE_BUFFERS_BEGIN ox::strncpy(cstr.get(), text.data(), text.bytes()); + OX_ALLOW_UNSAFE_BUFFERS_END glfwSetClipboardString(ctx.window, cstr.get()); } diff --git a/src/olympic/turbine/src/glfw/gfx.cpp b/src/olympic/turbine/src/glfw/gfx.cpp index 01a8df23..0e1177bb 100644 --- a/src/olympic/turbine/src/glfw/gfx.cpp +++ b/src/olympic/turbine/src/glfw/gfx.cpp @@ -267,7 +267,9 @@ ox::Error setWindowIcon(Context &ctx, ox::SpanView> const void setWindowTitle(Context &ctx, ox::StringViewCR title) noexcept { auto cstr = ox_malloca(title.bytes() + 1, char); + OX_ALLOW_UNSAFE_BUFFERS_BEGIN ox::strncpy(cstr.get(), title.data(), title.bytes()); + OX_ALLOW_UNSAFE_BUFFERS_END glfwSetWindowTitle(ctx.window, cstr.get()); }