aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-10-24 12:59:35 +0200
committerNicholas Rodrigues Lordello <[email protected]>2026-10-24 12:59:35 +0200
commitc6c868ceea68bf70d04a148f16252df389d96593 (patch)
treea92dd79bbe127c97c8f0f26f22cc6ddbc8733225
parentf43e306bf050b9f96be4b22382cacd96d86b5239 (diff)
Fix Strict-Aliasing Violation
I think I was doing an aliasing violation when I was casting the `char *` to the union type, so instead reaturn bytes and provide a method for reading a pixel from an image. These are only called when loading images during cart selection and setup (for the debug info font) and so I would prefer to be safe rather than sorry (since performance isn't that critical - it is not in the tight PICO-8 game loop).
-rw-r--r--src/cart.c18
-rw-r--r--src/debug.c8
-rw-r--r--src/png.c20
-rw-r--r--src/png.h12
4 files changed, 45 insertions, 13 deletions
diff --git a/src/cart.c b/src/cart.c
index 3e61e18..c9a8951 100644
--- a/src/cart.c
+++ b/src/cart.c
@@ -145,7 +145,7 @@ void pw_carts_free(struct pw_carts *carts) {
SDL_free(carts);
}
-static bool blit_image(union pw_png_pixel const *image, SDL_Texture *texture) {
+static bool blit_image(pw_png image, SDL_Texture *texture) {
SDL_Surface *surface = NULL;
if (!SDL_LockTextureToSurface(texture, NULL, &surface)) {
return false;
@@ -164,7 +164,9 @@ static bool blit_image(union pw_png_pixel const *image, SDL_Texture *texture) {
// steganographic encoding of the cart data.
uint32_t *pixels = surface->pixels;
for (size_t i = 0; i < PW_CART_PNG_WIDTH * PW_CART_PNG_HEIGHT; i++) {
- union pw_png_pixel px = image[i];
+ union pw_png_pixel px;
+ pw_png_pixel(image, i, &px);
+
px.raw &= 0xfcfcfcfc;
px.ch.a |= 0x03;
pixels[i] = px.raw;
@@ -190,9 +192,9 @@ 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);
+ pw_png 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) {
goto error;
}
@@ -233,7 +235,7 @@ static bool load_cart_png(char const *path, SDL_Texture *texture,
bool result = false;
uint8_t *data = NULL;
- union pw_png_pixel *image = NULL;
+ pw_png image = NULL;
int64_t ssize = SDL_GetIOSize(png);
if (ssize < 0) {
@@ -266,7 +268,9 @@ static bool load_cart_png(char const *path, SDL_Texture *texture,
// Read steganographically encoded cart ROM.
for (size_t i = 0; i < PW_CART_ROM_SIZE; i++) {
- union pw_png_pixel px = image[i];
+ union pw_png_pixel px;
+ pw_png_pixel(image, i, &px);
+
px.raw &= 0x03030303;
rom[i] = (px.ch.a << 6) | (px.ch.r << 4) | (px.ch.g << 2) | px.ch.b;
}
diff --git a/src/debug.c b/src/debug.c
index f232490..f9248c1 100644
--- a/src/debug.c
+++ b/src/debug.c
@@ -19,7 +19,7 @@ struct pw_debug *pw_debug_init(SDL_Renderer *renderer) {
SDL_Palette *palette = NULL;
SDL_Surface *surface = NULL;
- union pw_png_pixel *image = NULL;
+ pw_png image = NULL;
SDL_Texture *font = NULL;
struct pw_debug *debug = NULL;
@@ -47,10 +47,14 @@ struct pw_debug *pw_debug_init(SDL_Renderer *renderer) {
if (!image) {
goto error;
}
+
union pw_png_pixel mask = {.ch = {0xff, 0xff, 0xff, 0x00}};
uint8_t *pixels = surface->pixels;
for (size_t i = 0; i < PW_FONT_WIDTH * PW_FONT_HEIGHT; ++i) {
- pixels[i] = !!(image[i].raw & mask.raw);
+ union pw_png_pixel px;
+ pw_png_pixel(image, i, &px);
+
+ pixels[i] = !!(px.raw & mask.raw);
}
SDL_free(image);
image = NULL;
diff --git a/src/png.c b/src/png.c
index 878d659..72bccf8 100644
--- a/src/png.c
+++ b/src/png.c
@@ -18,10 +18,15 @@ static int stbi__mul2shorts_valid(int, int) __attribute__((unused));
#endif
#include <stb/stb_image.h>
-union pw_png_pixel *pw_png_load(uint8_t const *data, size_t sz, int w, int h) {
+pw_png pw_png_load(uint8_t const *data, size_t sz, size_t w, size_t h) {
+ if (w > STBI_MAX_DIMENSIONS || h > STBI_MAX_DIMENSIONS) {
+ SDL_SetError("image dimensions too large");
+ return NULL;
+ }
+
int x, y, n;
int ok = stbi_info_from_memory(data, sz, &x, &y, &n);
- if (!ok || x != w || y != h) {
+ if (!ok || x != (int)w || y != (int)h) {
SDL_SetError("unexpected PNG dimensions");
return NULL;
}
@@ -32,8 +37,17 @@ union pw_png_pixel *pw_png_load(uint8_t const *data, size_t sz, int w, int h) {
return NULL;
}
+ return image;
+}
+
+void pw_png_pixel(pw_png png, size_t i, union pw_png_pixel *pixel) {
+ if (!png) {
+ SDL_zerop(pixel);
+ return;
+ }
+
// The desired channels are set to 4, making the STB image library ensure
// that the resulting image has exactly 4 8-bit colour channels per pixel;
// allowing us to treat the image as x-by-y 32-bit pixels.
- return (union pw_png_pixel *)image;
+ SDL_memcpy(&pixel->raw, png + i * sizeof(pixel->raw), sizeof(pixel->raw));
}
diff --git a/src/png.h b/src/png.h
index 516c4fa..1bef974 100644
--- a/src/png.h
+++ b/src/png.h
@@ -4,6 +4,11 @@
#include <SDL3/SDL_stdinc.h>
/**
+ * A PNG image.
+ */
+typedef uint8_t *pw_png;
+
+/**
* A pixel from a loaded PNG image.
*/
union pw_png_pixel {
@@ -19,6 +24,11 @@ union pw_png_pixel {
/**
* Loads a PNG with exact dimensions.
*/
-union pw_png_pixel *pw_png_load(uint8_t const *data, size_t sz, int w, int h);
+pw_png pw_png_load(uint8_t const *data, size_t sz, size_t w, size_t h);
+
+/**
+ * Loads a pixel from the PNG image.
+ */
+void pw_png_pixel(pw_png png, size_t i, union pw_png_pixel *pixel);
#endif