Skip to contentSkip to conversation

string-list: allow passing NULL for `get_entry_index`

PATCH In review shejialuo 3 participants 6 messages Sep 7, 2025 – Sep 15, 2025

lore
Share & tools
lore

shejialuo

author
#0 lore raw

PATCH 1/4 string-list: allow passing NULL for `get_entry_index`

Callers of get_entry_index() are required to pass a non-NULL exact_match parameter to receive information about whether an exact match is found. However, in some cases, callers only need the index position.

Let's allow callers to pass NULL for the exact_match parameter when they don't need this information, reducing unnecessary variable declarations in calling code.

---
 string-list.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
string-list.c +4 -2
index 343cf1ca90..bf358d1a5c 100644
@@ -29,12 +29,14 @@ static size_t get_entry_index(const struct string_list *list, const char *string
 		else if (compare > 0)
 			left = middle + 1;
 		else {
-			*exact_match = 1;
+			if (exact_match)
+				*exact_match = 1;
 			return middle;
 		}
 	}
 
-	*exact_match = 0;
+	if (exact_match)
+		*exact_match = 0;
 	return right;
 }
 
Signature · 1 line
2.51.0
#1 lore raw
Quoted 5 lines · shejialuo

Callers of get_entry_index() are required to pass a non-NULL exact_match parameter to receive information about whether an exact match is found. However, in some cases, callers only need the index position.

Quoted 8 lines

Let's allow callers to pass NULL for the exact_match parameter when they don't need this information, reducing unnecessary variable declarations in calling code.

---
 string-list.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

I do not quite see the point of adding these conditional assignments to clutter the control flow. What benefit do these callers gain by not having to have a throw-away int variable on the stack and passing its address to the call chain?

Quoted 20 lines
string-list.c +4 -2
index 343cf1ca90..bf358d1a5c 100644
@@ -29,12 +29,14 @@ static size_t get_entry_index(const struct string_list *list, const char *string
 		else if (compare > 0)
 			left = middle + 1;
 		else {
-			*exact_match = 1;
+			if (exact_match)
+				*exact_match = 1;
 			return middle;
 		}
 	}
 
-	*exact_match = 0;
+	if (exact_match)
+		*exact_match = 0;
 	return right;
 }
#2 lore raw
Quoted 19 lines · Junio C Hamano
Quoted 5 lines · shejialuo

Callers of get_entry_index() are required to pass a non-NULL exact_match parameter to receive information about whether an exact match is found. However, in some cases, callers only need the index position.

Quoted 8 lines

Let's allow callers to pass NULL for the exact_match parameter when they don't need this information, reducing unnecessary variable declarations in calling code.

---
string-list.c | 6 ++++--

1 file changed, 4 insertions(+), 2 deletions(-)
Quoted 4 lines

I do not quite see the point of adding these conditional assignments to clutter the control flow. What benefit do these callers gain by not having to have a throw-away int variable on the stack and passing its address to the call chain?

Wouldn’t the point be that (at the cost of slightly more interesting library code) callers don’t need to introduce extra ceremony for results they don’t use? In other words, take the boilerplate and push it down rather than up?

#3 lore raw
Quoted 18 lines · Ben Knoble
Quoted 18 lines · Junio C Hamano
Quoted 13 lines · shejialuo

Callers of get_entry_index() are required to pass a non-NULL exact_match parameter to receive information about whether an exact match is found. However, in some cases, callers only need the index position.

Let's allow callers to pass NULL for the exact_match parameter when they don't need this information, reducing unnecessary variable declarations in calling code.

---
string-list.c | 6 ++++--

1 file changed, 4 insertions(+), 2 deletions(-)
Quoted 6 lines
Quoted 4 lines

I do not quite see the point of adding these conditional assignments to clutter the control flow. What benefit do these callers gain by not having to have a throw-away int variable on the stack and passing its address to the call chain?

Wouldn’t the point be that (at the cost of slightly more interesting library code) callers don’t need to introduce extra ceremony for results they don’t use? In other words, take the boilerplate and push it down rather than up?

Yeah, but the point is in what situation does a caller *not* need to be able to tell between "we found an existing one and this is its index" vs "there is no such thing, but here is where it *would* fit if it were placed in the array"? I do not think of any, so I doubted if it makes much sense to complicate the interface to make it optional.

#4 lore raw
Quoted 28 lines · Junio C Hamano
Quoted 23 lines · Ben Knoble
Quoted 22 lines
Quoted 1 line · Junio C Hamano
Quoted 5 lines · shejialuo

Callers of get_entry_index() are required to pass a non-NULL exact_match parameter to receive information about whether an exact match is found. However, in some cases, callers only need the index position.

Quoted 8 lines

Let's allow callers to pass NULL for the exact_match parameter when they don't need this information, reducing unnecessary variable declarations in calling code.

---
string-list.c | 6 ++++--

1 file changed, 4 insertions(+), 2 deletions(-)

I do not quite see the point of adding these conditional assignments to clutter the control flow. What benefit do these callers gain by not having to have a throw-away int variable on the stack and passing its address to the call chain?

Quoted 9 lines
Quoted 1 line

Wouldn’t the point be that (at the cost of slightly more interesting library code) callers don’t need to introduce extra ceremony for results they don’t use? In other words, take the boilerplate and push it down rather than up?

Yeah, but the point is in what situation does a caller *not* need to be able to tell between "we found an existing one and this is its index" vs "there is no such thing, but here is where it *would* fit if it were placed in the array"? I do not think of any, so I doubted if it makes much sense to complicate the interface to make it optional.

Good question! Thanks for clarifying.

author
#5 lore raw
In reply to Junio C Hamano
Quoted 16 lines · Junio C Hamano
Quoted 5 lines · shejialuo

Callers of get_entry_index() are required to pass a non-NULL exact_match parameter to receive information about whether an exact match is found. However, in some cases, callers only need the index position.

Quoted 8 lines

Let's allow callers to pass NULL for the exact_match parameter when they don't need this information, reducing unnecessary variable declarations in calling code.

---
 string-list.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
Quoted 5 lines

I do not quite see the point of adding these conditional assignments to clutter the control flow. What benefit do these callers gain by not having to have a throw-away int variable on the stack and passing its address to the call chain?

I want to make the commit small. However, I have introduced confusion to the reviewers. I would rebase the first commit and second commit into one commit to improve this.

Thanks,
Jialuo