Re: [PATCH] Off-by-one error in get_path_prefix(), found by Valgrind

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

 



On Wed, Jun 07, 2006 at 01:01:40PM -0400, Pavel Roskin wrote:
> From: Pavel Roskin <proski@xxxxxxx>
> 
> Signed-off-by: Pavel Roskin <proski@xxxxxxx>
> ---
> 
>  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@xxxxxxxxxxxxxx>

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;
 }
 
-
: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]