Skip to contentSkip to conversation

mailsplit.c: remove dead code

PATCH In review Stefan Beller 6 participants 9 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mailsplit.c: remove dead code

This was found by coverity. (Id: 290001)

the variable 'output' is only assigned to a value inequal to NUL, after all gotos to the corrupt label.
Therefore we can conclude the two removed lines are actually dead code.

---
 builtin/mailsplit.c | 2 --
 1 file changed, 2 deletions(-)
builtin/mailsplit.c +0 -2
index 06296d4..b499014 100644
@@ -93,8 +93,6 @@ static int split_one(FILE *mbox, const char *name, int allow_bare)
 	return status;
 
  corrupt:
-	if (output)
-		fclose(output);
 	unlink(name);
 	fprintf(stderr, "corrupt mailbox\n");
 	exit(1);
Signature · 1 line
2.1.0.rc2
#1 lore raw

Am 11.08.2014 um 23:11 schrieb Stefan Beller:

Quoted 5 lines

This was found by coverity. (Id: 290001)

the variable 'output' is only assigned to a value inequal to NUL, after all gotos to the corrupt label.
Therefore we can conclude the two removed lines are actually dead code.

After reading the above for the first time I thought it meant the opposite of what's actually going on. Perhaps it's the placement of "only", the comma or a flawed understanding of grammar on my part?

In any case, there is only one way to reach the label named corrupt, and the variable named output is always NULL if that branch is taken. That means the removed code was a no-op. With those two lines gone you also don't need to initialize output anymore, by the way.

And since there is only a single goto, you could move the three remaining error handling lines up to the if statement. Keeping condition and dependent code together would be an improvement, I think.

Quoted 19 lines
---
  builtin/mailsplit.c | 2 --
  1 file changed, 2 deletions(-)
builtin/mailsplit.c +0 -2
index 06296d4..b499014 100644
@@ -93,8 +93,6 @@ static int split_one(FILE *mbox, const char *name, int allow_bare)
  	return status;
 
   corrupt:
-	if (output)
-		fclose(output);
  	unlink(name);
  	fprintf(stderr, "corrupt mailbox\n");
  	exit(1);
#2 lore raw

PATCH 1/1 mailsplit.c: remove dead code

This was found by coverity. (Id: 290001)

The variable 'output' is assigned to a value after all gotos to the corrupt label.

Remove the goto by moving the errorhandling code to the condition, which detects the error.

---
 builtin/mailsplit.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)

If I understood correctly, this is what you had in mind?

Thanks,
Stefan

builtin/mailsplit.c +6 -11
index 06296d4..763cda0 100644
@@ -53,14 +53,16 @@ static int keep_cr;
  */
 static int split_one(FILE *mbox, const char *name, int allow_bare)
 {
-	FILE *output = NULL;
+	FILE *output;
 	int fd;
 	int status = 0;
 	int is_bare = !is_from_line(buf.buf, buf.len);
 
-	if (is_bare && !allow_bare)
-		goto corrupt;
-
+	if (is_bare && !allow_bare) {
+		unlink(name);
+		fprintf(stderr, "corrupt mailbox\n");
+		exit(1);
+	}
 	fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
 	if (fd < 0)
 		die_errno("cannot open output file '%s'", name);
@@ -91,13 +93,6 @@ static int split_one(FILE *mbox, const char *name, int allow_bare)
 	}
 	fclose(output);
 	return status;
-
- corrupt:
-	if (output)
-		fclose(output);
-	unlink(name);
-	fprintf(stderr, "corrupt mailbox\n");
-	exit(1);
 }
 
 static int populate_maildir_list(struct string_list *list, const char *path)
Signature · 1 line
2.1.0.rc2
Reviewed-by on v1 1/1
#3 lore raw
Quoted 1 line · Stefan Beller

This was found by coverity. (Id: 290001)

[...]

Quoted 5 lines
---
 builtin/mailsplit.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)

The idea is to simplify error handling (removing cleanup of the 'output' var) by making it more obvious that there is only one kind of error, right?

For what it's worth, it looks good to me (and much clearer than the last version). It's always possible to reintroduce goto-based error handling later if another kind of error is introduced, and in the meantime this is simpler and does not change behavior.

Thanks.

#4 lore raw
In reply to Stefan Beller

builtin/mailsplit.c static int keep_cr; · line 65

Quoted 5 lines · Stefan Beller
builtin/mailsplit.c +5 -0
+	if (is_bare && !allow_bare) {
+		unlink(name);
+		fprintf(stderr, "corrupt mailbox\n");
+		exit(1);
+	}

Not directly related to your patch, but shouldn't this be using

die(_("corrupt mailbox"));

instead?

Signature · 2 lines
Matthieu Moy
http://www-verimag.imag.fr/~moy/
#5 lore raw
Quoted 10 lines · Matthieu Moy

builtin/mailsplit.c static int keep_cr; · line 65

Quoted 5 lines · Stefan Beller
builtin/mailsplit.c +5 -0
+	if (is_bare && !allow_bare) {
+		unlink(name);
+		fprintf(stderr, "corrupt mailbox\n");
+		exit(1);
+	}

Not directly related to your patch, but shouldn't this be using

Quoted 3 lines

die(_("corrupt mailbox"));

instead?

Doesn't the exit status of mailsplit matter to its existing invokers?

#6 lore raw

... answering to myself, the only invoker does not seem to care, so I do not mind if the fprintf/exit gets turned into die() in a follow-up patch.

Thanks.

Quoted 18 lines · Junio C Hamano
Quoted 8 lines · Matthieu Moy

builtin/mailsplit.c static int keep_cr; · line 65

Quoted 5 lines · Stefan Beller
builtin/mailsplit.c +5 -0
+    if (is_bare && !allow_bare) {
+            unlink(name);
+            fprintf(stderr, "corrupt mailbox\n");
+            exit(1);
+    }
Quoted 5 lines

Not directly related to your patch, but shouldn't this be using

die(_("corrupt mailbox"));

instead?

Doesn't the exit status of mailsplit matter to its existing invokers?

#7 lore raw
In reply to Junio C Hamano
Quoted 12 lines · Junio C Hamano
Quoted 10 lines · Matthieu Moy

builtin/mailsplit.c static int keep_cr; · line 65

Quoted 5 lines · Stefan Beller
builtin/mailsplit.c +5 -0
+	if (is_bare && !allow_bare) {
+		unlink(name);
+		fprintf(stderr, "corrupt mailbox\n");
+		exit(1);
+	}

Not directly related to your patch, but shouldn't this be using

Quoted 6 lines
Quoted 3 lines

die(_("corrupt mailbox"));

instead?

Doesn't the exit status of mailsplit matter to its existing invokers?

Not within Git. I doubt anybody checks if the exit status is 1 and relies on that for corrupt mailboxes, but OTOH, changing the code may not be worth the trouble.

Signature · 2 lines
Matthieu Moy
http://www-verimag.imag.fr/~moy/
#8 lore raw
In reply to René Scharfe
Quoted 11 lines · René Scharfe

Am 11.08.2014 um 23:11 schrieb Stefan Beller:

Quoted 5 lines

This was found by coverity. (Id: 290001)

the variable 'output' is only assigned to a value inequal to NUL, after all gotos to the corrupt label.
Therefore we can conclude the two removed lines are actually dead code.

After reading the above for the first time I thought it meant the opposite of what's actually going on. Perhaps it's the placement of "only", the comma or a flawed understanding of grammar on my part?

Quoted 8 lines

In any case, there is only one way to reach the label named corrupt, and the variable named output is always NULL if that branch is taken. That means the removed code was a no-op. With those two lines gone you also don't need to initialize output anymore, by the way.

And since there is only a single goto, you could move the three remaining error handling lines up to the if statement. Keeping condition and dependent code together would be an improvement, I think.

I think that would be a correct refactoring of the current code, but I have to wonder why the other die cases are not using "goto corrupt" in the first place.

The other thing this code path does is unlink the file "name". In the current code, this is _also_ a noop. We "goto corrupt" before we actually open the output file. So like the fclose(output), it is cleaning up an operation that was never started. It can just go away.

But the bigger question is: should the other code paths be cleaning up the file? It probably doesn't matter, as mailsplit is typically run in a temporary directory in the first place, so it is up to the caller to clean up any half-formed cruft. And if we did want to clean up cruft, we should probably do it with an atexit/signal handler to catch more cases.

Given that we are not cleaning up now and nobody has complained, I'd be inclined to say we should not. And the unlink can just go away, and all errors can just call die().

-Peff