Fix strncpy() usage
PATCH 1/1 Fix strncpy() usage
Manpage of strncpy() says:
If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.
Put \0 to the end of the buffer to ensure that
the destination string is NULL-terminated.
This patch also fixes a compile error with a newer version of GCC.
--- mmc_cmds.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mmc_cmds.c +2 -2
@@ -1834,8 +1834,8 @@ int do_read_extcsd(int nargs, char **argv) }if (ext_csd_rev >=7 ) { -memset (lbuf,0 ,sizeof (lbuf)); -strncpy (lbuf, (char *)&ext_csd[EXT_CSD_FIRMWARE_VERSION ],8 ); +strncpy (lbuf, (char *)&ext_csd[EXT_CSD_FIRMWARE_VERSION ],sizeof (lbuf) -1 ); + lbuf[sizeof (lbuf) -1 ] =0 ;printf ("eMMC Firmware Version: %s\n" , lbuf);printf ("eMMC Life Time Estimation A [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]: 0x%02x\n" , ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A ]);
Signature · 1 line
2.32.0
+ Avri, Bean
Quoted 20 lines · Oleh Kravchenko
Manpage of
strncpy()says:
If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.Put \0 to the end of the buffer to ensure that
the destination string is NULL-terminated.This patch also fixes a compile error with a newer version of GCC.
Signed-off-by Oleh Kravchenko <[email protected]>--- mmc_cmds.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)mmc_cmds.c +0
-0
mmc_cmds.c int do_read_extcsd(int nargs, char **argv) · line 1837
Quoted 6 lines
mmc_cmds.c +1
-2@@ -1834,8 +1834,8 @@ int do_read_extcsd(int nargs, char **argv) }if (ext_csd_rev >=7 ) { -memset (lbuf,0 ,sizeof (lbuf)); -strncpy (lbuf, (char *)&ext_csd[EXT_CSD_FIRMWARE_VERSION ],8 ); +strncpy (lbuf, (char *)&ext_csd[EXT_CSD_FIRMWARE_VERSION ],sizeof (lbuf) -1 );
This may look better, but is actually making it worse. lbuf is 10 bytes long, while the ext_csd is 8 bytes.
Quoted 7 lines
mmc_cmds.c +1
-1+ lbuf[sizeof (lbuf) -1 ] =0 ;printf ("eMMC Firmware Version: %s\n" , lbuf);printf ("eMMC Life Time Estimation A [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]: 0x%02x\n" , ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A ]); --2.32.0
Kind regards
Uffe
PATCH v2 1/1 Replace strncpy() usage by printf()
In reply to Oleh KravchenkoManpage of strncpy() says:
If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.
Put \0 to the end of the buffer to ensure that
the destination string is NULL-terminated.
This patch also fixes a compile error with a newer version of GCC.
--- mmc_cmds.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
mmc_cmds.c +1 -4
@@ -1392,7 +1392,6 @@ int do_read_extcsd(int nargs, char **argv)__u32 regl;int fd, ret;char *device; -char lbuf[10 ];const char *str;if (nargs !=2 ) {
@@ -1834,9 +1833,7 @@ int do_read_extcsd(int nargs, char **argv) }if (ext_csd_rev >=7 ) { -memset (lbuf,0 ,sizeof (lbuf)); -strncpy (lbuf, (char *)&ext_csd[EXT_CSD_FIRMWARE_VERSION ],8 ); -printf ("eMMC Firmware Version: %s\n" , lbuf); +printf ("eMMC Firmware Version: %.*s\n" ,8 , (char *)&ext_csd[EXT_CSD_FIRMWARE_VERSION ]);printf ("eMMC Life Time Estimation A [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]: 0x%02x\n" , ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A ]); printf("eMMC Life Time Estimation B [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B]: 0x%02x\n",
Signature · 1 line
2.32.0
Re: [PATCH v2] Replace strncpy() usage by printf()
+ Avri
Quoted 7 lines · Oleh Kravchenko
Manpage of
strncpy()says:
If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.Put \0 to the end of the buffer to ensure that
the destination string is NULL-terminated.
Please update the commit message to reflect what the patch really does, as the above is no longer correct.
Quoted 4 lines
This patch also fixes a compile error with a newer version of GCC.
Signed-off-by Oleh Kravchenko <[email protected]>
At next submission, please also add Avri Altman <[email protected]>, who co-maintains the mmc-utils.
Quoted 17 lines
--- mmc_cmds.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)mmc_cmds.c +0
-1@@ -1392,7 +1392,6 @@ int do_read_extcsd(int nargs, char **argv)__u32 regl;int fd, ret;char *device; -char lbuf[10 ];const char *str;if (nargs !=2 ) {
mmc_cmds.c int do_read_extcsd(int nargs, char **argv) · line 1836
Quoted 7 lines
mmc_cmds.c +1
-3@@ -1834,9 +1833,7 @@ int do_read_extcsd(int nargs, char **argv) }if (ext_csd_rev >=7 ) { -memset (lbuf,0 ,sizeof (lbuf)); -strncpy (lbuf, (char *)&ext_csd[EXT_CSD_FIRMWARE_VERSION ],8 ); -printf ("eMMC Firmware Version: %s\n" , lbuf); +printf ("eMMC Firmware Version: %.*s\n" ,8 , (char *)&ext_csd[EXT_CSD_FIRMWARE_VERSION ]);
This looks good to me. However, I suggest you skip the dynamic length format. Just use %.8s.
Quoted 6 lines
mmc_cmds.c +0
-1printf ("eMMC Life Time Estimation A [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]: 0x%02x\n" , ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A ]); printf("eMMC Life Time Estimation B [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B]: 0x%02x\n", --2.32.0
Kind regards
Uffe
PATCH v3 1/1 Use printf() to extract and print fw version
In reply to Oleh KravchenkoThis patch also fixes a compile error with a newer version of GCC:
error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 511 [-Werror=stringop-truncation]
--- mmc_cmds.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
mmc_cmds.c +1 -4
@@ -1392,7 +1392,6 @@ int do_read_extcsd(int nargs, char **argv)__u32 regl;int fd, ret;char *device; -char lbuf[10 ];const char *str;if (nargs !=2 ) {
@@ -1834,9 +1833,7 @@ int do_read_extcsd(int nargs, char **argv) }if (ext_csd_rev >=7 ) { -memset (lbuf,0 ,sizeof (lbuf)); -strncpy (lbuf, (char *)&ext_csd[EXT_CSD_FIRMWARE_VERSION ],8 ); -printf ("eMMC Firmware Version: %s\n" , lbuf); +printf ("eMMC Firmware Version: %.8s\n" , (char *)&ext_csd[EXT_CSD_FIRMWARE_VERSION ]);printf ("eMMC Life Time Estimation A [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]: 0x%02x\n" , ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A ]); printf("eMMC Life Time Estimation B [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B]: 0x%02x\n",
Signature · 1 line
2.32.0
RE: [EXT] [PATCH v3] Use printf() to extract and print fw version
Micron Confidential
Quoted 7 lines
Cc: Oleh Kravchenko <[email protected]>
Subject: [EXT] [PATCH v3] Useprintf()to extract and print fw versionCAUTION: EXTERNAL EMAIL. Do not click links or open attachments unless you recognize the sender and were expecting this message.
Quoted 5 lines
This patch also fixes a compile error with a newer version of GCC:
error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 511 [-Werror=stringop-truncation]Signed-off-by Oleh Kravchenko <[email protected]>
Hi Oleh,
It is better to add the prefix "mmc-utils:" in the Commit subject to distinguish it from Linux kernel changes
Micron Confidential
Re: [EXT] [PATCH v3] Use printf() to extract and print fw version
29.11.21 17:05, Bean Huo (beanhuo) пише:
Quoted 9 lines
Micron Confidential
Quoted 7 lines
Cc: Oleh Kravchenko <[email protected]>
Subject: [EXT] [PATCH v3] Useprintf()to extract and print fw versionCAUTION: EXTERNAL EMAIL. Do not click links or open attachments unless you recognize the sender and were expecting this message.
Quoted 8 lines
Quoted 5 lines
This patch also fixes a compile error with a newer version of GCC:
error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 511 [-Werror=stringop-truncation]Signed-off-by Oleh Kravchenko <[email protected]>Hi Oleh,
It is better to add the prefix "mmc-utils:" in the Commit subject to distinguish it from Linux kernel changes
Will do it.
Quoted 1 line
Reviewed-by Bean Huo <[email protected]>
Thanks!
PATCH v3 1/1 mmc-utils: Use printf() to extract and print fw version
In reply to Oleh KravchenkoThis patch also fixes a compile error with a newer version of GCC:
error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 511 [-Werror=stringop-truncation]
--- mmc_cmds.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
Update commit message.
mmc_cmds.c +1 -4
@@ -1392,7 +1392,6 @@ int do_read_extcsd(int nargs, char **argv)__u32 regl;int fd, ret;char *device; -char lbuf[10 ];const char *str;if (nargs !=2 ) {
@@ -1834,9 +1833,7 @@ int do_read_extcsd(int nargs, char **argv) }if (ext_csd_rev >=7 ) { -memset (lbuf,0 ,sizeof (lbuf)); -strncpy (lbuf, (char *)&ext_csd[EXT_CSD_FIRMWARE_VERSION ],8 ); -printf ("eMMC Firmware Version: %s\n" , lbuf); +printf ("eMMC Firmware Version: %.8s\n" , (char *)&ext_csd[EXT_CSD_FIRMWARE_VERSION ]);printf ("eMMC Life Time Estimation A [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]: 0x%02x\n" , ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A ]); printf("eMMC Life Time Estimation B [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B]: 0x%02x\n",
Signature · 1 line
2.32.0
PATCH v4 1/1 mmc-utils: Use printf() to extract and print fw version
In reply to Oleh KravchenkoThis patch also fixes a compile error with a newer version of GCC:
error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 511 [-Werror=stringop-truncation]
--- mmc_cmds.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
PATCH v4
Update commit message.
mmc_cmds.c +1 -4
@@ -1392,7 +1392,6 @@ int do_read_extcsd(int nargs, char **argv)__u32 regl;int fd, ret;char *device; -char lbuf[10 ];const char *str;if (nargs !=2 ) {
@@ -1834,9 +1833,7 @@ int do_read_extcsd(int nargs, char **argv) }if (ext_csd_rev >=7 ) { -memset (lbuf,0 ,sizeof (lbuf)); -strncpy (lbuf, (char *)&ext_csd[EXT_CSD_FIRMWARE_VERSION ],8 ); -printf ("eMMC Firmware Version: %s\n" , lbuf); +printf ("eMMC Firmware Version: %.8s\n" , (char *)&ext_csd[EXT_CSD_FIRMWARE_VERSION ]);printf ("eMMC Life Time Estimation A [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]: 0x%02x\n" , ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A ]); printf("eMMC Life Time Estimation B [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B]: 0x%02x\n",
Signature · 1 line
2.32.0
RE: [PATCH v4] mmc-utils: Use printf() to extract and print fw version
Hi,
Quoted 4 lines
This patch also fixes a compile error with a newer version of GCC:
error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 511 [-Werror=stringop-truncation]
You are reverting commit 0eea71e4f2 (mmc-utils: Fix for Firmware Version string printing). Please use git revert and add an explanation in your commit log.
You might also would like to take a look at the correspondence concerning a different approach to the same issue here - https://lore.kernel.org/lkml/[email protected]/
Thanks,
Avri
Quoted 38 lines
Signed-off-by Oleh Kravchenko <[email protected]>Reviewed-by Bean Huo <[email protected]>--- mmc_cmds.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)PATCH v4
Update commit message.mmc_cmds.c +2
-4@@ -1392,7 +1392,6 @@ int do_read_extcsd(int nargs, char **argv)__u32 regl;int fd, ret;char *device; -char lbuf[10 ];const char *str;if (nargs !=2 ) {@@ -1834,9 +1833,7 @@ int do_read_extcsd(int nargs, char **argv) }if (ext_csd_rev >=7 ) { -memset (lbuf,0 ,sizeof (lbuf)); -strncpy (lbuf, (char *)&ext_csd[EXT_CSD_FIRMWARE_VERSION ],8 ); -printf ("eMMC Firmware Version: %s\n" , lbuf); +printf ("eMMC Firmware Version: %.8s\n" , + (char *)&ext_csd[EXT_CSD_FIRMWARE_VERSION ]); printf("eMMC Life Time Estimation A [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]: 0x%02x\n", ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A ]); printf("eMMC Life Time Estimation B[
EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B]: 0x%02x\n",
--
2.32.0
Re: [PATCH v4] mmc-utils: Use printf() to extract and print fw version
Hello Avri!
30.11.21 09:42, Avri Altman пише:
Quoted 6 lines
Hi,
Quoted 3 lines
This patch also fixes a compile error with a newer version of GCC:
error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 511 [-Werror=stringop-truncation]You are reverting commit
0eea71e4f2(mmc-utils: Fix for Firmware Version string printing). Please use git revert and add an explanation in your commit log.
I'm not reverting this commit.
It's similar but not.
Quoted 2 lines
You might also would like to take a look at the correspondence concerning a different approach to the same issue here - https://lore.kernel.org/lkml/[email protected]/
Thank you.
I didn't know that.
Quoted 41 lines
Thanks,
AvriQuoted 37 lines
Signed-off-by Oleh Kravchenko <[email protected]>Reviewed-by Bean Huo <[email protected]>--- mmc_cmds.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)PATCH v4
Update commit message.mmc_cmds.c +2
-4@@ -1392,7 +1392,6 @@ int do_read_extcsd(int nargs, char **argv)__u32 regl;int fd, ret;char *device; -char lbuf[10 ];const char *str;if (nargs !=2 ) {@@ -1834,9 +1833,7 @@ int do_read_extcsd(int nargs, char **argv) }if (ext_csd_rev >=7 ) { -memset (lbuf,0 ,sizeof (lbuf)); -strncpy (lbuf, (char *)&ext_csd[EXT_CSD_FIRMWARE_VERSION ],8 ); -printf ("eMMC Firmware Version: %s\n" , lbuf); +printf ("eMMC Firmware Version: %.8s\n" , + (char *)&ext_csd[EXT_CSD_FIRMWARE_VERSION ]); printf("eMMC Life Time Estimation A [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]: 0x%02x\n", ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A ]); printf("eMMC Life Time Estimation B[
EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B]: 0x%02x\n",
--
2.32.0
Re: [PATCH v4] mmc-utils: Use printf() to extract and print fw version
Quoted 5 lines · Oleh Kravchenko
Hello Avri!
30.11.21 09:42, Avri Altman пише:
Quoted 1 line
Hi,
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
This patch also fixes a compile error with a newer version of GCC:
error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 511 [-Werror=stringop-truncation]You are reverting commit
0eea71e4f2(mmc-utils: Fix for Firmware Version string printing). Please use git revert and add an explanation in your commit log.I'm not reverting this commit.
It's similar but not.
Right, this time we limit the print to 8 chars, which avoids garbage from a non-NULL terminated string.
Perhaps we should add (similar to what we do for kernel commits):
[...]
Kind regards
Uffe
Re: [PATCH v4] mmc-utils: Use printf() to extract and print fw version
Hello Ulf,
30.11.21 14:14, Ulf Hansson пише:
Quoted 10 lines · Oleh Kravchenko
Quoted 9 lines
Hello Avri!
30.11.21 09:42, Avri Altman пише:
Quoted 6 lines
Hi,
Quoted 3 lines
This patch also fixes a compile error with a newer version of GCC:
error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 511 [-Werror=stringop-truncation]You are reverting commit
0eea71e4f2(mmc-utils: Fix for Firmware Version string printing). Please use git revert and add an explanation in your commit log.
Quoted 7 lines
Quoted 2 lines
I'm not reverting this commit.
It's similar but not.Right, this time we limit the print to 8 chars, which avoids garbage from a non-NULL terminated string.
Perhaps we should add (similar to what we do for kernel commits):
Fixes 0eea71e4f22a ("mmc-utils: Fix for Firmware Version string printing")
Good point!
Quoted 5 lines
[...]
Kind regards
Uffe
PATCH v5 1/1 mmc-utils: Use printf() to extract and print fw version
In reply to Oleh KravchenkoThis patch also fixes a compile error with a newer version of GCC:
error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 511 [-Werror=stringop-truncation]
--- mmc_cmds.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
PATCH v5
Updating commit message.
Adding Fixes: 0eea71e4f22a
mmc_cmds.c +1 -4
@@ -1392,7 +1392,6 @@ int do_read_extcsd(int nargs, char **argv)__u32 regl;int fd, ret;char *device; -char lbuf[10 ];const char *str;if (nargs !=2 ) {
@@ -1834,9 +1833,7 @@ int do_read_extcsd(int nargs, char **argv) }if (ext_csd_rev >=7 ) { -memset (lbuf,0 ,sizeof (lbuf)); -strncpy (lbuf, (char *)&ext_csd[EXT_CSD_FIRMWARE_VERSION ],8 ); -printf ("eMMC Firmware Version: %s\n" , lbuf); +printf ("eMMC Firmware Version: %.8s\n" , (char *)&ext_csd[EXT_CSD_FIRMWARE_VERSION ]);printf ("eMMC Life Time Estimation A [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]: 0x%02x\n" , ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A ]); printf("eMMC Life Time Estimation B [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B]: 0x%02x\n",
Signature · 1 line
2.32.0
Ulf Hansson
AppliedRe: [PATCH v5] mmc-utils: Use printf() to extract and print fw version
Quoted 8 lines · Oleh Kravchenko
This patch also fixes a compile error with a newer version of GCC:
error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 511 [-Werror=stringop-truncation]Signed-off-by Oleh Kravchenko <[email protected]>Reviewed-by Bean Huo <[email protected]>Fixes 0eea71e4f22a (mmc-utils: Fix for Firmware Version string printing)
Applied to git.kernel.org/pub/scm/utils/mmc/mmc-utils.git master, thanks!
Kind regards
Uffe
Quoted 34 lines
--- mmc_cmds.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)PATCH v5
Updating commit message.
Adding Fixes:0eea71e4f22ammc_cmds.c +1
-5@@ -1392,7 +1392,6 @@ int do_read_extcsd(int nargs, char **argv)__u32 regl;int fd, ret;char *device; -char lbuf[10 ];const char *str;if (nargs !=2 ) {@@ -1834,9 +1833,7 @@ int do_read_extcsd(int nargs, char **argv) }if (ext_csd_rev >=7 ) { -memset (lbuf,0 ,sizeof (lbuf)); -strncpy (lbuf, (char *)&ext_csd[EXT_CSD_FIRMWARE_VERSION ],8 ); -printf ("eMMC Firmware Version: %s\n" , lbuf); +printf ("eMMC Firmware Version: %.8s\n" , (char *)&ext_csd[EXT_CSD_FIRMWARE_VERSION ]);printf ("eMMC Life Time Estimation A [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]: 0x%02x\n" , ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A ]); printf("eMMC Life Time Estimation B [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B]: 0x%02x\n", --2.32.0