[nostalgia] Enable warnings for unsafe buffers

This commit is contained in:
Gary Talent 2024-11-27 00:14:57 -06:00
parent 86b9f9316e
commit 6af00d9a2e
9 changed files with 38 additions and 25 deletions

View File

@ -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(nostalgia CXX)
#project packages #project packages

View File

@ -17,6 +17,8 @@
#include "context.hpp" #include "context.hpp"
OX_ALLOW_UNSAFE_BUFFERS_BEGIN
namespace nostalgia::core { namespace nostalgia::core {
static constexpr auto SpriteCount = 128; static constexpr auto SpriteCount = 128;
@ -283,3 +285,5 @@ uint_t spriteCount(Context&) noexcept {
} }
} }
OX_ALLOW_UNSAFE_BUFFERS_END

View File

@ -25,7 +25,9 @@ using namespace nostalgia::core;
void panic(const char *file, int line, const char *panicMsg, ox::Error const&err) noexcept { 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 // 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::heapmgr::initHeap(HEAP_BEGIN, HEAP_END);
OX_ALLOW_UNSAFE_BUFFERS_END
auto tctx = turbine::init(keel::loadRomFs("").unwrap(), "Nostalgia").unwrap(); auto tctx = turbine::init(keel::loadRomFs("").unwrap(), "Nostalgia").unwrap();
auto ctx = init(*tctx).unwrap(); auto ctx = init(*tctx).unwrap();
std::ignore = initGfx(*ctx, {}); std::ignore = initGfx(*ctx, {});

View File

@ -554,20 +554,21 @@ static ox::Result<TileSheetData> buildSetTsd(
static void copyPixels( static void copyPixels(
CompactTileSheet const&ts, CompactTileSheet const&ts,
uint32_t *dst, ox::Span<uint32_t> dst,
size_t const srcPxIdx, size_t const srcPxIdx,
size_t pxlCnt) noexcept { size_t pxlCnt) noexcept {
size_t idx{};
if (ts.bpp == 4) { if (ts.bpp == 4) {
for (size_t i = 0; i < pxlCnt; i += 2) { for (size_t i = 0; i < pxlCnt; i += 2) {
auto const [a, b] = get2Pixels4Bpp(ts, i + srcPxIdx); auto const [a, b] = get2Pixels4Bpp(ts, i + srcPxIdx);
*(dst++) = a; dst[idx++] = a;
*(dst++) = b; dst[idx++] = b;
} }
} else if (ts.bpp == 8) { } else if (ts.bpp == 8) {
for (size_t i = 0; i < pxlCnt; i += 2) { for (size_t i = 0; i < pxlCnt; i += 2) {
auto const [a, b] = get2Pixels8Bpp(ts, i + srcPxIdx); auto const [a, b] = get2Pixels8Bpp(ts, i + srcPxIdx);
*(dst++) = a; dst[idx++] = a;
*(dst++) = b; dst[idx++] = b;
} }
} }
} }
@ -587,7 +588,7 @@ ox::Error loadBgTileSheet(
if (dstPxIdx + pxlCnt >= cbbPxls.size()) { if (dstPxIdx + pxlCnt >= cbbPxls.size()) {
return OxError(1, "video mem dst overflow"); return OxError(1, "video mem dst overflow");
} }
auto const dst = &cbbPxls[dstPxIdx]; auto const dst = ox::Span{cbbPxls} + dstPxIdx;
copyPixels(ts, dst, srcPxIdx, pxlCnt); copyPixels(ts, dst, srcPxIdx, pxlCnt);
auto const cbbTiles = cbbPxls.size() / bytesPerTile; auto const cbbTiles = cbbPxls.size() / bytesPerTile;
int constexpr cbbWidth = 8; int constexpr cbbWidth = 8;

View File

@ -22,7 +22,7 @@ core::DeleteTilesCommand::DeleteTilesCommand(
auto &s = getSubSheet(m_img, m_idx); auto &s = getSubSheet(m_img, m_idx);
auto &p = s.pixels; auto &p = s.pixels;
auto dst = m_deletedPixels.data(); 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])); const auto sz = m_deleteSz * sizeof(decltype(p[0]));
ox::memcpy(dst, src, sz); ox::memcpy(dst, src, sz);
} }
@ -32,9 +32,9 @@ ox::Error core::DeleteTilesCommand::redo() noexcept {
auto &s = getSubSheet(m_img, m_idx); auto &s = getSubSheet(m_img, m_idx);
auto &p = s.pixels; auto &p = s.pixels;
auto srcPos = m_deletePos + m_deleteSz; auto srcPos = m_deletePos + m_deleteSz;
const auto src = p.data() + srcPos; const auto src = &p[srcPos];
const auto dst1 = p.data() + m_deletePos; const auto dst1 = &p[m_deletePos];
const auto dst2 = p.data() + (p.size() - m_deleteSz); const auto dst2 = &p[(p.size() - m_deleteSz)];
ox::memmove(dst1, src, p.size() - srcPos); ox::memmove(dst1, src, p.size() - srcPos);
ox::memset(dst2, 0, m_deleteSz * sizeof(decltype(p[0]))); ox::memset(dst2, 0, m_deleteSz * sizeof(decltype(p[0])));
return {}; return {};
@ -43,8 +43,8 @@ ox::Error core::DeleteTilesCommand::redo() noexcept {
ox::Error DeleteTilesCommand::undo() noexcept { ox::Error DeleteTilesCommand::undo() noexcept {
auto &s = getSubSheet(m_img, m_idx); auto &s = getSubSheet(m_img, m_idx);
auto &p = s.pixels; auto &p = s.pixels;
const auto src = p.data() + m_deletePos; const auto src = &p[m_deletePos];
const auto dst1 = p.data() + m_deletePos + m_deleteSz; const auto dst1 = &p[m_deletePos + m_deleteSz];
const auto dst2 = src; const auto dst2 = src;
const auto sz = p.size() - m_deletePos - m_deleteSz; const auto sz = p.size() - m_deletePos - m_deleteSz;
ox::memmove(dst1, src, sz); ox::memmove(dst1, src, sz);

View File

@ -22,7 +22,7 @@ core::InsertTilesCommand::InsertTilesCommand(
auto &s = getSubSheet(m_img, m_idx); auto &s = getSubSheet(m_img, m_idx);
auto &p = s.pixels; auto &p = s.pixels;
auto dst = m_deletedPixels.data(); 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])); const auto sz = m_insertCnt * sizeof(decltype(p[0]));
ox::memcpy(dst, src, sz); ox::memcpy(dst, src, sz);
} }
@ -32,8 +32,8 @@ ox::Error InsertTilesCommand::redo() noexcept {
auto &s = getSubSheet(m_img, m_idx); auto &s = getSubSheet(m_img, m_idx);
auto &p = s.pixels; auto &p = s.pixels;
auto dstPos = m_insertPos + m_insertCnt; auto dstPos = m_insertPos + m_insertCnt;
const auto dst = p.data() + dstPos; auto const dst = &p[dstPos];
const auto src = p.data() + m_insertPos; auto const src = &p[m_insertPos];
ox::memmove(dst, src, p.size() - dstPos); ox::memmove(dst, src, p.size() - dstPos);
ox::memset(src, 0, m_insertCnt * sizeof(decltype(p[0]))); ox::memset(src, 0, m_insertCnt * sizeof(decltype(p[0])));
return {}; return {};
@ -42,11 +42,11 @@ ox::Error InsertTilesCommand::redo() noexcept {
ox::Error InsertTilesCommand::undo() noexcept { ox::Error InsertTilesCommand::undo() noexcept {
auto &s = getSubSheet(m_img, m_idx); auto &s = getSubSheet(m_img, m_idx);
auto &p = s.pixels; auto &p = s.pixels;
const auto srcIdx = m_insertPos + m_insertCnt; auto const srcIdx = m_insertPos + m_insertCnt;
const auto src = p.data() + srcIdx; auto const src = &p[srcIdx];
const auto dst1 = p.data() + m_insertPos; auto const dst1 = &p[m_insertPos];
const auto dst2 = p.data() + p.size() - m_insertCnt; auto const dst2 = &p[p.size() - m_insertCnt];
const auto sz = p.size() - srcIdx; auto const sz = p.size() - srcIdx;
ox::memmove(dst1, src, sz); ox::memmove(dst1, src, sz);
ox::memcpy(dst2, m_deletedPixels.data(), m_deletedPixels.size()); ox::memcpy(dst2, m_deletedPixels.data(), m_deletedPixels.size());
return {}; return {};

View File

@ -120,7 +120,7 @@ ox::StringView TileSheetEditorModel::palPath() const noexcept {
} }
constexpr ox::StringView uuidPrefix = "uuid://"; constexpr ox::StringView uuidPrefix = "uuid://";
if (ox::beginsWith(path, uuidPrefix)) { 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); auto out = keelCtx(m_tctx).uuidToPath.at(uuid);
if (out.error) { if (out.error) {
return {}; 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) { if (pt.x >= s.columns * TileWidth || pt.y >= s.rows * TileHeight) {
return; return;
} }
getFillPixels(updateMap.data(), pt, oldColor); getFillPixels(updateMap, pt, oldColor);
ox::Vector<std::size_t> idxList; ox::Vector<std::size_t> idxList;
auto i = core::idx(s, pt) / PixelsPerTile * PixelsPerTile; auto i = core::idx(s, pt) / PixelsPerTile * PixelsPerTile;
for (auto u : updateMap) { for (auto u : updateMap) {
@ -281,7 +281,7 @@ bool TileSheetEditorModel::pixelSelected(std::size_t idx) const noexcept {
return m_selection && m_selection->contains(pt); 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<bool> pixels, ox::Point const&pt, int oldColor) const noexcept {
const auto &activeSubSheet = this->activeSubSheet(); const auto &activeSubSheet = this->activeSubSheet();
const auto tileIdx = [activeSubSheet](const ox::Point &pt) noexcept { const auto tileIdx = [activeSubSheet](const ox::Point &pt) noexcept {
return ptToIdx(pt, activeSubSheet.columns) / PixelsPerTile; return ptToIdx(pt, activeSubSheet.columns) / PixelsPerTile;

View File

@ -128,7 +128,7 @@ class TileSheetEditorModel: public ox::SignalHandler {
bool pixelSelected(std::size_t idx) const noexcept; bool pixelSelected(std::size_t idx) const noexcept;
private: private:
void getFillPixels(bool *pixels, ox::Point const&pt, int oldColor) const noexcept; void getFillPixels(ox::Span<bool> pixels, ox::Point const&pt, int oldColor) const noexcept;
void pushCommand(studio::UndoCommand *cmd) noexcept; void pushCommand(studio::UndoCommand *cmd) noexcept;

View File

@ -24,9 +24,10 @@ static std::map<ox::StringView, ox::Error(*)()> tests = {
}, },
}; };
int main(int argc, const char **args) { int main(int argc, const char **argv) {
int retval = -1; int retval = -1;
if (argc > 0) { if (argc > 0) {
auto const args = ox::SpanView{argv, static_cast<size_t>(argc)};
auto const testName = ox::StringView(args[1]); auto const testName = ox::StringView(args[1]);
if (tests.find(testName) != tests.end()) { if (tests.find(testName) != tests.end()) {
retval = static_cast<int>(tests[testName]()); retval = static_cast<int>(tests[testName]());