public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox•com>
To: "René Scharfe" <l.s.r@web•de>
Cc: Git Mailing List <git@vger•kernel.org>
Subject: Re: [PATCH] sha1_name: use strlcpy() to copy strings
Date: Sun, 22 Feb 2015 12:00:15 -0800	[thread overview]
Message-ID: <xmqqsidxvhrk.fsf@gitster.dls.corp.google.com> (raw)
In-Reply-To: <54E8E2AA.1020300@web.de> ("René Scharfe"'s message of "Sat, 21 Feb 2015 20:55:22 +0100")

René Scharfe <l.s.r@web•de> writes:

> Use strlcpy() instead of calling strncpy() and then setting the last
> byte of the target buffer to NUL explicitly.  This shortens and
> simplifies the code a bit.

Thanks.  It makes me wonder if the longer term direction should be
not to use a bound buffer for oc->path, though.

>
> Signed-of-by: Rene Scharfe <l.s.r@web•de>
> ---
>  sha1_name.c | 8 ++------
>  1 file changed, 2 insertions(+), 6 deletions(-)
>
> diff --git a/sha1_name.c b/sha1_name.c
> index cf2a83b..95f9f8f 100644
> --- a/sha1_name.c
> +++ b/sha1_name.c
> @@ -1391,9 +1391,7 @@ static int get_sha1_with_context_1(const char *name,
>  			namelen = strlen(cp);
>  		}
>  
> -		strncpy(oc->path, cp,
> -			sizeof(oc->path));
> -		oc->path[sizeof(oc->path)-1] = '\0';
> +		strlcpy(oc->path, cp, sizeof(oc->path));
>  
>  		if (!active_cache)
>  			read_cache();
> @@ -1443,9 +1441,7 @@ static int get_sha1_with_context_1(const char *name,
>  							   name, len);
>  			}
>  			hashcpy(oc->tree, tree_sha1);
> -			strncpy(oc->path, filename,
> -				sizeof(oc->path));
> -			oc->path[sizeof(oc->path)-1] = '\0';
> +			strlcpy(oc->path, filename, sizeof(oc->path));
>  
>  			free(new_filename);
>  			return ret;

  reply	other threads:[~2015-02-22 20:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-21 19:55 [PATCH] sha1_name: use strlcpy() to copy strings René Scharfe
2015-02-22 20:00 ` Junio C Hamano [this message]
2015-02-22 22:33   ` René Scharfe
2015-02-23 18:36     ` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqsidxvhrk.fsf@gitster.dls.corp.google.com \
    --to=gitster@pobox$(echo .)com \
    --cc=git@vger$(echo .)kernel.org \
    --cc=l.s.r@web$(echo .)de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox