Skip to contentSkip to conversation

libattr - severe memory leaks from attr_copy_file()

PATCH Zdenek Prikryl 3 participants 3 messages Feb 17, 2009 – Feb 18, 2009

lore
Share & tools
lore
#0 lore raw

Hello,
libattr's function attr_copy_file() seems to be producing severe memory leaks. I'm sending a patch which removes sources of leaks.

Regards.

Signature · 1 line
Zdenek Prikryl <[email protected]>
libattr/attr_copy_action.c renamed from libattr/attr_copy_action.c.leak +10 -7
diff -up attr-2.4.43/libattr/attr_copy_action.c.leak attr-2.4.43/libattr/attr_copy_action.c
@@ -53,7 +53,7 @@ free_attr_actions(void)
 static int
 attr_parse_attr_conf(struct error_context *ctx)
 {
-	char *text, *t;
+	char *text = NULL, *t;
 	size_t size_guess = 4096, len;
 	FILE *file;
 	char *pattern = NULL;
@@ -64,15 +64,16 @@ attr_parse_attr_conf(struct error_contex
 		return 0;
 
 repeat:
-	text = malloc(size_guess + 1);
-	if (!text)
-		goto fail;
-
 	if ((file = fopen(ATTR_CONF, "r")) == NULL) {
 		if (errno == ENOENT)
 			return 0;
 		goto fail;
 	}
+
+	text = malloc(size_guess + 1);
+	if (!text)
+		goto fail;
+
 	len = fread(text, 1, size_guess, file);
 	if (ferror(file))
 		goto fail;
@@ -140,10 +141,12 @@ fail:
 		quote_free (ctx, q);
 	}
 
-	free(pattern);
+	if (pattern)
+		free(pattern);
 	if (file)
 		fclose(file);
-	free(text);
+	if (text)
+		free(text);
 	free_attr_actions();
 	return -1;
 }

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#1 lore raw
Quoted 3 lines · Zdenek Prikryl

-       free(text);
+       if (text)
+               free(text);

free(NULL) is fine (on Linux at least)

Signature · 6 lines
Arkadiusz Miśkiewicz        PLD/Linux Team
arekm / maven.pl            http://ftp.pld-linux.org/
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#2 lore raw
Quoted 7 lines · Arkadiusz Miskiewicz

libattr/attr_copy_action.c fail: · line 149

Quoted 3 lines · Zdenek Prikryl
libattr/attr_copy_action.c renamed from libattr/attr_copy_action.c.leak +2 -1
-       free(text);
+       if (text)
+               free(text);

free(NULL) is fine (on Linux at least)

Exactly - in most implementations for free I would say NULL is just fine.

And the variable, text, is assigned straight away before any use, so I missed where the problem is.

--Tim

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs