commit 3122c071397b4db9c9122072c56bd31c02085ca7
Author:     FRIGN <[email protected]>
AuthorDate: Fri Mar 4 12:23:04 2016 +0100
Commit:     FRIGN <[email protected]>
CommitDate: Fri Mar 4 12:23:04 2016 +0100

    Use uint32-array to store the header
    
    this goes around the undefined behaviour imposed by using
    (*(uint32_t *)(hdr + 4n))

diff --git a/ff2png.c b/ff2png.c
index 52bdc58..bf210fb 100644
--- a/ff2png.c
+++ b/ff2png.c
@@ -9,8 +9,6 @@
 
 #include <png.h>
 
-#define HDR "farbfeld########"
-
 static char *argv0;
 
 void
@@ -26,9 +24,8 @@ main(int argc, char *argv[])
        png_structp pngs;
        png_infop pngi;
        size_t rowlen;
-       uint32_t width, height, i;
+       uint32_t hdr[4], width, height, i;
        uint16_t *row;
-       uint8_t hdr[16];
 
        argv0 = argv[0], argc--, argv++;
 
@@ -38,15 +35,15 @@ main(int argc, char *argv[])
        }
 
        /* header */
-       if (fread(hdr, 1, sizeof(HDR) - 1, stdin) != sizeof(HDR) - 1) {
+       if (fread(hdr, sizeof(*hdr), 4, stdin) != 4) {
                goto readerr;
        }
        if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1)) {
                fprintf(stderr, "%s: invalid magic value\n", argv0);
                return 1;
        }
-       width = ntohl(*((uint32_t *)(hdr + 8)));
-       height = ntohl(*((uint32_t *)(hdr + 12)));
+       width = ntohl(hdr[2]);
+       height = ntohl(hdr[3]);
 
        /* load png */
        pngs = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, pngerr,

Reply via email to