diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-29 13:22:28 +0100 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-29 13:33:26 +0100 |
| commit | 838d75bce98ca5a1f90f762e80d2aa649d4e0642 (patch) | |
| tree | 06d661dd944d80632aa80a9f605a679779575dc5 /src | |
| parent | e51027a576a07ffb41b6e7d2bfc3aade6ae091d7 (diff) | |
Minor Fixes
Diffstat (limited to 'src')
| -rw-r--r-- | src/cart.c | 59 | ||||
| -rw-r--r-- | src/cart.h | 5 | ||||
| -rw-r--r-- | src/env.c | 9 | ||||
| -rw-r--r-- | src/fs.c | 10 | ||||
| -rw-r--r-- | src/input.c | 2 | ||||
| -rw-r--r-- | src/menu.c | 25 | ||||
| -rw-r--r-- | src/menu.h | 1 | ||||
| -rw-r--r-- | src/player.c | 3 |
8 files changed, 78 insertions, 36 deletions
@@ -10,7 +10,7 @@ static bool cart_file_path(struct pw_cart_file *file, char const *dir, char const *filename, char const *ext) { if (!pw_pathjoin(file->path, PW_CART_PATH_MAX, dir, filename, NULL) || !pw_stripext(file->name, PW_CART_NAME_MAX, filename, ext)) { - SDL_SetError("invalid cart file name %s", filename); + SDL_SetError("invalid file name %s", filename); return false; } @@ -48,22 +48,24 @@ struct pw_carts *pw_carts_list(char const *dir) { goto error; } - size_t count = (size_t)icount; - if (count > SIZE_MAX / sizeof(struct pw_cart_file)) { + size_t max_count = (size_t)icount; + if (max_count > SIZE_MAX / sizeof(struct pw_cart_file)) { goto error; } - carts = SDL_malloc(sizeof(*carts) + count * sizeof(*carts->files)); + carts = SDL_malloc(sizeof(*carts) + max_count * sizeof(*carts->files)); if (!carts) { goto error; } - carts->count = 0; - for (size_t i = 0; i < count; i++) { - struct pw_cart_file *file = &carts->files[i]; + size_t count = 0; + for (size_t i = 0; i < max_count; i++) { + struct pw_cart_file *file = &carts->files[count]; if (!cart_file_path(file, dir, cartfiles[i], ".png")) { - goto error; + SDL_LogWarn(0, "unsupported cart file: %s", SDL_GetError()); + continue; } + count++; } carts->count = count; @@ -140,20 +142,10 @@ struct pw_cart_png *pw_cart_png_init(SDL_Renderer *renderer) { goto error; } - union pw_png_pixel *image = pw_png_load( - pw_sysroot_unknown_p8_png.data, pw_sysroot_unknown_p8_png.size, - PW_CART_PNG_WIDTH, PW_CART_PNG_HEIGHT); - if (!image) { + if (!pw_cart_png_reset(png)) { goto error; } - bool ok = blit_image(image, png->texture); - SDL_free(image); - if (!ok) { - goto error; - } - - SDL_zeroa(png->rom); return png; error: @@ -247,17 +239,38 @@ cleanup: bool pw_cart_png_load(struct pw_cart_file *file, struct pw_cart_png *png) { if (!file) { - SDL_InvalidParamError("file"); - return false; + return SDL_InvalidParamError("file"); } if (!png) { - SDL_InvalidParamError("png"); - return false; + return SDL_InvalidParamError("png"); } return load_cart_png(file->path, png->texture, png->rom); } +bool pw_cart_png_reset(struct pw_cart_png *png) { + if (!png) { + return SDL_InvalidParamError("png"); + } + + union pw_png_pixel *image = pw_png_load( + pw_sysroot_unknown_p8_png.data, pw_sysroot_unknown_p8_png.size, + PW_CART_PNG_WIDTH, PW_CART_PNG_HEIGHT); + if (!image) { + SDL_LogCritical(0, "unexpected error parsing builtin unknown cart PNG"); + return false; + } + + bool ok = blit_image(image, png->texture); + SDL_free(image); + if (!ok) { + return false; + } + + SDL_zeroa(png->rom); + return true; +} + static struct pw_fs_node_buffer *embedded_cart(struct pw_fs_entry *e, char const **filename) { static char const cart_dir[] = "user_data/carts/"; @@ -93,6 +93,11 @@ void pw_cart_png_free(struct pw_cart_png *png); bool pw_cart_png_load(struct pw_cart_file *file, struct pw_cart_png *png); /** + * Resets a cart to its initial empty state. + */ +bool pw_cart_png_reset(struct pw_cart_png *png); + +/** * Loads a cart from a file, including mounting its user data file-system. * * Optionally specify a loaded cart PNG to copy the ROM from instead of @@ -729,11 +729,17 @@ uint32_t w2c_env_0x5F_0x5Fsyscall146(struct w2c_env *env, uint32_t which, for (uint32_t i = 0; i < iovcnt; i++) { uint32_t buffer = wasm_rt_memory_load32(&env->memory, iov + 8 * i); uint32_t len = wasm_rt_memory_load32(&env->memory, iov + 8 * i + 4); + + // Make sure to check the source range before writing, this ensures that + // we don't modify the storage file-system when we would trap anyway, + // leaving it in a bad state. + wasm_rt_memory_check(&env->memory, buffer, len); + uint8_t *contents = pw_fs_write(env->context.cart->storage, fd, len); if (!contents) { return -pw_fs_errno(env->context.cart->storage); } - wasm_rt_memory_load(&env->memory, buffer, contents, len); + wasm_rt_memory_load_unchecked(&env->memory, buffer, contents, len); } if (!pw_fs_flush(env->context.cart->storage, fd)) { @@ -763,6 +769,7 @@ uint32_t w2c_env_0x5F_0x5Fsyscall195(struct w2c_env *env, uint32_t which, // of the fields. If ever this causes issues with PICO-8 things (which I // have not noticed for any cart), we can implement something. + wasm_rt_memory_check(&env->memory, stat, 88); MEM_WRITE(32, env, stat, 0); // st_dev MEM_WRITE(64, env, stat, 0); // st_ino MEM_WRITE(32, env, stat, s.mode); @@ -829,10 +829,14 @@ uint8_t *pw_fs_write(struct pw_fs *fs, pw_fd fd, uint32_t len) { w->capacity = capacity; } + // We cannot know for sure that the caller will actually write the + // data to the buffer, which means that it is possible for us to sync + // uninitialized malloc data which would be undefined behaviour. To + // prevent this, always clear the new portions of the buffer. Note that // POSIX requires holes to be filled with 0s (in case the file was - // seeked past the end). - if (s->pos > w->size) { - SDL_memset(w->data + w->size, 0, s->pos - w->size); + // seeked past the end), so we satisfy that as well. + if (end > w->size) { + SDL_memset(w->data + w->size, 0, end - w->size); } uint8_t *buffer = w->data + s->pos; diff --git a/src/input.c b/src/input.c index 8e2ee33..0cc2f78 100644 --- a/src/input.c +++ b/src/input.c @@ -458,7 +458,7 @@ enum pw_window_flags pw_input_window(struct pw_input *input) { SDL_WindowFlags flags = SDL_GetWindowFlags(input->window); if (!(flags & - (SDL_WINDOW_MINIMIZED | SDL_WINDOW_HIDDEN | SDL_WINDOW_MINIMIZED))) { + (SDL_WINDOW_OCCLUDED | SDL_WINDOW_HIDDEN | SDL_WINDOW_MINIMIZED))) { result |= PW_WINDOW_VISIBLE; } if (flags & SDL_WINDOW_INPUT_FOCUS) { @@ -3,6 +3,18 @@ #include "input.h" #include "menu.h" +static bool load_cart(struct pw_menu *menu, size_t i) { + struct pw_cart_file *file = &menu->carts->files[i]; + menu->selection.valid = pw_cart_png_load(file, menu->png); + if (!menu->selection.valid) { + SDL_LogWarn(0, "malformed cart '%s': %s", file->name, SDL_GetError()); + if (!pw_cart_png_reset(menu->png)) { + return false; + } + } + return true; +} + struct pw_menu *pw_menu_init(SDL_Renderer *renderer, char const *carts, enum pw_scale_mode mode) { if (!renderer) { @@ -14,11 +26,10 @@ struct pw_menu *pw_menu_init(SDL_Renderer *renderer, char const *carts, return NULL; } - struct pw_menu *menu = SDL_malloc(sizeof(*menu)); + struct pw_menu *menu = SDL_calloc(1, sizeof(*menu)); if (!menu) { return NULL; } - SDL_zerop(menu); menu->carts = pw_carts_list(carts); if (!menu->carts) { @@ -32,7 +43,8 @@ struct pw_menu *pw_menu_init(SDL_Renderer *renderer, char const *carts, if (!menu->carts->count) { SDL_LogWarn(0, "empty or missing carts directory"); - } else if (!pw_cart_png_load(&menu->carts->files[0], menu->png)) { + menu->selection.valid = false; + } else if (!load_cart(menu, 0)) { goto error; } @@ -187,23 +199,22 @@ bool pw_menu_iterate(struct pw_menu *menu, struct pw_cart **cart) { } if (menu->selection.current != menu->selection.next) { - if (!pw_cart_png_load(&menu->carts->files[menu->selection.next], - menu->png)) { + if (!load_cart(menu, menu->selection.next)) { return false; } menu->selection.current = menu->selection.next; } - if (menu->selection.selected) { + if (menu->selection.valid && menu->selection.selected) { *cart = pw_cart_load(&menu->carts->files[menu->selection.current], menu->png); if (!*cart) { return false; } - menu->selection.selected = false; } else { *cart = NULL; } + menu->selection.selected = false; return true; } @@ -17,6 +17,7 @@ struct pw_menu { struct { size_t current; size_t next; + bool valid; bool selected; } selection; }; diff --git a/src/player.c b/src/player.c index a4d7a75..06545af 100644 --- a/src/player.c +++ b/src/player.c @@ -43,7 +43,8 @@ static bool add_gamepad(struct pw_players *players, players->gamepads[i] = SDL_OpenGamepad(event->which); if (!players->gamepads[i]) { - return false; + SDL_LogWarn(0, "failed to open gamepad: %s", SDL_GetError()); + return true; } SDL_LogDebug(0, "connected gamepad for player %zu", i + 1); |
