Skip to contentSkip to conversation

Fix resource leaks in wrapper.c

PATCH In review Laszlo Papp 3 participants 4 messages Jun 15, 2016 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix resource leaks in wrapper.c

Fix the following issues with the desired close tags:

[wrapper.c:276]: (error) Resource leak: fd
[wrapper.c:291]: (error) Resource leak: fd

---
 wrapper.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
wrapper.c +2 -2
index c9be140..76ecf0a 100644
@@ -266,7 +266,7 @@ int odb_mkstemp(char *template, size_t limit, const char *pattern)
 	fd = mkstemp(template);
 	if (0 <= fd)
 		return fd;
-
+	close(fd);
 	/* slow path */
 	/* some mkstemp implementations erase template on failure */
 	snprintf(template, limit, "%s/%s",
@@ -284,7 +284,7 @@ int odb_pack_keep(char *name, size_t namesz, unsigned char *sha1)
 	fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
 	if (0 <= fd)
 		return fd;
-
+	close(fd);
 	/* slow path */
 	safe_create_leading_directories(name);
 	return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
Signature · 1 line
1.6.5
#1 lore raw

wrapper.c int odb_mkstemp(char *template, size_t limit, const char *pattern) · line 269

Quoted 6 lines · Laszlo Papp
wrapper.c +1 -1
@@ -266,7 +266,7 @@ int odb_mkstemp(char *template, size_t limit, const char *pattern)
 	fd = mkstemp(template);
 	if (0 <= fd)
 		return fd;
-
+	close(fd);

Sorry, where is here a resource leak? You are "closing" something that was never opened because fd is less than zero.

Ditto for the other case.

-- Hannes

#2 lore raw

Johannes Sixt venit, vidit, dixit 27.10.2009 08:13:

Quoted 11 lines

wrapper.c int odb_mkstemp(char *template, size_t limit, const char *pattern) · line 269

Quoted 6 lines · Laszlo Papp
wrapper.c +1 -1
@@ -266,7 +266,7 @@ int odb_mkstemp(char *template, size_t limit, const char *pattern)
 	fd = mkstemp(template);
 	if (0 <= fd)
 		return fd;
-
+	close(fd);

Sorry, where is here a resource leak? You are "closing" something that was never opened because fd is less than zero.

Quoted 1 line

Ditto for the other case.

I guess it's about silencing some challenged code analysis tool. I recall that last time we had something like this we decided that coders are smarter than tools... and also that clean up like this (for real leaks) would be something for libgit.

Michael

#4 lore raw
In reply to a message not in the archive

Laszlo Papp venit, vidit, dixit 27.10.2009 11:35:

Quoted 26 lines

On Tue, Oct 27, 2009 at 9:26 AM, Michael J Gruber <[email protected] <mailto:[email protected]>> wrote:

Johannes Sixt venit, vidit, dixit 27.10.2009 08:13:
> Laszlo Papp schrieb:
>> @@ -266,7 +266,7 @@ int odb_mkstemp(char *template, size_t limit, const char *pattern)
>> fd = mkstemp(template);
>> if (0 <= fd)
>> return fd;
>> -
>> + close(fd);
>
> Sorry, where is here a resource leak? You are "closing" something that was
> never opened because fd is less than zero.
>
> Ditto for the other case.

I guess it's about silencing some challenged code analysis tool. I recall that last time we had something like this we decided that coders are smarter than tools... and also that clean up like this (for real leaks) would be something for libgit.

Quoted 5 lines

Michael

Yeah you're rights guys, sorry for my fault, this cppcheck program is not the best at this momment, really sorry.

No need to feel overly sorry, but in general it helps if, in a commit message or thereabout, you say something like "cppcheck found the following (potential) errors".

Cheers,
Michael