Commit 91246ee5e0d8be8a15a669844f0893cd0f01c604

Edward Thomson 2021-11-01T20:14:34

path: use new length validation functions

diff --git a/src/attrcache.c b/src/attrcache.c
index 15c7fab..b16d95c 100644
--- a/src/attrcache.c
+++ b/src/attrcache.c
@@ -12,6 +12,7 @@
 #include "config.h"
 #include "sysdir.h"
 #include "ignore.h"
+#include "path.h"
 
 GIT_INLINE(int) attr_cache_lock(git_attr_cache *cache)
 {
@@ -43,6 +44,7 @@ int git_attr_cache__alloc_file_entry(
 	const char *path,
 	git_pool *pool)
 {
+	git_str fullpath_str = GIT_STR_INIT;
 	size_t baselen = 0, pathlen = strlen(path);
 	size_t cachesize = sizeof(git_attr_file_entry) + pathlen + 1;
 	git_attr_file_entry *ce;
@@ -66,7 +68,10 @@ int git_attr_cache__alloc_file_entry(
 	}
 	memcpy(&ce->fullpath[baselen], path, pathlen);
 
-	if (git_fs_path_validate_workdir_with_len(repo, ce->fullpath, pathlen + baselen) < 0)
+	fullpath_str.ptr = ce->fullpath;
+	fullpath_str.size = pathlen + baselen;
+
+	if (git_path_validate_str_length(repo, &fullpath_str) < 0)
 		return -1;
 
 	ce->path = &ce->fullpath[baselen];
@@ -173,7 +178,7 @@ static int attr_cache_lookup(
 		git_str *p = attr_session ? &attr_session->tmp : &path;
 
 		if (git_str_joinpath(p, source->base, source->filename) < 0 ||
-		    git_fs_path_validate_workdir_buf(repo, p) < 0)
+		    git_path_validate_str_length(repo, p) < 0)
 			return -1;
 
 		filename = p->ptr;
diff --git a/src/checkout.c b/src/checkout.c
index ad4eddd..5733f4a 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -329,7 +329,7 @@ static int checkout_target_fullpath(
 	if (path && git_str_puts(&data->target_path, path) < 0)
 		return -1;
 
-	if (git_fs_path_validate_workdir_buf(data->repo, &data->target_path) < 0)
+	if (git_path_validate_str_length(data->repo, &data->target_path) < 0)
 		return -1;
 
 	*out = &data->target_path;
@@ -2035,7 +2035,7 @@ static int checkout_merge_path(
 	int error = 0;
 
 	if ((error = git_str_joinpath(out, data->opts.target_directory, result->path)) < 0 ||
-	    (error = git_fs_path_validate_workdir_buf(data->repo, out)) < 0)
+	    (error = git_path_validate_str_length(data->repo, out)) < 0)
 		return error;
 
 	/* Most conflicts simply use the filename in the index */
@@ -2338,7 +2338,7 @@ static int validate_target_directory(checkout_data *data)
 {
 	int error;
 
-	if ((error = git_fs_path_validate_workdir(data->repo, data->opts.target_directory)) < 0)
+	if ((error = git_path_validate_length(data->repo, data->opts.target_directory)) < 0)
 		return error;
 
 	if (git_fs_path_isdir(data->opts.target_directory))
diff --git a/src/filter.c b/src/filter.c
index 9502960..2712e8c 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -18,6 +18,7 @@
 #include "blob.h"
 #include "attr_file.h"
 #include "array.h"
+#include "path.h"
 
 struct git_filter_source {
 	git_repository    *repo;
@@ -1095,7 +1096,7 @@ int git_filter_list_stream_file(
 	if ((error = stream_list_init(
 			&stream_start, &filter_streams, filters, target)) < 0 ||
 	    (error = git_fs_path_join_unrooted(&abspath, path, base, NULL)) < 0 ||
-	    (error = git_fs_path_validate_workdir_buf(repo, &abspath)) < 0)
+	    (error = git_path_validate_str_length(repo, &abspath)) < 0)
 		goto done;
 
 	initialized = 1;
diff --git a/src/ignore.c b/src/ignore.c
index e7d8b79..cee58d7 100644
--- a/src/ignore.c
+++ b/src/ignore.c
@@ -13,6 +13,7 @@
 #include "fs_path.h"
 #include "config.h"
 #include "wildmatch.h"
+#include "path.h"
 
 #define GIT_IGNORE_INTERNAL		"[internal]exclude"
 
@@ -320,14 +321,14 @@ int git_ignore__for_path(
 		    (error = git_fs_path_resolve_relative(&local, 0)) < 0 ||
 		    (error = git_fs_path_to_dir(&local)) < 0 ||
 		    (error = git_str_joinpath(&ignores->dir, workdir, local.ptr)) < 0 ||
-		    (error = git_fs_path_validate_workdir_buf(repo, &ignores->dir)) < 0) {
+		    (error = git_path_validate_str_length(repo, &ignores->dir)) < 0) {
 			/* Nothing, we just want to stop on the first error */
 		}
 
 		git_str_dispose(&local);
 	} else {
 		if (!(error = git_str_joinpath(&ignores->dir, path, "")))
-		    error = git_fs_path_validate_filesystem(ignores->dir.ptr, ignores->dir.size);
+		    error = git_path_validate_str_length(NULL, &ignores->dir);
 	}
 
 	if (error < 0)
diff --git a/src/iterator.c b/src/iterator.c
index a627e0f..a4337bb 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -9,6 +9,7 @@
 
 #include "tree.h"
 #include "index.h"
+#include "path.h"
 
 #define GIT_ITERATOR_FIRST_ACCESS   (1 << 15)
 #define GIT_ITERATOR_HONOR_IGNORES  (1 << 16)
@@ -1279,7 +1280,7 @@ static int filesystem_iterator_entry_hash(
 			iter->base.repo, entry->path, GIT_OBJECT_BLOB, NULL);
 
 	if (!(error = git_str_joinpath(&fullpath, iter->root, entry->path)) &&
-	    !(error = git_fs_path_validate_workdir_buf(iter->base.repo, &fullpath)))
+	    !(error = git_path_validate_str_length(iter->base.repo, &fullpath)))
 		error = git_odb_hashfile(&entry->id, fullpath.ptr, GIT_OBJECT_BLOB);
 
 	git_str_dispose(&fullpath);
@@ -1361,7 +1362,7 @@ static int filesystem_iterator_frame_push(
 		git_str_puts(&root, iter->root);
 
 	if (git_str_oom(&root) ||
-	    git_fs_path_validate_workdir_buf(iter->base.repo, &root) < 0) {
+	    git_path_validate_str_length(iter->base.repo, &root) < 0) {
 		error = -1;
 		goto done;
 	}
@@ -1389,10 +1390,16 @@ static int filesystem_iterator_frame_push(
 
 	while ((error = git_fs_path_diriter_next(&diriter)) == 0) {
 		iterator_pathlist_search_t pathlist_match = ITERATOR_PATHLIST_FULL;
+		git_str path_str = GIT_STR_INIT;
 		bool dir_expected = false;
 
-		if ((error = git_fs_path_diriter_fullpath(&path, &path_len, &diriter)) < 0 ||
-		    (error = git_fs_path_validate_workdir_with_len(iter->base.repo, path, path_len)) < 0)
+		if ((error = git_fs_path_diriter_fullpath(&path, &path_len, &diriter)) < 0)
+			goto done;
+
+		path_str.ptr = (char *)path;
+		path_str.size = path_len;
+
+		if ((error = git_path_validate_str_length(iter->base.repo, &path_str)) < 0)
 			goto done;
 
 		GIT_ASSERT(path_len > iter->root_len);
@@ -1565,7 +1572,7 @@ static int filesystem_iterator_is_dir(
 	}
 
 	if ((error = git_str_joinpath(&fullpath, iter->root, entry->path)) < 0 ||
-	    (error = git_fs_path_validate_workdir_buf(iter->base.repo, &fullpath)) < 0 ||
+	    (error = git_path_validate_str_length(iter->base.repo, &fullpath)) < 0 ||
 	    (error = p_stat(fullpath.ptr, &st)) < 0)
 		goto done;
 
diff --git a/src/mailmap.c b/src/mailmap.c
index 4fbb1ae..4336fe3 100644
--- a/src/mailmap.c
+++ b/src/mailmap.c
@@ -16,6 +16,7 @@
 #include "git2/revparse.h"
 #include "blob.h"
 #include "parse.h"
+#include "path.h"
 
 #define MM_FILE ".mailmap"
 #define MM_FILE_CONFIG "mailmap.file"
@@ -331,7 +332,7 @@ static int mailmap_add_file_ondisk(
 	if (error < 0)
 		goto cleanup;
 
-	error = git_fs_path_validate_workdir_buf(repo, &fullpath);
+	error = git_path_validate_str_length(repo, &fullpath);
 	if (error < 0)
 		goto cleanup;
 
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index acd6270..dc291d0 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -1362,7 +1362,7 @@ static int refdb_fs_backend__prune_refs(
 				git_str_cstr(&relative_path));
 
 		if (!error)
-			error = git_fs_path_validate_filesystem(base_path.ptr, base_path.size);
+			error = git_path_validate_str_length(NULL, &base_path);
 
 		if (error < 0)
 			goto cleanup;
diff --git a/src/repository.c b/src/repository.c
index 2f7ae9b..f564453 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -32,7 +32,7 @@
 #include "annotated_commit.h"
 #include "submodule.h"
 #include "worktree.h"
-
+#include "path.h"
 #include "strmap.h"
 
 #ifdef GIT_WIN32
@@ -2662,7 +2662,7 @@ int git_repository_workdir_path(
 	}
 
 	if (!(error = git_str_joinpath(out, repo->workdir, path)))
-		error = git_fs_path_validate_workdir_buf(repo, out);
+		error = git_path_validate_str_length(repo, out);
 
 	return error;
 }
@@ -2858,7 +2858,7 @@ int git_repository_hashfile(
 	GIT_ASSERT_ARG(repo);
 
 	if ((error = git_fs_path_join_unrooted(&full_path, path, workdir, NULL)) < 0 ||
-	    (error = git_fs_path_validate_workdir_buf(repo, &full_path)) < 0)
+	    (error = git_path_validate_str_length(repo, &full_path)) < 0)
 		return error;
 
 	/*
diff --git a/src/submodule.c b/src/submodule.c
index ffe29cc..0370ac8 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -386,7 +386,7 @@ int git_submodule__lookup_with_cache(
 			if (git_str_join3(&path, '/',
 			                  git_repository_workdir(repo),
 					  name, DOT_GIT) < 0 ||
-			    git_fs_path_validate_workdir_buf(NULL, &path) < 0)
+			    git_path_validate_str_length(NULL, &path) < 0)
 				return -1;
 
 			if (git_fs_path_exists(path.ptr))
diff --git a/src/worktree.c b/src/worktree.c
index f0fc6d7..e08d6d4 100644
--- a/src/worktree.c
+++ b/src/worktree.c
@@ -9,6 +9,7 @@
 
 #include "buf.h"
 #include "repository.h"
+#include "path.h"
 
 #include "git2/branch.h"
 #include "git2/commit.h"
@@ -136,7 +137,7 @@ static int open_worktree_dir(git_worktree **out, const char *parent, const char 
 		goto out;
 	}
 
-	if ((error = git_fs_path_validate_workdir(NULL, dir)) < 0)
+	if ((error = git_path_validate_length(NULL, dir)) < 0)
 		goto out;
 
 	if ((wt = git__calloc(1, sizeof(*wt))) == NULL) {