cmake: use extra compiler warnings only on gcc6+
[project/fwtool.git] / fwtool.c
index 89e89514ad35f4539b55cc9eddc9d977871e22cc..6340db9da63cfdc8c69fd07f3e5a9e16a5d5541d 100644 (file)
--- a/fwtool.c
+++ b/fwtool.c
@@ -145,11 +145,12 @@ append_trailer(FILE *out, struct fwimage_trailer *tr)
 static int
 add_metadata(struct fwimage_trailer *tr)
 {
-       struct fwimage_header hdr = {};
+       struct fwimage_header hdr;
 
        tr->type = FWIMAGE_INFO;
        tr->size = sizeof(hdr) + sizeof(*tr);
 
+       memset(&hdr, 0, sizeof(hdr));
        trailer_update_crc(tr, &hdr, sizeof(hdr));
        fwrite(&hdr, sizeof(hdr), 1, firmware_file);
 
@@ -181,13 +182,15 @@ add_signature(struct fwimage_trailer *tr)
 static int
 add_data(const char *name)
 {
-       struct fwimage_trailer tr = {
-               .magic = cpu_to_be32(FWIMAGE_MAGIC),
-               .crc32 = ~0,
-       };
+       struct fwimage_trailer tr;
        int file_len = 0;
        int ret = 0;
 
+       memset(&tr, 0, sizeof(tr));
+
+       tr.crc32 = ~0;
+       tr.magic = cpu_to_be32(FWIMAGE_MAGIC);
+
        firmware_file = fopen(name, "r+");
        if (!firmware_file) {
                msg("Failed to open firmware file\n");
@@ -213,7 +216,9 @@ add_data(const char *name)
 
        if (ret) {
                fflush(firmware_file);
-               ftruncate(fileno(firmware_file), file_len);
+               ret = ftruncate(fileno(firmware_file), file_len);
+               if (ret < 0)
+                       msg("Error during ftruncate: %m\n");
        }
 
        return ret;
@@ -249,7 +254,7 @@ extract_tail(struct data_buf *dbuf, void *dest, int len)
        remove_tail(dbuf, cur_len);
 
        cur_len = len - cur_len;
-       if (cur_len && !dbuf->cur)
+       if (cur_len < 0 || !dbuf->cur)
                return 1;
 
        memcpy(dest, dbuf->cur + dbuf->cur_len - cur_len, cur_len);
@@ -287,6 +292,8 @@ extract_data(const char *name)
        void *buf;
        bool metadata_keep = false;
 
+       memset(&tr, 0, sizeof(tr));
+
        firmware_file = open_file(name, false);
        if (!firmware_file) {
                msg("Failed to open firmware file\n");
@@ -325,8 +332,10 @@ extract_data(const char *name)
 
        while (1) {
 
-               if (extract_tail(&dbuf, &tr, sizeof(tr)))
+               if (extract_tail(&dbuf, &tr, sizeof(tr))) {
+                       msg("unable to extract trailer header\n");
                        break;
+               }
 
                if (tr.magic != cpu_to_be32(FWIMAGE_MAGIC)) {
                        msg("Data not found\n");
@@ -346,7 +355,10 @@ extract_data(const char *name)
                        break;
                }
 
-               extract_tail(&dbuf, buf, data_len);
+               if (extract_tail(&dbuf, buf, data_len)) {
+                       msg("unable to extract trailer data\n");
+                       break;
+               }
 
                if (tr.type == FWIMAGE_SIGNATURE) {
                        if (!signature_file)
@@ -374,8 +386,13 @@ extract_data(const char *name)
                }
        }
 
-       if (!ret && truncate_file)
-               ftruncate(fileno(firmware_file), dbuf.file_len);
+       if (!ret && truncate_file) {
+               ret = ftruncate(fileno(firmware_file), dbuf.file_len);
+               if (ret < 0) {
+                       msg("Error during ftruncate: %m\n");
+                       goto out;
+               }
+       }
 
        if (write_truncated) {
                if (dbuf.prev)