Skip to contentSkip to conversation

ls-files: don't try to prune an empty index

PATCH In review René Scharfe 3 participants 9 messages Jul 15, 2017 – Jul 16, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ls-files: don't try to prune an empty index

Exit early when asked to prune an index that contains no entries to begin with. This avoids pointer arithmetic on istate->cache, which is possibly NULL in that case.

Found with Clang's UBSan.

---
 builtin/ls-files.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
builtin/ls-files.c +1 -1
index b8514a0029..adf572da68 100644
@@ -362,7 +362,7 @@ static void prune_index(struct index_state *istate,
 	int pos;
 	unsigned int first, last;
 
-	if (!prefix)
+	if (!prefix || !istate->cache_nr)
 		return;
 	pos = index_name_pos(istate, prefix, prefixlen);
 	if (pos < 0)
Signature · 1 line
2.13.3
#1 lore raw
Quoted 16 lines · René Scharfe

Exit early when asked to prune an index that contains no entries to begin with. This avoids pointer arithmetic on istate->cache, which is possibly NULL in that case.

Found with Clang's UBSan.

---
 builtin/ls-files.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
builtin/ls-files.c +0 -0
index b8514a0029..adf572da68 100644

builtin/ls-files.c static void prune_index(struct index_state *istate, · line 368

Quoted 8 lines
builtin/ls-files.c +1 -1
@@ -362,7 +362,7 @@ static void prune_index(struct index_state *istate,
     int pos;
     unsigned int first, last;
 
-    if (!prefix)
+    if (!prefix || !istate->cache_nr)
         return;
     pos = index_name_pos(istate, prefix, prefixlen);
     if (pos < 0)

My patch looked like:

- if (!prefix)
+ if (!prefix || !istate->cache || istate->cache_nr == 0)

... which is probably a bit 'belt-n-braces'. ;-)

ATB,
Ramsay Jones

#2 lore raw

Am 16.07.2017 um 02:28 schrieb Ramsay Jones:

Quoted 33 lines · René Scharfe
Quoted 16 lines

Exit early when asked to prune an index that contains no entries to begin with. This avoids pointer arithmetic on istate->cache, which is possibly NULL in that case.

Found with Clang's UBSan.

---
  builtin/ls-files.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
builtin/ls-files.c +0 -0
index b8514a0029..adf572da68 100644

builtin/ls-files.c static void prune_index(struct index_state *istate, · line 368

Quoted 8 lines
builtin/ls-files.c +1 -1
@@ -362,7 +362,7 @@ static void prune_index(struct index_state *istate,
      int pos;
      unsigned int first, last;
 
-    if (!prefix)
+    if (!prefix || !istate->cache_nr)
          return;
      pos = index_name_pos(istate, prefix, prefixlen);
      if (pos < 0)

My patch looked like:

(quoted diff) +1 -1
-       if (!prefix)
+       if (!prefix || !istate->cache || istate->cache_nr == 0)
Quoted 1 line

... which is probably a bit 'belt-n-braces'. ;-)

Not checking for !istate->cache at this point is a good thing, I think. If we have entries, then ->cache must not be NULL, and if it is we'd get a segfault, notifying us that we have a bug. We could add an assert to state this requirement explicitly, but that would be the topic of a different patch.

René

#3 lore raw
In reply to René Scharfe
Quoted 5 lines · René Scharfe

Exit early when asked to prune an index that contains no entries to begin with. This avoids pointer arithmetic on istate->cache, which is possibly NULL in that case.

Found with Clang's UBSan.

Makes sense. We could use MOVE_ARRAY() here, but this is a sensible short-circuit to the whole function.

Looks like a good solution.

builtin/ls-files.c static void prune_index(struct index_state *istate, · line 368

Quoted 13 lines
builtin/ls-files.c +1 -1
index b8514a0029..adf572da68 100644
@@ -362,7 +362,7 @@ static void prune_index(struct index_state *istate,
 	int pos;
 	unsigned int first, last;
 
-	if (!prefix)
+	if (!prefix || !istate->cache_nr)
 		return;
 	pos = index_name_pos(istate, prefix, prefixlen);
 	if (pos < 0)

"git am" complained that this does not apply to its blobs. Did you hand-edit?

-Peff

#4 lore raw

Am 16.07.2017 um 12:41 schrieb Jeff King:

Quoted 21 lines · René Scharfe
Quoted 6 lines

Exit early when asked to prune an index that contains no entries to begin with. This avoids pointer arithmetic on istate->cache, which is possibly NULL in that case.

Found with Clang's UBSan.

Makes sense. We could use MOVE_ARRAY() here, but this is a sensible short-circuit to the whole function.

Looks like a good solution.

Quoted 8 lines
builtin/ls-files.c +0 -0
index b8514a0029..adf572da68 100644
@@ -362,7 +362,7 @@ static void prune_index(struct index_state *istate,
  	int pos;
  	unsigned int first, last;
 
Quoted 8 lines

builtin/ls-files.c static void prune_index(struct index_state *istate, · line 368

Quoted 5 lines
builtin/ls-files.c +1 -1
-	if (!prefix)
+	if (!prefix || !istate->cache_nr)
  		return;
  	pos = index_name_pos(istate, prefix, prefixlen);
  	if (pos < 0)

"git am" complained that this does not apply to its blobs. Did you hand-edit?

I didn't, but perhaps I messed up the order of patches? MOVE_ARRAY patch 2 touches the same file, but I wouldn't expect the two changes to conflict. So not sure what's going on.

René

#5 lore raw
Quoted 17 lines · René Scharfe
Quoted 8 lines
Quoted 8 lines
builtin/ls-files.c +0 -0
index b8514a0029..adf572da68 100644
@@ -362,7 +362,7 @@ static void prune_index(struct index_state *istate,
  	int pos;
  	unsigned int first, last;
 
Quoted 8 lines

builtin/ls-files.c static void prune_index(struct index_state *istate, · line 368

Quoted 5 lines
builtin/ls-files.c +1 -1
-	if (!prefix)
+	if (!prefix || !istate->cache_nr)
  		return;
  	pos = index_name_pos(istate, prefix, prefixlen);
  	if (pos < 0)

"git am" complained that this does not apply to its blobs. Did you hand-edit?

Quoted 3 lines

I didn't, but perhaps I messed up the order of patches? MOVE_ARRAY patch 2 touches the same file, but I wouldn't expect the two changes to conflict. So not sure what's going on.

For some reason there's an extra space before the tab on each of the context lines. MUA issue or cut-and-paste, maybe?

-Peff

#8 lore raw

PATCH 1/1 ls-files: don't try to prune an empty index

In reply to René Scharfe

Exit early when asked to prune an index that contains no entries to begin with. This avoids pointer arithmetic on istate->cache, which is possibly NULL in that case.

Found with Clang's UBSan.

---
Messed up whitespace when sending this the first time.

 builtin/ls-files.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
builtin/ls-files.c +1 -1
index b8514a0029..adf572da68 100644
@@ -362,7 +362,7 @@ static void prune_index(struct index_state *istate,
 	int pos;
 	unsigned int first, last;
 
-	if (!prefix)
+	if (!prefix || !istate->cache_nr)
 		return;
 	pos = index_name_pos(istate, prefix, prefixlen);
 	if (pos < 0)
Signature · 1 line
2.13.3
#6 lore raw
In reply to Jeff King

Am 16.07.2017 um 13:08 schrieb Jeff King:

Quoted 19 lines · René Scharfe
Quoted 18 lines
Quoted 17 lines
Quoted 6 lines
builtin/ls-files.c +0 -0
index b8514a0029..adf572da68 100644

builtin/ls-files.c static void prune_index(struct index_state *istate, · line 368

Quoted 8 lines
builtin/ls-files.c +1 -1
@@ -362,7 +362,7 @@ static void prune_index(struct index_state *istate,
   	int pos;
   	unsigned int first, last;
 
-	if (!prefix)
+	if (!prefix || !istate->cache_nr)
   		return;
   	pos = index_name_pos(istate, prefix, prefixlen);
   	if (pos < 0)

"git am" complained that this does not apply to its blobs. Did you hand-edit?

Quoted 6 lines
Quoted 3 lines

I didn't, but perhaps I messed up the order of patches? MOVE_ARRAY patch 2 touches the same file, but I wouldn't expect the two changes to conflict. So not sure what's going on.

For some reason there's an extra space before the tab on each of the context lines. MUA issue or cut-and-paste, maybe?

That's possible. Will resend.

#7 lore raw

Am 16.07.2017 um 13:15 schrieb René Scharfe:

Quoted 21 lines

Am 16.07.2017 um 13:08 schrieb Jeff King:

Quoted 20 lines · René Scharfe
Quoted 19 lines
Quoted 18 lines
Quoted 14 lines
builtin/ls-files.c +1 -1
index b8514a0029..adf572da68 100644
@@ -362,7 +362,7 @@ static void prune_index(struct index_state 
 *istate,
       int pos;
       unsigned int first, last;
 
-    if (!prefix)
+    if (!prefix || !istate->cache_nr)
           return;
       pos = index_name_pos(istate, prefix, prefixlen);
Quoted 1 line
       if (pos < 0)

"git am" complained that this does not apply to its blobs. Did you hand-edit?

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

I didn't, but perhaps I messed up the order of patches? MOVE_ARRAY patch 2 touches the same file, but I wouldn't expect the two changes to conflict. So not sure what's going on.

For some reason there's an extra space before the tab on each of the context lines. MUA issue or cut-and-paste, maybe?

That's possible. Will resend.

... pressed Send to fast. Thanks for reporting the broken patch!

I use the extension Toggle Word Wrap with Thunderbird, but it wraps by default with no way to change that, so I forgot toggling this time. Grr, I've had enough of this! Went past the warranty warning and set mailnews.wraplength=0 now.

René