All of lore.kernel.org
 help / color / mirror / Atom feed
From: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
To: Pavel Roskin <proski@gnu.org>, Junio C Hamano <junkio@cox.net>
Cc: git@vger.kernel.org
Subject: Re: [PATCH] Off-by-one error in get_path_prefix(), found by Valgrind
Date: Wed, 7 Jun 2006 20:05:43 +0200	[thread overview]
Message-ID: <20060607180543.GA26638@lsrfire.ath.cx> (raw)
In-Reply-To: <20060607170140.13372.64613.stgit@dv.roinet.com>

On Wed, Jun 07, 2006 at 01:01:40PM -0400, Pavel Roskin wrote:
> From: Pavel Roskin <proski@gnu.org>
> 
> Signed-off-by: Pavel Roskin <proski@gnu.org>
> ---
> 
>  builtin-tar-tree.c |    4 ++--
>  1 files changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/builtin-tar-tree.c b/builtin-tar-tree.c
> index 5f740cf..05da1f2 100644
> --- a/builtin-tar-tree.c
> +++ b/builtin-tar-tree.c
> @@ -166,8 +166,8 @@ static unsigned int ustar_header_chksum(
>  static int get_path_prefix(const struct strbuf *path, int maxlen)
>  {
>  	int i = path->len;
> -	if (i > maxlen)
> -		i = maxlen;
> +	if (i >= maxlen)
> +		i = maxlen - 1;
>  	while (i > 0 && path->buf[i] != '/')
>  		i--;
>  	return i;

Argh, yes.  Thanks, Pavel!  However, the other branch is incorrect, too:
accessing path->buf[path->len] is wrong, even if it's within the buffer.
In order to use a length variable to point to the end of some string we
need to subtract 1. *sigh*  So, how about this one instead?

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>

diff --git a/builtin-tar-tree.c b/builtin-tar-tree.c
index 5f740cf..7663b9b 100644
--- a/builtin-tar-tree.c
+++ b/builtin-tar-tree.c
@@ -168,8 +168,9 @@ static int get_path_prefix(const struct 
 	int i = path->len;
 	if (i > maxlen)
 		i = maxlen;
-	while (i > 0 && path->buf[i] != '/')
+	do {
 		i--;
+	} while (i > 0 && path->buf[i] != '/');
 	return i;
 }
 

  reply	other threads:[~2006-06-07 18:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-06-07 17:01 [PATCH] Off-by-one error in get_path_prefix(), found by Valgrind Pavel Roskin
2006-06-07 18:05 ` Rene Scharfe [this message]
2006-06-07 18:33   ` Pavel Roskin
2006-06-07 18:47     ` Junio C Hamano

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=20060607180543.GA26638@lsrfire.ath.cx \
    --to=rene.scharfe@lsrfire.ath.cx \
    --cc=git@vger.kernel.org \
    --cc=junkio@cox.net \
    --cc=proski@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.