public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [U-Boot] [PATCH v1] tools: mtk_image: replace strncpy(d, s, sizeof(d)) with snprintf()
@ 2018-12-11 14:41 Andy Shevchenko
  2018-12-13  4:22 ` Weijie Gao
  2018-12-17 12:11 ` [U-Boot] [U-Boot, " Tom Rini
  0 siblings, 2 replies; 3+ messages in thread
From: Andy Shevchenko @ 2018-12-11 14:41 UTC (permalink / raw)
  To: u-boot

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>
---
 tools/mtk_image.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/mtk_image.c b/tools/mtk_image.c
index 2706d2de32..2ca519483d 100644
--- a/tools/mtk_image.c
+++ b/tools/mtk_image.c
@@ -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));
 
-- 
2.19.2

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [U-Boot] [PATCH v1] tools: mtk_image: replace strncpy(d, s, sizeof(d)) with snprintf()
  2018-12-11 14:41 [U-Boot] [PATCH v1] tools: mtk_image: replace strncpy(d, s, sizeof(d)) with snprintf() Andy Shevchenko
@ 2018-12-13  4:22 ` Weijie Gao
  2018-12-17 12:11 ` [U-Boot] [U-Boot, " Tom Rini
  1 sibling, 0 replies; 3+ messages in thread
From: Weijie Gao @ 2018-12-13  4:22 UTC (permalink / raw)
  To: u-boot

On Tue, 2018-12-11 at 16:41 +0200, Andy Shevchenko wrote:
> 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>
> ---
>  tools/mtk_image.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/mtk_image.c b/tools/mtk_image.c
> index 2706d2de32..2ca519483d 100644
> --- a/tools/mtk_image.c
> +++ b/tools/mtk_image.c
> @@ -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));
>  

Hi Andy,

The snprintf adds a NULL char at the end of the string which gives
only 31 available bytes for the lk name filed. But it's OK for this
because almost no one would like to give a name to fill the whole
field. The same applies to the device header.

Reviewed-by: Weijie Gao <weijie.gao@mediatek.com>

Best Regards,

Weijie

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [U-Boot] [U-Boot, v1] tools: mtk_image: replace strncpy(d, s, sizeof(d)) with snprintf()
  2018-12-11 14:41 [U-Boot] [PATCH v1] tools: mtk_image: replace strncpy(d, s, sizeof(d)) with snprintf() Andy Shevchenko
  2018-12-13  4:22 ` Weijie Gao
@ 2018-12-17 12:11 ` Tom Rini
  1 sibling, 0 replies; 3+ messages in thread
From: Tom Rini @ 2018-12-17 12:11 UTC (permalink / raw)
  To: u-boot

On Tue, Dec 11, 2018 at 04:41:43PM +0200, Andy Shevchenko wrote:

> 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>

Applied to u-boot/master, thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20181217/b7767982/attachment.sig>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-12-17 12:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-12-11 14:41 [U-Boot] [PATCH v1] tools: mtk_image: replace strncpy(d, s, sizeof(d)) with snprintf() Andy Shevchenko
2018-12-13  4:22 ` Weijie Gao
2018-12-17 12:11 ` [U-Boot] [U-Boot, " Tom Rini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox