commit cb87edf1710e8021ed0affc2157cf0c340660c72
parent 601733aa309a83af7871a7d209386a978c086106
Author: Ricardo Mones <ricardo@mones.org>
Date: Tue, 23 Jun 2015 00:38:31 +0200
Libravatar: refactor and fix leak on corner case
• Move cache initialization to its own file
• Fix leaked cache_dir path when initialization failed
Diffstat:
5 files changed, 87 insertions(+), 24 deletions(-)
diff --git a/src/plugins/libravatar/Makefile.am b/src/plugins/libravatar/Makefile.am
@@ -78,6 +78,7 @@ libravatar_la_CPPFLAGS = \
libravatar_la_SOURCES = \
libravatar.c libravatar.h \
libravatar_prefs.c libravatar_prefs.h \
+ libravatar_cache.c libravatar_cache.h \
libravatar_missing.c libravatar_missing.h \
libravatar_federation.c libravatar_federation.h
diff --git a/src/plugins/libravatar/libravatar.c b/src/plugins/libravatar/libravatar.c
@@ -30,6 +30,7 @@
#include "version.h"
#include "libravatar.h"
#include "libravatar_prefs.h"
+#include "libravatar_cache.h"
#include "libravatar_missing.h"
#include "libravatar_federation.h"
#include "prefs_common.h"
@@ -312,29 +313,8 @@ static gboolean libravatar_image_render_hook(gpointer source, gpointer data)
static gint cache_dir_init()
{
- gchar *subdir;
- int i;
-
- cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
- LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
- NULL);
- if (!is_dir_exist(cache_dir)) {
- if (make_dir(cache_dir) < 0) {
- g_free(cache_dir);
- return -1;
- }
- }
- for (i = DEF_MODE_MM; i <= DEF_MODE_RETRO; ++i) {
- subdir = g_strconcat(cache_dir, def_mode[i - 10], NULL);
- if (!is_dir_exist(subdir)) {
- if (make_dir(subdir) < 0) {
- g_warning("cannot create directory %s\n", subdir);
- g_free(subdir);
- return -1;
- }
- }
- g_free(subdir);
- }
+ cache_dir = libravatar_cache_init(def_mode, DEF_MODE_MM - 10, DEF_MODE_RETRO - 10);
+ cm_return_val_if_fail (cache_dir != NULL, -1);
return 0;
}
diff --git a/src/plugins/libravatar/libravatar.h b/src/plugins/libravatar/libravatar.h
@@ -31,7 +31,6 @@
#define AVATAR_LIBRAVATAR 3
#define AVATAR_SIZE 48
-#define LIBRAVATAR_CACHE_DIR "avatarcache"
/* https://github.com/mathiasbynens/small/pull/19 */
#define MIN_PNG_SIZE 67L
#define MAX_URL_LENGTH 1024
diff --git a/src/plugins/libravatar/libravatar_cache.c b/src/plugins/libravatar/libravatar_cache.c
@@ -0,0 +1,52 @@
+/*
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2015 Hiroyuki Yamamoto and the Claws Mail Team
+ * Copyright (C) 2014-2015 Ricardo Mones
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "libravatar_cache.h"
+#include "utils.h"
+
+gchar *libravatar_cache_init(const char *dirs[], gint start, gint end)
+{
+ gchar *subdir, *rootdir;
+ int i;
+
+ rootdir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
+ LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
+ NULL);
+ if (!is_dir_exist(rootdir)) {
+ if (make_dir(rootdir) < 0) {
+ g_warning("cannot create root directory %s\n", subdir);
+ g_free(rootdir);
+ return NULL;
+ }
+ }
+ for (i = start; i <= end; ++i) {
+ subdir = g_strconcat(rootdir, dirs[i], NULL);
+ if (!is_dir_exist(subdir)) {
+ if (make_dir(subdir) < 0) {
+ g_warning("cannot create directory %s\n", subdir);
+ g_free(subdir);
+ g_free(rootdir);
+ return NULL;
+ }
+ }
+ g_free(subdir);
+ }
+
+ return rootdir;
+}
diff --git a/src/plugins/libravatar/libravatar_cache.h b/src/plugins/libravatar/libravatar_cache.h
@@ -0,0 +1,31 @@
+/*
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2015 Hiroyuki Yamamoto and the Claws Mail Team
+ * Copyright (C) 2014-2015 Ricardo Mones
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __LIBRAVATAR_CACHE_H
+#define __LIBRAVATAR_CACHE_H
+
+#include <glib.h>
+
+#define LIBRAVATAR_CACHE_DIR "avatarcache"
+
+gchar *libravatar_cache_init (const char *dirs[],
+ gint start,
+ gint end);
+
+#endif