From 0783cdf83f98bb8e1359892fa318b93f71d10b44 Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Sun, 18 Oct 2026 13:51:41 +0200 Subject: Minor Bug Fixes --- src/config.c | 5 ++--- src/debug.c | 10 +++++----- src/env.c | 10 ++++++---- src/game.c | 3 +++ src/picow.c | 10 +++++----- src/wasmrt.c | 6 ++++-- 6 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/config.c b/src/config.c index d2bd649..91f45b8 100644 --- a/src/config.c +++ b/src/config.c @@ -67,12 +67,11 @@ static bool parse_size(char const *s, size_t *m) { long long v = SDL_strtoll(s, &end, 10); if (end == s || *end != '\0') { - SDL_SetError("invalid digit %c", *end); - return false; + return SDL_SetError("invalid digit %c", *end); } if (errno == ERANGE || v < 0 || (unsigned long long)v > (unsigned long long)SIZE_MAX) { - SDL_SetError("size out of range"); + return SDL_SetError("size out of range"); } *m = (size_t)v; diff --git a/src/debug.c b/src/debug.c index dc06c51..b87aa31 100644 --- a/src/debug.c +++ b/src/debug.c @@ -147,16 +147,16 @@ static bool debug_print(struct pw_debug *debug, float x, float y, SDL_FRect dst = {x * sx, y * sy, src.w * sx, src.h * sy}; for (int i = 0; i < n; i++) { uint8_t c = (uint8_t)text[i]; - uint8_t i = c % 32; - uint8_t j = c / 32; - src.x = (float)(i * 4); - src.y = (float)(j * 8); + uint8_t ci = c % 32; + uint8_t cj = c / 32; + src.x = (float)(ci * 4); + src.y = (float)(cj * 8); ok = ok && SDL_RenderTexture(renderer, debug->font, &src, &dst); dst.x += dst.w; } SDL_free(text); - return true; + return ok; } bool pw_debug_render(struct pw_debug *debug) { diff --git a/src/env.c b/src/env.c index 4581195..bb4f7f9 100644 --- a/src/env.c +++ b/src/env.c @@ -29,8 +29,10 @@ #define ENV_PIXELFORMAT_ABGR8888 0x86762004 struct w2c_env *pw_env_init(size_t memory) { - size_t pages = (memory << 20) / wasm2c_pico8_pagesize_env_memory; - if (pages < ENV_MIN_PAGES || pages > wasm2c_pico8_max_env_memory) { + size_t bytes = memory << 20; + size_t pages = bytes / wasm2c_pico8_pagesize_env_memory; + if (bytes < memory || pages < ENV_MIN_PAGES || + pages > wasm2c_pico8_max_env_memory) { SDL_InvalidParamError("memory"); return NULL; } @@ -57,10 +59,10 @@ struct w2c_env *pw_env_init(size_t memory) { return env; error: - if (!env->memory.data) { + if (env->memory.data) { SDL_free(env->memory.data); } - if (!env->table.data) { + if (env->table.data) { SDL_free(env->table.data); } SDL_free(env); diff --git a/src/game.c b/src/game.c index 35d98b9..fe02d6f 100644 --- a/src/game.c +++ b/src/game.c @@ -53,6 +53,9 @@ void pw_game_free(struct pw_game *game) { return; } + if (game->carts) { + pw_multicart_free(game->carts); + } pw_input_free(game->input); pw_fb_free(game->fb); pw_env_free(game->env); diff --git a/src/picow.c b/src/picow.c index ef4b726..a998307 100644 --- a/src/picow.c +++ b/src/picow.c @@ -195,10 +195,6 @@ SDL_AppResult SDL_AppIterate(void *appstate) { } void SDL_AppQuit(void *appstate, SDL_AppResult result) { - if (result != SDL_APP_SUCCESS) { - SDL_LogCritical(0, "application error: %s", SDL_GetError()); - } - struct pw_appstate *state = appstate; if (!state) { goto bye; @@ -216,5 +212,9 @@ void SDL_AppQuit(void *appstate, SDL_AppResult result) { SDL_free(state); bye: - SDL_Log("bye"); + if (result == SDL_APP_SUCCESS) { + SDL_Log("bye"); + } else { + SDL_LogCritical(0, "application error"); + } } diff --git a/src/wasmrt.c b/src/wasmrt.c index fce7ac8..a1daaf7 100644 --- a/src/wasmrt.c +++ b/src/wasmrt.c @@ -76,8 +76,10 @@ void wasm_rt_trap_unregister(void) { void wasm_rt_memory_check(struct wasm_rt_memory_t const *mem, uint32_t addr, uint32_t len) { + // While memory address 0 is _technically_ allowed in Wasm, we reject it in + // our memory bounds checking, as nothing should ask us to write to it. uint32_t end = addr + len; - if (UNLIKELY(!addr || end < addr || end >= mem->size)) { + if (UNLIKELY(!addr || end < addr || end > mem->size)) { wasm_rt_trap(WASM_RT_TRAP_OOB); } } @@ -153,7 +155,7 @@ void wasm_rt_memory_store8_unchecked(struct wasm_rt_memory_t *mem, #if SDL_BYTEORDER == SDL_LIL_ENDIAN mem->data[addr] = value; #else - mem->data[size - addr - sizeof(value)] = value; + mem->data[mem->size - addr - sizeof(value)] = value; #endif } -- cgit v1.2.3