fix possible garbage in unitialized char* struct members
authorPetr Štetiar <ynezz@true.cz>
Wed, 23 Oct 2019 10:11:47 +0000 (12:11 +0200)
committerPetr Štetiar <ynezz@true.cz>
Sat, 9 Nov 2019 13:28:09 +0000 (14:28 +0100)
scan-build from clang version 9 has reported following issues:

 crc32.h:44:32: warning: The right operand of '^' is a garbage value
                val = crc_table[(uint8_t)val ^ *(uint8_t*)buf] ^ (val >> 8);
                                             ^ ~~~~~~~~~~~~~~

cppcheck version 1.89 has reported following issues:

 fwtool.c:260:9: error: Uninitialized variable: dest [uninitvar]
  memcpy(dest, dbuf->cur + dbuf->cur_len - cur_len, cur_len);
         ^
 fwtool.c:333:27: note: Calling function 'extract_tail', 2nd argument '&tr' value is <Uninit>
   if (extract_tail(&dbuf, &tr, sizeof(tr))) {
                           ^

Signed-off-by: Petr Štetiar <ynezz@true.cz>
fwtool.c

index e925b0bf5e65ecb3d4b2d3c2e002cdbd3dbe5750..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");
@@ -289,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");