[klibc] [PATCH] Null terminate before printing the link name.

Neal Murphy neal.p.murphy at alum.wpi.edu
Mon Apr 14 18:05:36 PDT 2014


You also want:
    link_name = malloc(max_siz + 1);
(This is from v2.0.1).

Otherwise you'd write past the end of the buffer when the link happens to be 
128 bytes long. Granted, it's not critical in *this* application, but it's
good practice.

N


On Monday, April 14, 2014 08:33:29 PM Rafi Rubin wrote:
> This fixes garbage I'm seeing consistently from readlink
> /dev/disk/by-uuid/.....   during boot.
> 
> Signed-off-by: Rafi Rubin <rafi at seas.upenn.edu>
> ---
>  usr/utils/readlink.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/usr/utils/readlink.c b/usr/utils/readlink.c
> index 5ea4e41..75a0735 100644
> --- a/usr/utils/readlink.c
> +++ b/usr/utils/readlink.c
> @@ -15,6 +15,7 @@ int main(int argc, char *argv[])
>  {
>         char *name, *link_name = NULL;
>         size_t max_siz = 128;
> +       size_t actual_siz = 0;
> 
>         progname = *argv++;
> 
> @@ -28,8 +29,9 @@ int main(int argc, char *argv[])
>                 exit(1);
>         }
> 
> -       if (readlink(name, link_name, max_siz) == -1)
> +       if ((actual_siz = readlink(name, link_name, max_siz)) == -1)
>                 exit(1);
> +       link_name[actual_siz] = '\0';
>         printf("%s\n", link_name);
> 
>         exit(0);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.zytor.com/pipermail/klibc/attachments/20140414/ea14f2f1/attachment.html>


More information about the klibc mailing list