firmware-utils: mkrtn56uimg: fix compiler warnings/errors
authorPetr Štetiar <ynezz@true.cz>
Sun, 21 Jul 2019 06:44:55 +0000 (08:44 +0200)
committerPetr Štetiar <ynezz@true.cz>
Tue, 23 Jul 2019 20:07:23 +0000 (22:07 +0200)
mkrtn56uimg.c:105:6: error: unused variable ‘i’ [-Werror=unused-variable]
mkrtn56uimg.c:168:29: error: pointer targets in passing argument 2 of ‘strncpy’ differ in signedness [-Werror=pointer-sign]
mkrtn56uimg.c:233:20: error: pointer targets in passing argument 1 of ‘strncpy’ differ in signedness [-Werror=pointer-sign]
mkrtn56uimg.c:123:11: error: unused variable ‘checksum’ [-Werror=unused-variable]
mkrtn56uimg.c:120:10: error: unused variable ‘data’ [-Werror=unused-variable]
mkrtn56uimg.c:119:12: error: unused variable ‘len’ [-Werror=unused-variable]
mkrtn56uimg.c:211:3: error: ignoring return value of ‘ftruncate’, declared with attribute warn_unused_result [-Werror=unused-result]
mkrtn56uimg.c:245:2: error: ignoring return value of ‘ftruncate’, declared with attribute warn_unused_result [-Werror=unused-result]
mkrtn56uimg.c:271:11: error: this statement may fall through [-Werror=implicit-fallthrough=]
mkrtn56uimg.c:287:10: error: ‘filename’ may be used uninitialized in this function [-Werror=maybe-uninitialized]

Signed-off-by: Petr Štetiar <ynezz@true.cz>
tools/firmware-utils/src/mkrtn56uimg.c

index 92aaf314a1757cfc5d237d4b2320877cdb8a2bd9..d875c99220adcd5b42d18c1978a241b58bfaf5a5 100644 (file)
@@ -102,7 +102,6 @@ static void
 usage(const char *progname, int status)
 {
        FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
-       int i;
 
        fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
        fprintf(stream, "\n"
@@ -116,11 +115,11 @@ usage(const char *progname, int status)
 int
 process_image(char *progname, char *filename, op_mode_t opmode)
 {
-       int             fd, len;
-       void            *data, *ptr;
+       int             fd, r;
+       void            *ptr;
        char            namebuf[IH_NMLEN];
        struct          stat sbuf;
-       uint32_t        checksum, offset_kernel, offset_sqfs, offset_end,
+       uint32_t        offset_kernel, offset_sqfs, offset_end,
                                offset_sec_header, offset_eb, offset_image_end;
        squashfs_sb_t *sqs;
        image_header_t *hdr;
@@ -165,12 +164,14 @@ process_image(char *progname, char *filename, op_mode_t opmode)
        }
 
        if (opmode == FACTORY) {
-               strncpy(namebuf, hdr->tail.ih_name, IH_NMLEN);
+               memcpy(namebuf, hdr->tail.ih_name, IH_NMLEN);
                hdr->tail.asus.kernel.major = 0;
                hdr->tail.asus.kernel.minor = 0;
                hdr->tail.asus.fs.major = 0;
                hdr->tail.asus.fs.minor = 0;
-               strncpy((char *)&hdr->tail.asus.productid, "RT-N56U", IH_PRODLEN);
+
+               const char p[] = "RT-N56U";
+               memcpy(&hdr->tail.asus.productid, p, sizeof(p));
        }
 
        if (hdr->tail.asus.ih_ksz == 0)
@@ -208,7 +209,11 @@ process_image(char *progname, char *filename, op_mode_t opmode)
         */
        if (offset_image_end > sbuf.st_size) {
                (void) munmap((void *)ptr, sbuf.st_size);
-               ftruncate(fd, offset_image_end);
+
+               r = ftruncate(fd, offset_image_end);
+               if (r < 0)
+                       goto err_ftruncate;
+
                ptr = (void *)mmap(0, offset_image_end,
                                                PROT_READ | PROT_WRITE,
                                                MAP_SHARED,
@@ -230,7 +235,7 @@ process_image(char *progname, char *filename, op_mode_t opmode)
        if (opmode == FACTORY) {
                hdr = ptr+offset_sec_header;
                memcpy(hdr, ptr, sizeof(image_header_t));
-               strncpy(hdr->tail.ih_name, namebuf, IH_NMLEN);
+               memcpy(hdr->tail.ih_name, namebuf, IH_NMLEN);
                calc_crc(hdr, ptr+offset_kernel, offset_sqfs - offset_kernel);
                calc_crc((image_header_t *)ptr, ptr+offset_kernel, offset_image_end - offset_kernel);
        } else {
@@ -242,17 +247,26 @@ process_image(char *progname, char *filename, op_mode_t opmode)
        else
                (void) munmap((void *)ptr, offset_image_end);
 
-       ftruncate(fd, offset_image_end);
-       (void) close (fd);
+       r = ftruncate(fd, offset_image_end);
+       if (r < 0)
+               goto err_ftruncate;
 
+       (void) close (fd);
        return EXIT_SUCCESS;
+
+err_ftruncate:
+       close(fd);
+       fprintf(stderr, "%s: Can't ftruncate %s: %s\n",
+               progname, filename, strerror(errno));
+       return (EXIT_FAILURE);
 }
 
 int
 main(int argc, char **argv)
 {
        int             opt;
-       char            *filename, *progname;
+       char            *progname;
+       char            *filename = NULL;
        op_mode_t       opmode = NONE;
 
        progname = argv[0];
@@ -269,6 +283,7 @@ main(int argc, char **argv)
                        break;
                case 'h':
                        opmode = NONE;
+                       /* fall through */
                default:
                        usage(progname, EXIT_FAILURE);
                        opmode = NONE;