threads / patch / 12420

patchFix make_absolute_path() for parameters without a slash

Subject: [PATCH] Fix make_absolute_path() for parameters without a slash

## tl;dr

3 messages between Mar 2, 2008 and Mar 2, 2008. Diffs are folded; open one to read it.

replies: 2people: 1as markdown or json

Johannes Schindelin· Mar 2, 2008, 07:17 UTC · lore

When passing "xyz" to make_absolute_path(), make_absolute_path() erroneously tried to chdir("xyz"), and then append "/xyz". Instead, skip the chdir() completely when no slash was found.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
	I should add a test, but I am simply too tired now.  Will do so 
	tomorrow.
 path.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
Show changes to path.c +3 −1
diff --git a/path.c b/path.c
index 4260952..bf3e469 100644
--- a/path.c
+++ b/path.c
@@ -311,8 +311,10 @@ const char *make_absolute_path(const char *path)
 			if (last_slash) {
 				*last_slash = '\0';
 				last_elem = xstrdup(last_slash + 1);
-			} else
+			} else {
+				*buf = '\0';
 				last_elem = xstrdup(buf);
+			}
 		}
 
 		if (*buf) {
-- 
1.5.4.3.446.gbe8932
Johannes Schindelin· Mar 2, 2008, 07:31 UTC · re: Johannes Schindelin · lore

Re: [PATCH] Fix make_absolute_path() for parameters without a slash

Hi,
On Sun, 2 Mar 2008, Johannes Schindelin wrote:
Show 12 quoted lines
> diff --git a/path.c b/path.c
> index 4260952..bf3e469 100644
> --- a/path.c
> +++ b/path.c
> @@ -311,8 +311,10 @@ const char *make_absolute_path(const char *path)
>  			if (last_slash) {
>  				*last_slash = '\0';
>  				last_elem = xstrdup(last_slash + 1);
> -			} else
> +			} else {
> +				*buf = '\0';
>  				last_elem = xstrdup(buf);

Oh, my. The *buf = '\0' must come _after the assignment to last_elem, of course.

Will stop posting patches for today, and fix tomorrow.

Ciao, Dscho

Johannes Schindelin· Mar 2, 2008, 07:40 UTC · re: Johannes Schindelin · lore

When passing "xyz" to make_absolute_path(), make_absolute_path() erroneously tried to chdir("xyz"), and then append "/xyz". Instead, skip the chdir() completely when no slash was found.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
On Sun, 2 Mar 2008, Johannes Schindelin wrote:
> Will stop posting patches for today, and fix tomorrow.
It was just too embarassing.
 path.c           |    4 +++-
 t/t0000-basic.sh |    2 ++
 2 files changed, 5 insertions(+), 1 deletions(-)
Show changes to 2 files +5 −1

path.c, t/t0000-basic.sh

diff --git a/path.c b/path.c
index 4260952..af27161 100644
--- a/path.c
+++ b/path.c
@@ -311,8 +311,10 @@ const char *make_absolute_path(const char *path)
 			if (last_slash) {
 				*last_slash = '\0';
 				last_elem = xstrdup(last_slash + 1);
-			} else
+			} else {
 				last_elem = xstrdup(buf);
+				*buf = '\0';
+			}
 		}
 
 		if (*buf) {
diff --git a/t/t0000-basic.sh b/t/t0000-basic.sh
index 92de088..27b54cb 100755
--- a/t/t0000-basic.sh
+++ b/t/t0000-basic.sh
@@ -304,6 +304,8 @@ test_expect_success 'absolute path works as expected' '
 	test "$dir" = "$(test-absolute-path $dir2)" &&
 	file="$dir"/index &&
 	test "$file" = "$(test-absolute-path $dir2/index)" &&
+	basename=blub &&
+	test "$dir/$basename" = $(cd .git && test-absolute-path $basename) &&
 	ln -s ../first/file .git/syml &&
 	sym="$(cd first; pwd -P)"/file &&
 	test "$sym" = "$(test-absolute-path $dir2/syml)"
-- 
1.5.4.3.446.gbe8932

← back to recent threads