Skip to contentSkip to conversation

unpack-trees.c: check return value of lstat()

PATCH In review Torsten Bögershausen 2 participants 5 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 unpack-trees.c: check return value of lstat()

Since commit f66caa "do not overwrite files in leading path" at one place lstat() was called but the result was not checked.

---
(While I was experimenting with something different, this was found by valgrind)
Please review the patch, comments wellcome.

 unpack-trees.c |    7 +++----
 1 files changed, 3 insertions(+), 4 deletions(-)
unpack-trees.c +3 -4
index 1ca41b1..42f7aad 100644
@@ -1374,10 +1374,9 @@ static int verify_absent_1(struct cache_entry *ce,
 		char path[PATH_MAX + 1];
 		memcpy(path, ce->name, len);
 		path[len] = 0;
-		lstat(path, &st);
-
-		return check_ok_to_remove(path, len, DT_UNKNOWN, NULL, &st,
-				error_type, o);
+		if (!lstat(path, &st))
+			return check_ok_to_remove(path, len, DT_UNKNOWN, NULL, &st,
+			                          error_type, o);
 	} else if (!lstat(ce->name, &st))
 		return check_ok_to_remove(ce->name, ce_namelen(ce),
 				ce_to_dtype(ce), ce, &st,
Signature · 1 line
1.7.4
#1 lore raw

unpack-trees.c static int verify_absent_1(struct cache_entry *ce, · line 1380

Quoted 4 lines · Torsten Bögershausen
unpack-trees.c +3 -0
+		if (!lstat(path, &st))
+			return check_ok_to_remove(path, len, DT_UNKNOWN, NULL, &st,
+			                          error_type, o);
 	} else if (!lstat(ce->name, &st))

What happens if lstat returns a non-0 value?

Anyway, this seems to have been fixed by a93e53018 (Wed Jan 12 20:28:09 2011, unpack-trees: handle lstat failure for existing file) already.

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

unpack-trees.c static int verify_absent_1(struct cache_entry *ce, · line 1380

Quoted 4 lines · Torsten Bögershausen
unpack-trees.c +3 -0
+		if (!lstat(path, &st))
+			return check_ok_to_remove(path, len, DT_UNKNOWN, NULL, &st,
+			                          error_type, o);
 	} else if (!lstat(ce->name, &st))

What happens if lstat returns a non-0 value?

The result of "st" is 100% garbage, and should not be passed to anybody. Not checking the return value (error or not) is not a good thing. Developers might be tempted to write unreliable code...

Quoted 4 lines

Anyway, this seems to have been fixed by a93e53018 (Wed Jan 12 20:28:09 2011, unpack-trees: handle lstat failure for existing file) already.

Hm,
v1.7.4 says:
commit e39212ab08e8d37dda5d8fd32b54099fe01dbbdb
Merge: 716958c 9e08273
(so far so good)
My git looks like this:

static int verify_absent_1(struct cache_entry *ce,
enum unpack_trees_error_types error_type, struct unpack_trees_options *o)
{
[snip]

	else if (len > 0) {
		char path[PATH_MAX + 1];
		memcpy(path, ce->name, len);
		path[len] = 0;
		lstat(path, &st);
                ^^^^^^^^^^^^^^^^^^
		return check_ok_to_remove(path, len, DT_UNKNOWN, NULL, &st,
				error_type, o);

(And I had to correct [email protected] -> [email protected]) /Torsten

#3 lore raw
Quoted 11 lines · Torsten Bögershausen
Quoted 8 lines · Matthieu Moy

unpack-trees.c static int verify_absent_1(struct cache_entry *ce, · line 1380

Quoted 4 lines · Torsten Bögershausen
unpack-trees.c +3 -0
+		if (!lstat(path, &st))
+			return check_ok_to_remove(path, len, DT_UNKNOWN, NULL, &st,
+			                          error_type, o);
 	} else if (!lstat(ce->name, &st))

What happens if lstat returns a non-0 value?

The result of "st" is 100% garbage, and should not be passed to anybody.

With your code, you don't do a return, you'll reach the end of the function without calling return, which is probably the worst thing you could expect.

Quoted 3 lines

v1.7.4 says:
commit e39212ab08e8d37dda5d8fd32b54099fe01dbbdb
Merge: 716958c 9e08273

You've cut the date:

commit e39212ab08e8d37dda5d8fd32b54099fe01dbbdb
Merge: 716958c 9e08273
Author: Junio C Hamano <[email protected]>
Date: Wed Dec 22 14:40:26 2010 -0800

=> no big surprise that you don't see the bugfix from Jan 12th.

Quoted 2 lines
Quoted 2 lines

Anyway, this seems to have been fixed by a93e53018 (Wed Jan 12 20:28:09 2011, unpack-trees: handle lstat failure for existing file) already.

Did you look at the content of this commit?

Quoted 1 line

(And I had to correct [email protected] -> [email protected])

Yes, because you've mis-spelled it in the first place and I didn't fix it ;-).

Signature · 2 lines
Matthieu Moy
http://www-verimag.imag.fr/~moy/
#4 lore raw
Quoted 34 lines · Matthieu Moy
Quoted 11 lines · Torsten Bögershausen
Quoted 8 lines · Matthieu Moy

unpack-trees.c static int verify_absent_1(struct cache_entry *ce, · line 1380

Quoted 4 lines · Torsten Bögershausen
unpack-trees.c +3 -0
+		if (!lstat(path, &st))
+			return check_ok_to_remove(path, len, DT_UNKNOWN, NULL, &st,
+			                          error_type, o);
 	} else if (!lstat(ce->name, &st))

What happens if lstat returns a non-0 value?

The result of "st" is 100% garbage, and should not be passed to anybody.

With your code, you don't do a return, you'll reach the end of the function without calling return, which is probably the worst thing you could expect.

Quoted 3 lines

v1.7.4 says:
commit e39212ab08e8d37dda5d8fd32b54099fe01dbbdb
Merge: 716958c 9e08273

You've cut the date:

commit e39212ab08e8d37dda5d8fd32b54099fe01dbbdb
Merge: 716958c 9e08273
Author: Junio C Hamano <[email protected]>
Date: Wed Dec 22 14:40:26 2010 -0800

=> no big surprise that you don't see the bugfix from Jan 12th.

Quoted 2 lines
Quoted 2 lines

Anyway, this seems to have been fixed by a93e53018 (Wed Jan 12 20:28:09 2011, unpack-trees: handle lstat failure for existing file) already.

Quoted 7 lines

Did you look at the content of this commit?

Quoted 1 line

(And I had to correct [email protected] -> [email protected])

Yes, because you've mis-spelled it in the first place and I didn't fix it ;-).

Ojojo,
While working with the best tracking tool, I lost tracking myself. That's why I missed Jonathans fix and learned today 9e08273 != a93e53018 Thanks for the patience, (and sorry for the noise)
/Torsten