summary refs log tree commit diff stats
path: root/src/common
diff options
context:
space:
mode:
authorTingPing <tingping@tingping.se>2014-07-21 07:35:32 -0400
committerTingPing <tingping@tingping.se>2014-07-28 14:47:19 -0400
commitc2ecb4c68c79c3be40849100f28da8961d7fcd54 (patch)
treec69474e2ef1ef4b772d92aa0da6ab446eb40c3a2 /src/common
parentea9dafcd43df4f31dd36dceaa995fdd97d40c6d4 (diff)
Fix various crashes with pluginpref
Diffstat (limited to 'src/common')
-rw-r--r--src/common/plugin.c53
1 files changed, 18 insertions, 35 deletions
diff --git a/src/common/plugin.c b/src/common/plugin.c
index e356b26a..7cc384a7 100644
--- a/src/common/plugin.c
+++ b/src/common/plugin.c
@@ -1885,59 +1885,42 @@ hexchat_pluginpref_set_str (hexchat_plugin *pl, const char *var, const char *val
 	return hexchat_pluginpref_set_str_real (pl, var, value, 1);
 }
 
-int
-hexchat_pluginpref_get_str (hexchat_plugin *pl, const char *var, char *dest)
+static int
+hexchat_pluginpref_get_str_real (hexchat_plugin *pl, const char *var, char *dest, int dest_len)
 {
-	int fh;
-	int l;
-	char confname[64];
-	char *canon;
-	char *cfg;
-	struct stat st;
+	char *confname, *canon, *cfg;
 
 	canon = g_strdup (pl->name);
 	canonalize_key (canon);
-	sprintf (confname, "addon_%s.conf", canon);
+	confname = g_strdup_printf ("%s%caddon_%s.conf", get_xdir(), G_DIR_SEPARATOR, canon);
 	g_free (canon);
 
-	/* partly borrowed from palette.c */
-	fh = hexchat_open_file (confname, O_RDONLY, 0, 0);
-
-	if (fh == -1)
-	{
-		return 0;
-	}
-
-	fstat (fh, &st);
-	cfg = malloc (st.st_size + 1);
-
-	if (!cfg)
+	if (!g_file_get_contents (confname, &cfg, NULL, NULL))
 	{
-		close (fh);
+		g_free (confname);
 		return 0;
 	}
 
-	cfg[0] = '\0';
-	l = read (fh, cfg, st.st_size);
-
-	if (l >= 0)
-	{
-		cfg[l] = '\0';
-	}
+	g_free (confname);
 
-	if (!cfg_get_str (cfg, var, dest, 512)) /* dest_len is the same as buffer size in set */
+	if (!cfg_get_str (cfg, var, dest, dest_len))
 	{
-		free (cfg);
-		close (fh);
+		g_free (cfg);
 		return 0;
 	}
 
-	free (cfg);
-	close (fh);
+	g_free (cfg);
 	return 1;
 }
 
 int
+hexchat_pluginpref_get_str (hexchat_plugin *pl, const char *var, char *dest)
+{
+	/* All users of this must ensure dest is >= 512... */
+	return hexchat_pluginpref_get_str_real (pl, var, dest, 512);
+}
+
+int
 hexchat_pluginpref_set_int (hexchat_plugin *pl, const char *var, int value)
 {
 	char buffer[12];
@@ -1951,7 +1934,7 @@ hexchat_pluginpref_get_int (hexchat_plugin *pl, const char *var)
 {
 	char buffer[12];
 
-	if (hexchat_pluginpref_get_str (pl, var, buffer))
+	if (hexchat_pluginpref_get_str_real (pl, var, buffer, sizeof(buffer)))
 	{
 		return atoi (buffer);
 	}