tags 147832 + patch
bye

Hi,

I offer the attached patch to fix this bug.  It uses a temporary file
and overwrite the servlist_.conf file only if fclose returns without
errors.

I've intentionally ignored the file permissions issue mentioned above.

HTH
-- 
Walter Franzini
http://aegis.stepbuild.org/
Index: xchat-2.8.8/src/common/servlist.c
===================================================================
--- xchat-2.8.8.orig/src/common/servlist.c	2012-08-20 15:34:54.000000000 +0200
+++ xchat-2.8.8/src/common/servlist.c	2012-08-20 15:59:53.607555834 +0200
@@ -1077,17 +1077,24 @@
 	ircserver *serv;
 	GSList *list;
 	GSList *hlist;
+	char *servlist = "servlist_.conf";
+	char *new_servlist;
+
 #ifndef WIN32
 	int first = FALSE;
 
-	snprintf (buf, sizeof (buf), "%s/servlist_.conf", get_xdir_fs ());
+	snprintf (buf, sizeof (buf), "%s/servlist_.conf.new", get_xdir_fs ());
 	if (access (buf, F_OK) != 0)
 		first = TRUE;
 #endif
 
-	fp = xchat_fopen_file ("servlist_.conf", "w", 0);
+	new_servlist = malloc (strlen(servlist) + 5);
+	strcpy (new_servlist, servlist);
+	strcat (new_servlist, ".new");
+
+	fp = xchat_fopen_file (new_servlist, "w", 0);
 	if (!fp)
-		return FALSE;
+		goto failure;
 
 #ifndef WIN32
 	if (first)
@@ -1140,17 +1147,25 @@
 			hlist = hlist->next;
 		}
 
-		if (fprintf (fp, "\n") < 1)
-		{
-			fclose (fp);
-			return FALSE;
-		}
+		fprintf (fp, "\n");
 
 		list = list->next;
 	}
 
-	fclose (fp);
-	return TRUE;
+	if (0 == fclose (fp))
+	{
+#ifdef WIN32
+		unlink (servlist); /* win32 can't rename to an existing file */
+#endif
+		xchat_rename_file (new_servlist, servlist, 0);
+
+		free (new_servlist);
+		return TRUE;
+	}
+
+failure:
+	free(new_servlist);
+	return FALSE;
 }
 
 static void

Reply via email to