Skip to contentSkip to conversation

ident.c: fix LGTM warning on the possible abuse of the '=' operator

PATCH In review Elia Pinto 6 participants 15 messages Nov 4, 2019 – Nov 7, 2019

lore
Share & tools
lore

Elia Pinto

author
#0 lore raw

PATCH 30/32 ident.c: fix LGTM warning on the possible abuse of the '=' operator

Fix the LGTM warning of the rule that finds uses of the assignment operator = in places where the equality operator == would make more sense.

---
 ident.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
ident.c +8 -5
index e666ee4e59..07f2f03b0a 100644
@@ -172,12 +172,15 @@ const char *ident_default_email(void)
 			strbuf_addstr(&git_default_email, email);
 			committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
 			author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
-		} else if ((email = query_user_email()) && email[0]) {
-			strbuf_addstr(&git_default_email, email);
-			free((char *)email);
-		} else
-			copy_email(xgetpwuid_self(&default_email_is_bogus),
+		} else {
+			email = query_user_email();
+			if (email && email[0]) {
+				strbuf_addstr(&git_default_email, email);
+				free((char *)email);
+			} else
+				copy_email(xgetpwuid_self(&default_email_is_bogus),
 				   &git_default_email, &default_email_is_bogus);
+		}
 		strbuf_trim(&git_default_email);
 	}
 	return git_default_email.buf;
Signature · 1 line
2.24.0.rc0.467.g566ccdd3e4.dirty
author
#1 lore raw

PATCH 31/32 commit-graph.c: fix code that could convert the result of an integer multiplication to a larger type

Fix the LGTM warning fired by the rule that finds code that could convert the result of an integer multiplication to a larger type. Since the conversion applies after the multiplication, arithmetic overflow may still occur.

---
 commit-graph.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
commit-graph.c +4 -4
index a0f868522b..0be15f1cd4 100644
@@ -1415,8 +1415,8 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 
 	chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
 	chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
-	chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr;
-	chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr;
+	chunk_offsets[2] = chunk_offsets[1] + (uint64_t)hashsz * ctx->commits.nr;
+	chunk_offsets[3] = chunk_offsets[2] + ((uint64_t)hashsz + 16) * ctx->commits.nr;
 
 	num_chunks = 3;
 	if (ctx->num_extra_edges) {
@@ -1426,7 +1426,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 	}
 	if (ctx->num_commit_graphs_after > 1) {
 		chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
-						hashsz * (ctx->num_commit_graphs_after - 1);
+						(uint64_t)hashsz * (ctx->num_commit_graphs_after - 1);
 		num_chunks++;
 	}
 
@@ -1454,7 +1454,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 			    num_chunks);
 		ctx->progress = start_delayed_progress(
 			progress_title.buf,
-			num_chunks * ctx->commits.nr);
+			(uint64_t)num_chunks * ctx->commits.nr);
 	}
 	write_graph_chunk_fanout(f, ctx);
 	write_graph_chunk_oids(f, hashsz, ctx);
Signature · 1 line
2.24.0.rc0.467.g566ccdd3e4.dirty
#2 lore raw

Re: [PATCH 31/32] commit-graph.c: fix code that could convert the result of an integer multiplication to a larger type

Quoted 3 lines · Elia Pinto

Fix the LGTM warning fired by the rule that finds code that could convert the result of an integer multiplication to a larger type. Since the conversion applies after the multiplication, arithmetic overflow may still occur.

Overly long?

commit-graph.c static int write_commit_graph_file(struct write_commit_graph_context *ctx) · line 1419

Quoted 6 lines
commit-graph.c +2 -2
 	chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
 	chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
-	chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr;
-	chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr;
+	chunk_offsets[2] = chunk_offsets[1] + (uint64_t)hashsz * ctx->commits.nr;
+	chunk_offsets[3] = chunk_offsets[2] + ((uint64_t)hashsz + 16) * ctx->commits.nr;

chunk_offsets[] is u64[], while hashsz and is uint and ctx->commits.nr is int. OK.

commit-graph.c static int write_commit_graph_file(struct write_commit_graph_context *ctx) · line 1429

Quoted 6 lines
commit-graph.c +1 -1
@@ -1426,7 +1426,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 	}
 	if (ctx->num_commit_graphs_after > 1) {
 		chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
-						hashsz * (ctx->num_commit_graphs_after - 1);
+						(uint64_t)hashsz * (ctx->num_commit_graphs_after - 1);

Likewise.

commit-graph.c static int write_commit_graph_file(struct write_commit_graph_context *ctx) · line 1457

Quoted 6 lines
commit-graph.c +1 -1
@@ -1454,7 +1454,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 			    num_chunks);
 		ctx->progress = start_delayed_progress(
 			progress_title.buf,
-			num_chunks * ctx->commits.nr);
+			(uint64_t)num_chunks * ctx->commits.nr);

Hmph, do we need this? I understand that the second parameter to the callee is u64, so the caller needs to come up with u64 without overflow, but doesn't that automatically get promoted?

Quoted 3 lines
commit-graph.c +0 -0
 	}
 	write_graph_chunk_fanout(f, ctx);
 	write_graph_chunk_oids(f, hashsz, ctx);
#3 lore raw

Re: [PATCH 31/32] commit-graph.c: fix code that could convert the result of an integer multiplication to a larger type

Quoted 10 lines · Junio C Hamano

commit-graph.c static int write_commit_graph_file(struct write_commit_graph_context *ctx) · line 1457

Quoted 6 lines
commit-graph.c +1 -1
@@ -1454,7 +1454,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 			    num_chunks);
 		ctx->progress = start_delayed_progress(
 			progress_title.buf,
-			num_chunks * ctx->commits.nr);
+			(uint64_t)num_chunks * ctx->commits.nr);

Hmph, do we need this? I understand that the second parameter to the callee is u64, so the caller needs to come up with u64 without overflow, but doesn't that automatically get promoted?

Neither num_chunks nor ctx->commits.nr is promoted because both of them are int. The result of `num_chunks * ctx->commits.nr' will be int and will be promoted to u64 to pass to caller.

Signature · 1 line
Danh
#4 lore raw

Re: [PATCH 31/32] commit-graph.c: fix code that could convert the result of an integer multiplication to a larger type

Quoted 12 lines · Danh Doan
Quoted 10 lines · Junio C Hamano

commit-graph.c static int write_commit_graph_file(struct write_commit_graph_context *ctx) · line 1457

Quoted 6 lines
commit-graph.c +1 -1
@@ -1454,7 +1454,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 			    num_chunks);
 		ctx->progress = start_delayed_progress(
 			progress_title.buf,
-			num_chunks * ctx->commits.nr);
+			(uint64_t)num_chunks * ctx->commits.nr);

Hmph, do we need this? I understand that the second parameter to the callee is u64, so the caller needs to come up with u64 without overflow, but doesn't that automatically get promoted?

Quoted 3 lines

Neither num_chunks nor ctx->commits.nr is promoted because both of them are int. The result of `num_chunks * ctx->commits.nr' will be int and will be promoted to u64 to pass to caller.

Ah, yes. Thanks.

The commit title is about "integer multiplication", but can the same issue arise with addition and subtraction as well, by the way?

#5 lore raw

Re: [PATCH 31/32] commit-graph.c: fix code that could convert the result of an integer multiplication to a larger type

Quoted 14 lines · Junio C Hamano
Quoted 12 lines · Danh Doan
Quoted 10 lines · Junio C Hamano

commit-graph.c static int write_commit_graph_file(struct write_commit_graph_context *ctx) · line 1457

Quoted 6 lines
commit-graph.c +1 -1
@@ -1454,7 +1454,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 			    num_chunks);
 		ctx->progress = start_delayed_progress(
 			progress_title.buf,
-			num_chunks * ctx->commits.nr);
+			(uint64_t)num_chunks * ctx->commits.nr);

Hmph, do we need this? I understand that the second parameter to the callee is u64, so the caller needs to come up with u64 without overflow, but doesn't that automatically get promoted?

Quoted 8 lines
Quoted 3 lines

Neither num_chunks nor ctx->commits.nr is promoted because both of them are int. The result of `num_chunks * ctx->commits.nr' will be int and will be promoted to u64 to pass to caller.

Ah, yes. Thanks.

The commit title is about "integer multiplication", but can the same issue arise with addition and subtraction as well, by the way?

Yes, the same issue will arise with all binary (and ternary) arithmetic operators (+, -, *, /, %, ^, &, |, <<, >> and ?:).

IIRC, gcc doesn't have any warning for this kind of issue.

Microsoft Visual Studio (2017+) has C26451 for this.
https://docs.microsoft.com/en-us/visualstudio/code-quality/c26451?view=vs-2017
If our friends at Microsoft could help, we can check the remaining one in our codebase.

Signature · 1 line
Danh
#6 lore raw

Re: [PATCH 31/32] commit-graph.c: fix code that could convert the result of an integer multiplication to a larger type

Hi Danh,

Quoted 27 lines · Danh Doan
Quoted 14 lines · Junio C Hamano
Quoted 12 lines · Danh Doan
Quoted 10 lines · Junio C Hamano

commit-graph.c static int write_commit_graph_file(struct write_commit_graph_context *ctx) · line 1457

Quoted 6 lines
commit-graph.c +1 -1
@@ -1454,7 +1454,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 			    num_chunks);
 		ctx->progress = start_delayed_progress(
 			progress_title.buf,
-			num_chunks * ctx->commits.nr);
+			(uint64_t)num_chunks * ctx->commits.nr);

Hmph, do we need this? I understand that the second parameter to the callee is u64, so the caller needs to come up with u64 without overflow, but doesn't that automatically get promoted?

Quoted 8 lines
Quoted 3 lines

Neither num_chunks nor ctx->commits.nr is promoted because both of them are int. The result of `num_chunks * ctx->commits.nr' will be int and will be promoted to u64 to pass to caller.

Ah, yes. Thanks.

The commit title is about "integer multiplication", but can the same issue arise with addition and subtraction as well, by the way?

Yes, the same issue will arise with all binary (and ternary) arithmetic operators (+, -, *, /, %, ^, &, |, <<, >> and ?:).

Quoted 6 lines

IIRC, gcc doesn't have any warning for this kind of issue.

Microsoft Visual Studio (2017+) has C26451 for this.
https://docs.microsoft.com/en-us/visualstudio/code-quality/c26451?view=vs-2017
If our friends at Microsoft could help, we can check the remaining one in our codebase.

I am a bit busy right now, but it _was_ my hope that adding a Visual Studio job to our Azure Pipeline would enable everybody to perform tests like this one.

In other words, I _think_ that you can add something like

	#pragma warning(enable: 26451)

to compat/msvc.h and then open a PR at https://github.com/git/git, the Azure Pipeline should produce precisely what you want.

(If I were you, I would also try to save some CO2 by ripping out all jobs except the vs_build one from azure-pipelines.yml.)

Ciao,
Dscho

#7 lore raw

Re: [PATCH 31/32] commit-graph.c: fix code that could convert the result of an integer multiplication to a larger type

In reply to Danh Doan

Hi Danh,

Quoted 12 lines · Danh Doan
Quoted 10 lines · Junio C Hamano

commit-graph.c static int write_commit_graph_file(struct write_commit_graph_context *ctx) · line 1457

Quoted 6 lines
commit-graph.c +1 -1
@@ -1454,7 +1454,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 			    num_chunks);
 		ctx->progress = start_delayed_progress(
 			progress_title.buf,
-			num_chunks * ctx->commits.nr);
+			(uint64_t)num_chunks * ctx->commits.nr);

Hmph, do we need this? I understand that the second parameter to the callee is u64, so the caller needs to come up with u64 without overflow, but doesn't that automatically get promoted?

Quoted 3 lines

Neither num_chunks nor ctx->commits.nr is promoted because both of them are int. The result of `num_chunks * ctx->commits.nr' will be int and will be promoted to u64 to pass to caller.

If you up-cast, maybe do the second operand so that nobody has to spend cycles trying to remember whether the cast or the arithmetic operand bind stronger? I.e. num_chunks * (uint64_t)ctx->commits.nr.

Ciao,
Dscho

author
#8 lore raw

PATCH 32/32 date.c: fix code that may overflow 'int' before it is converted to 'time_t'

In reply to Elia Pinto

Fix the LGTM warning fired by the rule that finds code that could convert the result of an integer multiplication to a larger type. Since the conversion applies after the multiplication, arithmetic overflow may still occur.

---
 date.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
date.c +1 -1
index 041db7db4e..b8dcbdbb0e 100644
@@ -1172,7 +1172,7 @@ static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm
 	while (tl->type) {
 		int len = strlen(tl->type);
 		if (match_string(date, tl->type) >= len-1) {
-			update_tm(tm, now, tl->length * *num);
+			update_tm(tm, now, (time_t)tl->length * *num);
 			*num = 0;
 			*touched = 1;
 			return end;
Signature · 1 line
2.24.0.rc0.467.g566ccdd3e4.dirty
#9 lore raw
In reply to Elia Pinto
Quoted 1 line · Elia Pinto

Fix the LGTM warning of the rule that finds uses of the assignment

What is an "LGTM warning"?

I think including the actual compiler warning in the commit message would be great.

Quoted 36 lines

operator = in places where the equality operator == would make more sense.

---
 ident.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
ident.c +8 -5
index e666ee4e59..07f2f03b0a 100644
@@ -172,12 +172,15 @@ const char *ident_default_email(void)
 			strbuf_addstr(&git_default_email, email);
 			committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
 			author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
-		} else if ((email = query_user_email()) && email[0]) {
-			strbuf_addstr(&git_default_email, email);
-			free((char *)email);
-		} else
-			copy_email(xgetpwuid_self(&default_email_is_bogus),
+		} else {
+			email = query_user_email();
+			if (email && email[0]) {
+				strbuf_addstr(&git_default_email, email);
+				free((char *)email);
+			} else
+				copy_email(xgetpwuid_self(&default_email_is_bogus),
 				   &git_default_email, &default_email_is_bogus);
+		}
 		strbuf_trim(&git_default_email);
 	}
 	return git_default_email.buf;
Signature · 1 line
2.24.0.rc0.467.g566ccdd3e4.dirty
author
#10 lore raw

Il giorno lun 4 nov 2019 alle ore 11:26 SZEDER Gábor <[email protected]> ha scritto:

Quoted 8 lines · Elia Pinto
Quoted 1 line

Fix the LGTM warning of the rule that finds uses of the assignment

What is an "LGTM warning"?

I think including the actual compiler warning in the commit message would be great.

Yes indeed. I thought I did it, do you think i can do better? Thanks

https://lgtm.com/rules/2158670641/

Quoted 37 lines
Quoted 37 lines

operator = in places where the equality operator == would make more sense.

---
 ident.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
ident.c +8 -6
index e666ee4e59..07f2f03b0a 100644
@@ -172,12 +172,15 @@ const char *ident_default_email(void)
                      strbuf_addstr(&git_default_email, email);
                      committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
                      author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
-             } else if ((email = query_user_email()) && email[0]) {
-                     strbuf_addstr(&git_default_email, email);
-                     free((char *)email);
-             } else
-                     copy_email(xgetpwuid_self(&default_email_is_bogus),
+             } else {
+                     email = query_user_email();
+                     if (email && email[0]) {
+                             strbuf_addstr(&git_default_email, email);
+                             free((char *)email);
+                     } else
+                             copy_email(xgetpwuid_self(&default_email_is_bogus),
                                 &git_default_email, &default_email_is_bogus);
+             }
              strbuf_trim(&git_default_email);
      }
      return git_default_email.buf;
--

2.24.0.rc0.467.g566ccdd3e4.dirty

#11 lore raw

Hi Elia

Quoted 6 lines · Elia Pinto

Il giorno lun 4 nov 2019 alle ore 11:26 SZEDER Gábor <[email protected]> ha scritto:

Quoted 4 lines · Elia Pinto
Quoted 1 line

Fix the LGTM warning of the rule that finds uses of the assignment

Quoted 7 lines
Quoted 4 lines

What is an "LGTM warning"?

I think including the actual compiler warning in the commit message would be great.

Yes indeed. I thought I did it, do you think i can do better? Thanks

https://lgtm.com/rules/2158670641/

It would have been helpful to explain that LGTM was a static analyser for those of us who did not know. As far the patch is concerned I'm not convinced it is an improvement. There are loads of places where git uses this pattern ("git grep 'if (([^=)]*=[^)]*)' | wc" shows 212). So long as the assignment is inside its own set of parentheses it's safe and gcc will warn if the parentheses are missing.

Best Wishes

Phillip

Quoted 37 lines
Quoted 37 lines
Quoted 37 lines

operator = in places where the equality operator == would make more sense.

---
  ident.c | 13 ++++++++-----
  1 file changed, 8 insertions(+), 5 deletions(-)
ident.c +8 -6
index e666ee4e59..07f2f03b0a 100644
@@ -172,12 +172,15 @@ const char *ident_default_email(void)
                       strbuf_addstr(&git_default_email, email);
                       committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
                       author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
-             } else if ((email = query_user_email()) && email[0]) {
-                     strbuf_addstr(&git_default_email, email);
-                     free((char *)email);
-             } else
-                     copy_email(xgetpwuid_self(&default_email_is_bogus),
+             } else {
+                     email = query_user_email();
+                     if (email && email[0]) {
+                             strbuf_addstr(&git_default_email, email);
+                             free((char *)email);
+                     } else
+                             copy_email(xgetpwuid_self(&default_email_is_bogus),
                                  &git_default_email, &default_email_is_bogus);
+             }
               strbuf_trim(&git_default_email);
       }
       return git_default_email.buf;
--

2.24.0.rc0.467.g566ccdd3e4.dirty

author
#12 lore raw

Il giorno lun 4 nov 2019 alle ore 16:11 Phillip Wood <[email protected]> ha scritto:

Quoted 16 lines

Hi Elia

Quoted 12 lines · Elia Pinto

Il giorno lun 4 nov 2019 alle ore 11:26 SZEDER Gábor <[email protected]> ha scritto:

Quoted 8 lines · Elia Pinto
Quoted 1 line

Fix the LGTM warning of the rule that finds uses of the assignment

What is an "LGTM warning"?

I think including the actual compiler warning in the commit message would be great.

Yes indeed. I thought I did it, do you think i can do better? Thanks

Quoted 9 lines
Quoted 1 line

https://lgtm.com/rules/2158670641/

It would have been helpful to explain that LGTM was a static analyser for those of us who did not know. As far the patch is concerned I'm not convinced it is an improvement. There are loads of places where git uses this pattern ("git grep 'if (([^=)]*=[^)]*)' | wc" shows 212). So long as the assignment is inside its own set of parentheses it's safe and gcc will warn if the parentheses are missing.

LGTM only expresses a warning (actually it signals a possible error) that refers directly to this
https://cwe.mitre.org/data/definitions/481.html. On which we can hardly disagree, beyond the authority of the source. The reason LGTM only identifies this as an error, and not the various apparently analogous cases in the code I don't know, but it would certainly be interesting to find out.

Thanks for the review

Quoted 41 lines

Best Wishes

Phillip

Quoted 37 lines
Quoted 37 lines
Quoted 37 lines

operator = in places where the equality operator == would make more sense.

---
  ident.c | 13 ++++++++-----
  1 file changed, 8 insertions(+), 5 deletions(-)
ident.c +8 -6
index e666ee4e59..07f2f03b0a 100644
@@ -172,12 +172,15 @@ const char *ident_default_email(void)
                       strbuf_addstr(&git_default_email, email);
                       committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
                       author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
-             } else if ((email = query_user_email()) && email[0]) {
-                     strbuf_addstr(&git_default_email, email);
-                     free((char *)email);
-             } else
-                     copy_email(xgetpwuid_self(&default_email_is_bogus),
+             } else {
+                     email = query_user_email();
+                     if (email && email[0]) {
+                             strbuf_addstr(&git_default_email, email);
+                             free((char *)email);
+                     } else
+                             copy_email(xgetpwuid_self(&default_email_is_bogus),
                                  &git_default_email, &default_email_is_bogus);
+             }
               strbuf_trim(&git_default_email);
       }
       return git_default_email.buf;
--

2.24.0.rc0.467.g566ccdd3e4.dirty

#13 lore raw
In reply to Elia Pinto

Elia Pinto <[email protected]> writes:

Did I miss the first 29 patches (with what I see in this patch, I do not know if I want to see them immediately, though ;-))?

Quoted 3 lines

Fix the LGTM warning of the rule that finds uses of the assignment operator = in places where the equality operator == would make more sense.

I know you did not mean that existing

	} else if ((email = query_user_email()) && email[0]) {

better reads if it were written like so:

	} else if ((email == query_user_email()) && email[0]) {

but that is the only way how that sentence can be read (at least to me) without looking at what the patch actually does.

As "email" has already been assigned to at this point in the codeflow, I agree that, to an eye that does not (and is not willing to spend cycles to) understand what the code is doing, the latter do look more natural: "If the value of the variable is the same as the return value of the query_user_email() function, and ...". And if "email" were a simpler arithmetic type it would have been even more (iow, it is clear "email" is a character string from "&& email[0]", so it is unlikely that "email == que()" is what the user intended).

So I am somewhat sympathetic to the "warnings" here, but not all that much, especially if squelching makes the codeflow harder to follow by introducing otherwise unnecessary nesting levels (like this patch did). I suspect that it might be possible to futher restructure the code in such a way that we do not have to do an assignment in a conditional without making the code deeply nested, and that may perhaps be worth doing.

But the thing is, assignment in a cascading conditional is so useful in avoiding pointless nesting of the code (imagine a reverse patch of this one---which is easy to sell as cleaning-up and streamlining the code).

So, I dunno.

Quoted 30 lines
---
 ident.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
ident.c +8 -5
index e666ee4e59..07f2f03b0a 100644
@@ -172,12 +172,15 @@ const char *ident_default_email(void)
 			strbuf_addstr(&git_default_email, email);
 			committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
 			author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
-		} else if ((email = query_user_email()) && email[0]) {
-			strbuf_addstr(&git_default_email, email);
-			free((char *)email);
-		} else
-			copy_email(xgetpwuid_self(&default_email_is_bogus),
+		} else {
+			email = query_user_email();
+			if (email && email[0]) {
+				strbuf_addstr(&git_default_email, email);
+				free((char *)email);
+			} else
+				copy_email(xgetpwuid_self(&default_email_is_bogus),
 				   &git_default_email, &default_email_is_bogus);
+		}
 		strbuf_trim(&git_default_email);
 	}
 	return git_default_email.buf;
#14 lore raw

Hi,

Quoted 37 lines · Junio C Hamano

Elia Pinto <[email protected]> writes:

Did I miss the first 29 patches (with what I see in this patch, I do not know if I want to see them immediately, though ;-))?

Quoted 3 lines

Fix the LGTM warning of the rule that finds uses of the assignment operator = in places where the equality operator == would make more sense.

I know you did not mean that existing

	} else if ((email = query_user_email()) && email[0]) {

better reads if it were written like so:

	} else if ((email == query_user_email()) && email[0]) {

but that is the only way how that sentence can be read (at least to me) without looking at what the patch actually does.

As "email" has already been assigned to at this point in the codeflow, I agree that, to an eye that does not (and is not willing to spend cycles to) understand what the code is doing, the latter do look more natural: "If the value of the variable is the same as the return value of the query_user_email() function, and ...". And if "email" were a simpler arithmetic type it would have been even more (iow, it is clear "email" is a character string from "&& email[0]", so it is unlikely that "email == que()" is what the user intended).

So I am somewhat sympathetic to the "warnings" here, but not all that much, especially if squelching makes the codeflow harder to follow by introducing otherwise unnecessary nesting levels (like this patch did). I suspect that it might be possible to futher restructure the code in such a way that we do not have to do an assignment in a conditional without making the code deeply nested, and that may perhaps be worth doing.

Quoted 6 lines

But the thing is, assignment in a cascading conditional is so useful in avoiding pointless nesting of the code (imagine a reverse patch of this one---which is easy to sell as cleaning-up and streamlining the code).

So, I dunno.

For what it's worth, my reaction was exactly the same: I understand how some developers might deem the assignment inside an if () condition undesirable, in Git's context I do strongly prefer the current code over the version proposed in this patch.

Thanks,
Johannes

Quoted 32 lines
Quoted 31 lines
---
 ident.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
ident.c +8 -5
index e666ee4e59..07f2f03b0a 100644
@@ -172,12 +172,15 @@ const char *ident_default_email(void)
 			strbuf_addstr(&git_default_email, email);
 			committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
 			author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
-		} else if ((email = query_user_email()) && email[0]) {
-			strbuf_addstr(&git_default_email, email);
-			free((char *)email);
-		} else
-			copy_email(xgetpwuid_self(&default_email_is_bogus),
+		} else {
+			email = query_user_email();
+			if (email && email[0]) {
+				strbuf_addstr(&git_default_email, email);
+				free((char *)email);
+			} else
+				copy_email(xgetpwuid_self(&default_email_is_bogus),
 				   &git_default_email, &default_email_is_bogus);
+		}
 		strbuf_trim(&git_default_email);
 	}
 	return git_default_email.buf;