diff mbox series

[v4,05/10] unpack-trees: fix nested sparse-dir search

Message ID acdded0f762800392515c83aec2b276c62ad691c.1629841904.git.gitgitgadget@gmail.com (mailing list archive)
State New, archived
Headers show
Series Sparse index: delete ignored files outside sparse cone | expand

Commit Message

Derrick Stolee Aug. 24, 2021, 9:51 p.m. UTC
From: Derrick Stolee <dstolee@microsoft.com>

The iterated search in find_cache_entry() was recently modified to
include a loop that searches backwards for a sparse directory entry that
matches the given traverse_info and name_entry. However, the string
comparison failed to actually concatenate those two strings, so this
failed to find a sparse directory when it was not a top-level directory.

This caused some errors in rare cases where a 'git checkout' spanned a

Comments

René Scharfe Aug. 24, 2021, 10:21 p.m. UTC | #1
Am 24.08.21 um 23:51 schrieb Derrick Stolee via GitGitGadget:
> From: Derrick Stolee <dstolee@microsoft.com>
>
> The iterated search in find_cache_entry() was recently modified to
> include a loop that searches backwards for a sparse directory entry that
> matches the given traverse_info and name_entry. However, the string
> comparison failed to actually concatenate those two strings, so this
> failed to find a sparse directory when it was not a top-level directory.
>
> This caused some errors in rare cases where a 'git checkout' spanned a
> diff that modified files within the sparse directory entry, but we could
> not correctly find the entry.
>
> Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
> Helped-by: René Scharfe <l.s.r@web.de>
> Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
> ---
>  unpack-trees.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/unpack-trees.c b/unpack-trees.c
> index 5786645f315..47ef0cf4c53 100644
> --- a/unpack-trees.c
> +++ b/unpack-trees.c
> @@ -1255,7 +1255,8 @@ static int sparse_dir_matches_path(const struct cache_entry *ce,
>  static struct cache_entry *find_cache_entry(struct traverse_info *info,
>  					    const struct name_entry *p)
>  {
> -	struct cache_entry *ce;
> +	const char *path;
> +	struct cache_entry *ce = NULL;
                              ^^^^^^^
This added initialization doesn't seem to be needed...

>  	int pos = find_cache_pos(info, p->path, p->pathlen);
>  	struct unpack_trees_options *o = info->data;
>
> @@ -1283,7 +1284,9 @@ static struct cache_entry *find_cache_entry(struct traverse_info *info,
>  	while (pos >= 0) {
>  		ce = o->src_index->cache[pos];

... because ce is set here before it's used.

>
> -		if (strncmp(ce->name, p->path, p->pathlen))
> +		if (!skip_prefix(ce->name, info->traverse_path, &path) ||
> +		    strncmp(path, p->path, p->pathlen) ||
> +		    path[p->pathlen] != '/')
>  			return NULL;
>
>  		if (S_ISSPARSEDIR(ce->ce_mode) &&
>
Derrick Stolee Aug. 25, 2021, 1:09 a.m. UTC | #2
On 8/24/2021 6:21 PM, René Scharfe wrote:
> Am 24.08.21 um 23:51 schrieb Derrick Stolee via GitGitGadget:
>> -	struct cache_entry *ce;
>> +	const char *path;
>> +	struct cache_entry *ce = NULL;
>                               ^^^^^^^
> This added initialization doesn't seem to be needed...
> 
>>  	int pos = find_cache_pos(info, p->path, p->pathlen);
>>  	struct unpack_trees_options *o = info->data;
>>
>> @@ -1283,7 +1284,9 @@ static struct cache_entry *find_cache_entry(struct traverse_info *info,
>>  	while (pos >= 0) {
>>  		ce = o->src_index->cache[pos];
> 
> ... because ce is set here before it's used.

Better yet, 'ce' is only used within the while loop, so its
declaration can be moved to this line instead.

Thanks,
-Stolee
diff mbox series

Patch

diff that modified files within the sparse directory entry, but we could
not correctly find the entry.

Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Helped-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 unpack-trees.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/unpack-trees.c b/unpack-trees.c
index 5786645f315..47ef0cf4c53 100644
--- a/unpack-trees.c
+++ b/unpack-trees.c
@@ -1255,7 +1255,8 @@  static int sparse_dir_matches_path(const struct cache_entry *ce,
 static struct cache_entry *find_cache_entry(struct traverse_info *info,
 					    const struct name_entry *p)
 {
-	struct cache_entry *ce;
+	const char *path;
+	struct cache_entry *ce = NULL;
 	int pos = find_cache_pos(info, p->path, p->pathlen);
 	struct unpack_trees_options *o = info->data;
 
@@ -1283,7 +1284,9 @@  static struct cache_entry *find_cache_entry(struct traverse_info *info,
 	while (pos >= 0) {
 		ce = o->src_index->cache[pos];
 
-		if (strncmp(ce->name, p->path, p->pathlen))
+		if (!skip_prefix(ce->name, info->traverse_path, &path) ||
+		    strncmp(path, p->path, p->pathlen) ||
+		    path[p->pathlen] != '/')
 			return NULL;
 
 		if (S_ISSPARSEDIR(ce->ce_mode) &&