Commit e642f803bde24b919bd4fe2ba66956b4b8173c32

Stefan Sperling 2019-07-12T13:01:51

don't leak redundantly allocated relpath in got_worktree_checkout_files()

diff --git a/lib/worktree.c b/lib/worktree.c
index 21986f3..323bf4a 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -1572,11 +1572,6 @@ got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
 	if (path[0]) {
 		char *tree_path;
 		int obj_type;
-		relpath = strdup(path);
-		if (relpath == NULL) {
-			err = got_error_from_errno("strdup");
-			goto done;
-		}
 		if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
 		    got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
 		    path) == -1) {
@@ -1627,6 +1622,12 @@ got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
 				err = got_error_from_errno2("basename", path);
 				goto done;
 			}
+		} else {
+			relpath = strdup(path);
+			if (relpath == NULL) {
+				err = got_error_from_errno("strdup");
+				goto done;
+			}
 		}
 	} else {
 		relpath = strdup("");