SF.net SVN: geany: [2747] trunk

ntrel at users.sourceforge.net ntrel at xxxxx
Thu Jul 3 14:39:50 UTC 2008


Revision: 2747
          http://geany.svn.sourceforge.net/geany/?rev=2747&view=rev
Author:   ntrel
Date:     2008-07-03 07:39:49 -0700 (Thu, 03 Jul 2008)

Log Message:
-----------
Fix memory leak in utils_find_open_xml_tag() when size < 3, and only
allocate string when '<' found.
Factor insert_closing_tag() from handle_xml(), fixing a memory leak
when ignoring tags like <br>.

Modified Paths:
--------------
    trunk/ChangeLog
    trunk/src/editor.c
    trunk/src/utils.c

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2008-07-03 14:01:47 UTC (rev 2746)
+++ trunk/ChangeLog	2008-07-03 14:39:49 UTC (rev 2747)
@@ -1,3 +1,12 @@
+2008-07-03  Nick Treleaven  <nick(dot)treleaven(at)btinternet(dot)com>
+
+ * src/utils.c, src/editor.c:
+   Fix memory leak in utils_find_open_xml_tag() when size < 3, and only
+   allocate string when '<' found.
+   Factor insert_closing_tag() from handle_xml(), fixing a memory leak
+   when ignoring tags like <br>.
+
+
 2008-07-03  Enrico Tröger  <enrico(dot)troeger(at)uvena(dot)de>
 
  * src/utils.c:

Modified: trunk/src/editor.c
===================================================================
--- trunk/src/editor.c	2008-07-03 14:01:47 UTC (rev 2746)
+++ trunk/src/editor.c	2008-07-03 14:39:49 UTC (rev 2747)
@@ -1615,6 +1615,36 @@
 }
 
 
+static void insert_closing_tag(GeanyDocument *doc, gint pos, gchar ch, const gchar *tag_name)
+{
+	ScintillaObject *sci = doc->sci;
+	gchar *to_insert = NULL;
+
+	if (ch == '/')
+	{
+		const gchar *gt = ">";
+		/* if there is already a '>' behind the cursor, don't add it */
+		if (sci_get_char_at(sci, pos) == '>')
+			gt = "";
+
+		to_insert = g_strconcat(tag_name, gt, NULL);
+	}
+	else
+		to_insert = g_strconcat("</", tag_name, ">", NULL);
+
+	sci_start_undo_action(sci);
+	sci_replace_sel(sci, to_insert);
+	if (ch == '>')
+	{
+		SSM(sci, SCI_SETSEL, pos, pos);
+		if (utils_str_equal(tag_name, "table"))
+			editor_auto_table(doc, pos);
+	}
+	sci_end_undo_action(sci);
+	g_free(to_insert);
+}
+
+
 /*
  * (stolen from anjuta and heavily modified)
  * This routine will auto complete XML or HTML tags that are still open by closing them
@@ -1627,6 +1657,7 @@
 	gint lexer = SSM(sci, SCI_GETLEXER, 0, 0);
 	gint pos, min;
 	gchar *str_found, sel[512];
+	gboolean result = FALSE;
 
 	/* If the user has turned us off, quit now.
 	 * This may make sense only in certain languages */
@@ -1675,39 +1706,15 @@
 	 || utils_str_equal(str_found, "area")
 	 || utils_str_equal(str_found, "meta"))
 	{
-		return FALSE;
+		/* ignore tag */
 	}
-
-	if (*str_found != '\0')
+	else if (*str_found != '\0')
 	{
-		gchar *to_insert;
-		if (ch == '/')
-		{
-			gchar *gt = ">";
-			/* if there is already a '>' behind the cursor, don't add it */
-			if (sci_get_char_at(sci, pos) == '>')
-				gt = "";
-
-			to_insert = g_strconcat(str_found, gt, NULL);
-		}
-		else
-			to_insert = g_strconcat("</", str_found, ">", NULL);
-		sci_start_undo_action(sci);
-		sci_replace_sel(sci, to_insert);
-		if (ch == '>')
-		{
-			SSM(sci, SCI_SETSEL, pos, pos);
-			if (utils_str_equal(str_found, "table"))
-				editor_auto_table(doc, pos);
-		}
-		sci_end_undo_action(sci);
-		g_free(to_insert);
-		g_free(str_found);
-		return TRUE;
+		insert_closing_tag(doc, pos, ch, str_found);
+		result = TRUE;
 	}
-
 	g_free(str_found);
-	return FALSE;
+	return result;
 }
 
 

Modified: trunk/src/utils.c
===================================================================
--- trunk/src/utils.c	2008-07-03 14:01:47 UTC (rev 2746)
+++ trunk/src/utils.c	2008-07-03 14:39:49 UTC (rev 2747)
@@ -244,12 +244,11 @@
 
 /*
  * (stolen from anjuta and modified)
- * Search backward through size bytes looking for a '<', then return the tag if any
+ * Search backward through size bytes looking for a '<', then return the tag, if any.
  * @return The tag name
  */
 gchar *utils_find_open_xml_tag(const gchar sel[], gint size, gboolean check_tag)
 {
-	GString *result = g_string_sized_new(64);
 	const gchar *begin, *cur;
 
 	if (size < 3)
@@ -272,16 +271,18 @@
 
 	if (*cur == '<')
 	{
+		GString *result = g_string_sized_new(64);
+
 		cur++;
-		while((strchr(":_-.", *cur) || isalnum(*cur)))
+		while (strchr(":_-.", *cur) || isalnum(*cur))
 		{
 			g_string_append_c(result, *cur);
 			cur++;
 		}
+		return g_string_free(result, FALSE);
 	}
 
-	/* Return the tag name or "" */
-	return g_string_free(result, FALSE);
+	return NULL;
 }
 
 


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the Commits mailing list