]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
tools: mtk_image: replace strncpy(d, s, sizeof(d)) with snprintf()
authorAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Tue, 11 Dec 2018 14:41:43 +0000 (16:41 +0200)
committerTom Rini <trini@konsulko.com>
Sat, 15 Dec 2018 16:49:56 +0000 (11:49 -0500)
Starting from version 8 the GCC, i.e. C compiler, starts complaining about
possible '\0' terminator loss or, as in this case, garbage copy.

In function ‘mtk_image_set_gen_header’,
    inlined from ‘mtk_image_set_header’ at tools/mtk_image.c:733:3:
tools/mtk_image.c:659:2: warning: ‘strncpy’ specified bound 12 equals destination size [-Wstringop-truncation]
  strncpy(hdr->boot.name, bootname, sizeof(hdr->boot.name));
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In function ‘mtk_brom_parse_imagename’,
    inlined from ‘mtk_image_check_params’ at tools/mtk_image.c:388:9:
tools/mtk_image.c:325:5: warning: ‘strncpy’ specified bound 32 equals destination size [-Wstringop-truncation]
     strncpy(lk_name, val, sizeof(lk_name));
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Replace it with snprintf() to tell compiler how much room we have in the
destination buffer for source string.

Fixes: 3b975a147c3c ("tools: MediaTek: add MTK boot header generation to mkimage")
Cc: Ryder Lee <ryder.lee@mediatek.com>
Cc: Weijie Gao <weijie.gao@mediatek.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: Weijie Gao <weijie.gao@mediatek.com>
tools/mtk_image.c

index 2706d2de32ae2cf65a066927639f25ddd1f32c40..2ca519483d33dd7ae7ed8d264afd4d92684e064c 100644 (file)
@@ -322,7 +322,7 @@ static int mtk_brom_parse_imagename(const char *imagename)
                                lk = val;
 
                        if (!strcmp(key, "lkname"))
-                               strncpy(lk_name, val, sizeof(lk_name));
+                               snprintf(lk_name, sizeof(lk_name), "%s", val);
                }
 
                if (next)
@@ -656,7 +656,7 @@ static void mtk_image_set_gen_header(void *ptr, off_t filesize,
                bootname = SDMMC_BOOT_NAME;
 
        /* Generic device header */
-       strncpy(hdr->boot.name, bootname, sizeof(hdr->boot.name));
+       snprintf(hdr->boot.name, sizeof(hdr->boot.name), "%s", bootname);
        hdr->boot.version = cpu_to_le32(1);
        hdr->boot.size = cpu_to_le32(sizeof(hdr->boot));