[olympic,nostalgia] Address unsafe buffer warnings

This commit is contained in:
Gary Talent 2025-05-06 22:25:36 -05:00
parent a8c1387d5a
commit d8195d300d
15 changed files with 55 additions and 42 deletions

View File

@ -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;
}

View File

@ -36,13 +36,13 @@ OX_ALLOW_UNSAFE_BUFFERS_END
setBgStatus(*ctx, 0, true);
clearBg(*ctx, 0);
auto const serr = ox::sfmt<ox::IString<23>>("Error code: {}", static_cast<int64_t>(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) {

View File

@ -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,

View File

@ -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<size_t>(c) < colorCnt(m_pal)) {
m_selectedColorRow = static_cast<size_t>(c);
}
}
{
auto const [pg, err] = atoi(page);
auto const [pg, err] = strToInt(page);
if (!err && static_cast<size_t>(pg) < m_pal.pages.size()) {
m_page = static_cast<size_t>(pg);
}

View File

@ -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 {};
}

View File

@ -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<int>(CommandId::InsertTile);
}

View File

@ -5,12 +5,13 @@
#include <ox/claw/write.hpp>
#include <nostalgia/gfx/consts.hpp>
#include <nostalgia/gfx/gfx.hpp>
#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<GLuint>(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<GLuint>(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<float> const vbo,
ox::Vec2 const&pixSize) noexcept {
auto const x1 = static_cast<float>(pt1.x) * pixSize.x - 1.f;
auto const y1 = 1.f - static_cast<float>(pt1.y) * pixSize.y;
auto const x2 = static_cast<float>(pt2.x) * pixSize.x - 1.f;
auto const y2 = 1.f - static_cast<float>(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<float, VertexVboLength> const vertices = {x1, y1, x2, y2, redf(c), greenf(c), bluef(c)};
memcpy(vbo, vertices.data(), sizeof(vertices));
ox::spancpy(vbo, ox::SpanView<float>{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

View File

@ -7,7 +7,6 @@
#include <glutils/glutils.hpp>
#include <studio/studio.hpp>
#include <nostalgia/gfx/gfx.hpp>
#include <nostalgia/gfx/tilesheet.hpp>
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<float> vbo, ox::Vec2 const&pixSize) noexcept;
void setBufferObjects(ox::Vec2 const&paneSize, TileSheet::SubSheet const&subsheet) noexcept;

View File

@ -2,8 +2,6 @@
* Copyright 2016 - 2025 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include <nostalgia/gfx/consts.hpp>
#include <nostalgia/gfx/ptidxconv.hpp>
#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<float> const vbo,
ox::Span<GLuint> 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<float>{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<GLuint>{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<int>(i), subSheet.columns);
auto const fx = static_cast<float>(pt.x);
auto const fy = static_cast<float>(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;
}

View File

@ -6,11 +6,9 @@
#include <ox/std/vec.hpp>
#include <glutils/glutils.hpp>
#include <studio/studio.hpp>
#include <nostalgia/gfx/color.hpp>
#include <nostalgia/gfx/gfx.hpp>
#include <glutils/glutils.hpp>
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<float> vbo,
ox::Span<GLuint> ebo) const noexcept;
void setBufferObjects(ox::Vec2 const&paneS) noexcept;
void setBufferObjects(ox::Vec2 const&paneSize) noexcept;
};

View File

@ -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);

View File

@ -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<ox::String> toResult(nfdresult_t r, NFD::UniquePathN const&path) noexcept {

View File

@ -44,10 +44,10 @@ static ox::Result<std::size_t> 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<char*>(0x0a000000); current += headerLen) {
if (memcmp(current, headerP1, headerP1Len) == 0 &&
memcmp(current + headerP1Len, headerP2, headerP2Len) == 0) {

View File

@ -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());
}

View File

@ -267,7 +267,9 @@ ox::Error setWindowIcon(Context &ctx, ox::SpanView<ox::SpanView<uint8_t>> 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());
}