git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Off-by-one error in get_path_prefix(), found by Valgrind
@ 2006-06-07 17:01 Pavel Roskin
  2006-06-07 18:05 ` Rene Scharfe
  0 siblings, 1 reply; 4+ messages in thread
From: Pavel Roskin @ 2006-06-07 17:01 UTC (permalink / raw)
  To: git

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;

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] Off-by-one error in get_path_prefix(), found by Valgrind
  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
  2006-06-07 18:33   ` Pavel Roskin
  0 siblings, 1 reply; 4+ messages in thread
From: Rene Scharfe @ 2006-06-07 18:05 UTC (permalink / raw)
  To: Pavel Roskin, Junio C Hamano; +Cc: git

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;
 }
 

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] Off-by-one error in get_path_prefix(), found by Valgrind
  2006-06-07 18:05 ` Rene Scharfe
@ 2006-06-07 18:33   ` Pavel Roskin
  2006-06-07 18:47     ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Pavel Roskin @ 2006-06-07 18:33 UTC (permalink / raw)
  To: Rene Scharfe; +Cc: git

On Wed, 2006-06-07 at 20:05 +0200, Rene Scharfe wrote:
> Argh, yes.  Thanks, Pavel!

Actually, thanks to Julian Seward for Valgrind.

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

Fine with me.  Thank you for noticing!

-- 
Regards,
Pavel Roskin

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Off-by-one error in get_path_prefix(), found by Valgrind
  2006-06-07 18:33   ` Pavel Roskin
@ 2006-06-07 18:47     ` Junio C Hamano
  0 siblings, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2006-06-07 18:47 UTC (permalink / raw)
  To: Pavel Roskin, Rene Scharfe; +Cc: git

Pavel Roskin <proski@gnu.org> writes:

> On Wed, 2006-06-07 at 20:05 +0200, Rene Scharfe wrote:
>> Argh, yes.  Thanks, Pavel!
>
> Actually, thanks to Julian Seward for Valgrind.
>
>>   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?
>
> Fine with me.  Thank you for noticing!

Thanks both.  Applied.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-06-07 18:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2006-06-07 18:33   ` Pavel Roskin
2006-06-07 18:47     ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).