Re: [PATCH] Fix strncpy() usage

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



+ Avri, Bean


On Tue, 16 Nov 2021 at 11:51, Oleh Kravchenko <oleg@xxxxxxxxxx> wrote:
>
> 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 <oleg@xxxxxxxxxx>
> ---
>  mmc_cmds.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/mmc_cmds.c b/mmc_cmds.c
> index 73bd32a..016fe70 100644
> --- a/mmc_cmds.c
> +++ b/mmc_cmds.c
> @@ -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.

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



[Index of Archives]     [Linux Memonry Technology]     [Linux USB Devel]     [Linux Media]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux