Skip to contentSkip to conversation

Fix file descriptor leak on error

PATCH In review Thomas Jarosch 2 participants 3 messages Dec 25, 2012 – Dec 27, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix file descriptor leak on error

Detected by cppcheck

---
 lib/utils.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
lib/utils.c +12 -4
index 4007bee..50ca6d9 100644
@@ -60,24 +60,32 @@ int __nl_read_num_str_file(const char *path, int (*cb)(long, const char *))
 			continue;
 
 		num = strtol(buf, &end, 0);
-		if (end == buf)
+		if (end == buf) {
+			fclose(fd);
 			return -NLE_INVAL;
+		}
 
-		if (num == LONG_MIN || num == LONG_MAX)
+		if (num == LONG_MIN || num == LONG_MAX) {
+			fclose(fd);
 			return -NLE_RANGE;
+		}
 
 		while (*end == ' ' || *end == '\t')
 			end++;
 
 		goodlen = strcspn(end, "#\r\n\t ");
-		if (goodlen == 0)
+		if (goodlen == 0) {
+			fclose(fd);
 			return -NLE_INVAL;
+		}
 
 		end[goodlen] = '\0';
 
 		err = cb(num, end);
-		if (err < 0)
+		if (err < 0) {
+			fclose(fd);
 			return err;
+		}
 	}
 
 	fclose(fd);
Signature · 1 line
1.7.11.7
#1 lore raw
Quoted 1 line · Thomas Jarosch

Detected by cppcheck

Same question here.

#2 lore raw
Quoted 4 lines · On Thursday, 27. December 2012 07:23:35 Patrick McHardy wrote:
Quoted 1 line · Thomas Jarosch

Detected by cppcheck

Same question here.

I've submitted the patch to upstream. Thanks (also to Pablo).

Thomas