git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Handle double slashes in make_relative_path()

From
Junio C Hamano <gitster@pobox.com>
Date
Jan 22, 2010, 01:40 UTC
Message-ID
<7vpr52gbmu.fsf@alter.siamese.dyndns.org>
In-Reply-To
<379d55c6a4110736aadb8ace3b050de879a9deab.1264118830.git.trast@student.ethz.ch>
Thomas Rast <trast@student.ethz.ch> writes:
Show 18 quoted lines
> diff --git a/path.c b/path.c
> index 2ec950b..a195bab 100644
> --- a/path.c
> +++ b/path.c
> @@ -400,10 +400,10 @@ int set_shared_perm(const char *path, int mode)
>  	baselen = strlen(base);
>  	if (prefixcmp(abs, base))
>  		return abs;
> -	if (abs[baselen] == '/')
> -		baselen++;
> -	else if (base[baselen - 1] != '/')
> +	if (abs[baselen] != '/' && base[baselen - 1] != '/')
>  		return abs;
> +	while (abs[baselen] == '/')
> +		baselen++;
>  	strcpy(buf, abs + baselen);
>  	return buf;
>  }

Curious; why does your hunk header says set_shared_perm() while this is a patch to make_relative_path()? Do you run a broken git with funny funcname regexp pattern?

The function takes two paths, an early part of abs is supposed to match base; otherwise abs is not a path under base and the function returns the full path of abs.

Now what is the goal of this patch? To allow people to have duplicated slashes at random places in either abs or base, or is it only interested in a particular input that is malformed? If the latter, what is the permitted non-canonical input?

If abs were "/a//b/c" and base were "/a/b", then the combination is rejected by prefixcmp() and full "/a//b/c" is returned. Is it the intended behaviour of the patch?

I would actually have expected to see something like this, but I haven't even compile tested it, so...

 path.c |   29 ++++++++++++++++++++---------
 1 files changed, 20 insertions(+), 9 deletions(-)
diff --git a/path.c b/path.c
index 2ec950b..1c3570c 100644
--- a/path.c
+++ b/path.c
@@ -394,17 +394,28 @@ int set_shared_perm(const char *path, int mode)
 const char *make_relative_path(const char *abs, const char *base)
 {
 	static char buf[PATH_MAX + 1];
-	int baselen;
+	int i = 0, j = 0;
+
 	if (!base)
 		return abs;
-	baselen = strlen(base);
-	if (prefixcmp(abs, base))
-		return abs;
-	if (abs[baselen] == '/')
-		baselen++;
-	else if (base[baselen - 1] != '/')
-		return abs;
-	strcpy(buf, abs + baselen);
+	while (base[i]) {
+		if (base[i] == '/') {
+			if (abs[j] != '/')
+				return abs;
+			while (base[i] == '/')
+				i++;
+			while (abs[j] == '/')
+				j++;
+			continue;
+		} else if (abs[j] != base[i]) {
+			return abs;
+		}
+		i++;
+		j++;
+	}
+	while (abs[j] == '/')
+		j++;
+	strcpy(buf, abs + j);
 	return buf;
 }
 
Previous: Thomas RastNext: Junio C Hamano
Message 2 of 20 in “Handle double slashes in make_relative_path()”
  1. Handle double slashes in make_relative_path()Thomas Rast, Jan 22, 2010
  2. Junio C HamanoJan 22, 2010
  3. Junio C HamanoJan 22, 2010
  4. Johannes SixtJan 22, 2010
  5. Robin RosenbergJan 23, 2010
  6. Johannes SixtJan 23, 2010
  7. Robin RosenbergJan 23, 2010
  8. Johannes SixtJan 23, 2010
  9. Junio C HamanoJan 23, 2010
  10. Junio C HamanoJan 23, 2010
  11. Johannes SixtJan 23, 2010
  12. Sverre RabbelierJan 23, 2010
  13. Thomas RastJan 24, 2010
  14. Bernhard R. LinkJan 24, 2010
  15. Junio C HamanoJan 24, 2010
  16. Johannes SixtJan 24, 2010
  17. Junio C HamanoJan 24, 2010
  18. Robin RosenbergJan 25, 2010
  19. Thomas RastJan 22, 2010
  20. Junio C HamanoJan 22, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.