[PATCH] plug mem leak, simplifications

Albrecht Dreß <[email protected]>
Newsgroups gmane.comp.gnome.apps.balsa
Message-ID <[email protected]>
Hi all,

attached is a small patch with the following changes:
- libbalsa/misc.c: simplify deleting a folder recursively and creating a temp folder by exclusively using glib functions
- libbalsa/rfc3156.c: fix mem leak when encrypting a message
- libbalsa/smtp-server.c: remove misleading/confusing comment

I'm still working n the bigger patch with several gpg-related extensions...

Comments?

Cheers,
Albrecht.

_______________________________________________
balsa-list mailing list
[email protected]
https://mail.gnome.org/mailman/listinfo/balsa-list
misc-patches.diff (text/x-patch, 4.2 KB)
diff --git a/libbalsa/misc.c b/libbalsa/misc.c
index c05da73..40b2ff8 100644
--- a/libbalsa/misc.c
+++ b/libbalsa/misc.c
@@ -29,7 +29,6 @@
 
 #define _SVID_SOURCE           1
 #include <ctype.h>
-#include <dirent.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -43,6 +42,7 @@
 #include "libbalsa_private.h"
 #include "html.h"
 #include <glib/gi18n.h>
+#include <glib/gstdio.h>
 
 static const gchar *libbalsa_get_codeset_name(const gchar *txt, 
 					      LibBalsaCodeset Codeset);
@@ -191,41 +191,39 @@ libbalsa_wrap_string(gchar * str, int width)
 gboolean
 libbalsa_delete_directory_contents(const gchar *path)
 {
-    struct stat sb;
-    DIR *d;
-    struct dirent *de;
-    gchar *new_path;
-
-    d = opendir(path);
-    g_return_val_if_fail(d, FALSE);
-
-    for (de = readdir(d); de; de = readdir(d)) {
-	if (strcmp(de->d_name, ".") == 0 ||
-	    strcmp(de->d_name, "..") == 0)
-	    continue;
-	new_path = g_strdup_printf("%s/%s", path, de->d_name);
-
-	stat(new_path, &sb);
-	if (S_ISDIR(sb.st_mode)) {
-	    if (!libbalsa_delete_directory_contents(new_path) ||
-		rmdir(new_path) == -1) {
-		g_free(new_path);
-		closedir(d);
-		return FALSE;
-	    }
-	} else {
-	    if (unlink( new_path ) == -1) {
-		g_free(new_path);
-		closedir(d);
-		return FALSE;
-	    }
-	}
-	g_free(new_path);
-	new_path = 0;
+	GDir *dir;
+	gboolean result;
+
+    g_return_val_if_fail(path != NULL, FALSE);
+    dir = g_dir_open(path, 0, NULL);
+    if (dir == NULL) {
+    	result = FALSE;
+    } else {
+    	const gchar *item;
+
+    	result = TRUE;
+    	item = g_dir_read_name(dir);
+    	while (result && (item != NULL)) {
+    		gchar *full_path;
+
+    		full_path = g_build_filename(path, item, NULL);
+    		if (g_file_test(full_path, G_FILE_TEST_IS_DIR)) {
+    	   		result = libbalsa_delete_directory_contents(full_path);
+    			if (g_rmdir(full_path) != 0) {
+    				result = FALSE;
+    			}
+    		} else {
+    	   		if (g_unlink(full_path) != 0) {
+    				result = FALSE;
+    			}
+    		}
+    		g_free(full_path);
+    		item = g_dir_read_name(dir);
+    	}
+    	g_dir_close(dir);
     }
 
-    closedir(d);
-    return TRUE;
+    return result;
 }
 
 /* libbalsa_expand_path:
@@ -252,29 +250,9 @@ libbalsa_expand_path(const gchar * path)
 gboolean 
 libbalsa_mktempdir (char **s)
 {
-    gchar *name;
-    int fd;
-
     g_return_val_if_fail(s != NULL, FALSE);
-
-    do {
-	GError *error = NULL;
-	fd = g_file_open_tmp("balsa-tmpdir-XXXXXX", &name, &error);
-	close(fd);
-	unlink(name);
-	/* Here is a short time that the name could be reused */
-	fd = mkdir(name, 0700);
-	if (fd == -1) {
-	    g_free(name);
-	    if (!g_error_matches(error, G_FILE_ERROR, G_FILE_ERROR_EXIST))
-		return FALSE;
-	}
-	if (error)
-	    g_error_free(error);
-    } while (fd == -1);
-    *s = name;
-    /* FIXME: rmdir(name) at sometime */
-    return TRUE;
+    *s = g_build_filename(g_get_tmp_dir(), "balsa-tmpdir-XXXXXX", NULL);
+    return g_mkdtemp_full(*s, 0700) != NULL;
 }
 
 /* libbalsa_set_fallback_codeset: sets the codeset for incorrectly
diff --git a/libbalsa/rfc3156.c b/libbalsa/rfc3156.c
index 87084ea..f3b5fe8 100644
--- a/libbalsa/rfc3156.c
+++ b/libbalsa/rfc3156.c
@@ -262,7 +262,7 @@ libbalsa_encrypt_mime_object(GMimeObject ** content, GList * rfc822_for,
 
     	result = g_mime_application_pkcs7_encrypt(pkcs7, *content, recipients, always_trust, parent, error);
     }
-    g_ptr_array_free(recipients, FALSE);
+    g_ptr_array_unref(recipients);
 
     /* error checking */
     if (!result) {
@@ -497,7 +497,7 @@ libbalsa_rfc2440_sign_encrypt(GMimePart *part, const gchar *sign_for,
 					      always_trust, parent, error);
     /* clean up */
     if (recipients)
-	g_ptr_array_free(recipients, FALSE);
+	g_ptr_array_unref(recipients);
     return result;
 }
 
diff --git a/libbalsa/smtp-server.c b/libbalsa/smtp-server.c
index d1a24e2..3157ca2 100644
--- a/libbalsa/smtp-server.c
+++ b/libbalsa/smtp-server.c
@@ -47,7 +47,6 @@ struct _LibBalsaSmtpServer {
     gchar *name;
     guint big_message; /* size of partial messages; in kB; 0 disables splitting */
     gint lock_state;	/* 0 means unlocked; access via atomic operations */
-    // FIXME - add an atomic flag if an operation is running on this server
 };
 
 typedef struct _LibBalsaSmtpServerClass {
signature.asc (application/pgp-signature, 473 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQEcBAABCAAGBQJZg1dSAAoJEEypbw5n59n4rCgIAM6bl7t9RNePWQyUUR/SbZGB
P/NTQoUJH7mAdR0yJBkY7fxyvkwfczFKx3qzfLkk0L39KaxxYE2e85i+A/grUmGF
HCXgD0dGoiMHnWDgG2ibmvVcP8Yq/0RjTK4lZZMSBaK3J24LYR/+2qj36+aug3bp
DcNusxsEpXOdzwmZBAfjNMa9AIzB5L90JQACk+8QoC98BZDKIowlI0mIsh/2XtL8
iIkCbUbufkVpPerLJgPKTds2f+Gq+OhepZgbQIkZ2RUYp1C5AQIyDyg0d6IdpslQ
t6Q52gG0T4hBniOGwdZ6QqLacYGchA8VtsOAdpJ6jn1QSmyeXbawC0X6j/1KBDM=
=gPWF
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.