SF.net SVN: geany:[4012] trunk

ntrel at users.sourceforge.net ntrel at xxxxx
Thu Jul 23 11:00:33 UTC 2009


Revision: 4012
          http://geany.svn.sourceforge.net/geany/?rev=4012&view=rev
Author:   ntrel
Date:     2009-07-23 11:00:32 +0000 (Thu, 23 Jul 2009)

Log Message:
-----------
Change utils_str_remove_chars() to work in place; fix allocating on
the stack (the string length could exhaust the stack size).

Modified Paths:
--------------
    trunk/ChangeLog
    trunk/plugins/splitwindow.c
    trunk/src/plugindata.h
    trunk/src/toolbar.c
    trunk/src/utils.c
    trunk/src/utils.h

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2009-07-22 03:04:17 UTC (rev 4011)
+++ trunk/ChangeLog	2009-07-23 11:00:32 UTC (rev 4012)
@@ -1,3 +1,11 @@
+2009-07-23  Nick Treleaven  <nick(dot)treleaven(at)btinternet(dot)com>
+
+ * src/utils.c, src/utils.h, src/toolbar.c, src/plugindata.h,
+   plugins/splitwindow.c:
+   Change utils_str_remove_chars() to work in place; fix allocating on
+   the stack (the string length could exhaust the stack size).
+
+
 2009-07-21  Enrico Tröger  <enrico(dot)troeger(at)uvena(dot)de>
 
  * src/utils.c:

Modified: trunk/plugins/splitwindow.c
===================================================================
--- trunk/plugins/splitwindow.c	2009-07-22 03:04:17 UTC (rev 4011)
+++ trunk/plugins/splitwindow.c	2009-07-23 11:00:32 UTC (rev 4012)
@@ -223,8 +223,9 @@
 }
 
 
-/* Create a GtkToolButton with stock icon and tooltip.
- * @param label can be NULL to use stock label text, without underscores.
+/* Create a GtkToolButton with stock icon, label and tooltip.
+ * @param label can be NULL to use stock label text. @a label can contain underscores,
+ * which will be removed.
  * @param tooltip can be NULL to use label text (useful for GTK_TOOLBAR_ICONS). */
 static GtkWidget *ui_tool_button_new(const gchar *stock_id, const gchar *label, const gchar *tooltip)
 {
@@ -234,9 +235,10 @@
 	if (stock_id && !label)
 	{
 		label = ui_get_stock_label(stock_id);
-		dup = utils_str_remove_chars(label, "_");
-		label = dup;
 	}
+	dup = utils_str_remove_chars(g_strdup(label), "_");
+	label = dup;
+
 	item = gtk_tool_button_new(NULL, label);
 	if (stock_id)
 		gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(item), stock_id);
@@ -266,7 +268,6 @@
 {
 	GtkWidget *toolbar, *item;
 	GtkToolItem *tool_item;
-	gchar *label;
 
 	toolbar = gtk_toolbar_new();
 	gtk_toolbar_set_icon_size(GTK_TOOLBAR(toolbar), GTK_ICON_SIZE_MENU);
@@ -286,11 +287,9 @@
 	gtk_container_add(GTK_CONTAINER(tool_item), item);
 	edit_window.name_label = item;
 
-	label = utils_str_remove_chars(_("_Unsplit"), "_");
-	item = ui_tool_button_new(GTK_STOCK_CLOSE, label, NULL);
+	item = ui_tool_button_new(GTK_STOCK_CLOSE, _("_Unsplit"), NULL);
 	gtk_container_add(GTK_CONTAINER(toolbar), item);
 	g_signal_connect(item, "clicked", G_CALLBACK(on_unsplit), NULL);
-	g_free(label);
 
 	return toolbar;
 }

Modified: trunk/src/plugindata.h
===================================================================
--- trunk/src/plugindata.h	2009-07-22 03:04:17 UTC (rev 4011)
+++ trunk/src/plugindata.h	2009-07-23 11:00:32 UTC (rev 4012)
@@ -50,13 +50,13 @@
 enum {
 	/** The Application Programming Interface (API) version, incremented
 	 * whenever any plugin data types are modified or appended to. */
-	GEANY_API_VERSION = 148,
+	GEANY_API_VERSION = 149,
 
 	/** The Application Binary Interface (ABI) version, incremented whenever
 	 * existing fields in the plugin data types have to be changed or reordered. */
 	/* This should usually stay the same if fields are only appended, assuming only pointers to
 	 * structs and not structs themselves are declared by plugins. */
-	GEANY_ABI_VERSION = 62
+	GEANY_ABI_VERSION = 63
 };
 
 /** Check the plugin can be loaded by Geany.
@@ -369,7 +369,7 @@
 	guint		(*string_replace_first) (GString *haystack, const gchar *needle,
 				 const gchar *replace);
 	gchar*		(*str_middle_truncate) (const gchar *string, guint truncate_length);
-	gchar*		(*str_remove_chars) (const gchar *string, const gchar *chars);
+	gchar*		(*str_remove_chars) (gchar *string, const gchar *chars);
 }
 UtilsFuncs;
 

Modified: trunk/src/toolbar.c
===================================================================
--- trunk/src/toolbar.c	2009-07-22 03:04:17 UTC (rev 4011)
+++ trunk/src/toolbar.c	2009-07-23 11:00:32 UTC (rev 4012)
@@ -625,7 +625,7 @@
 
 		g_object_get(action, "label", &label, NULL);
 		if (label != NULL)
-			label_clean = utils_str_remove_chars(label, "_");
+			label_clean = utils_str_remove_chars(g_strdup(label), "_");
 	}
 
 	gtk_list_store_set(store, iter,

Modified: trunk/src/utils.c
===================================================================
--- trunk/src/utils.c	2009-07-22 03:04:17 UTC (rev 4011)
+++ trunk/src/utils.c	2009-07-23 11:00:32 UTC (rev 4012)
@@ -1756,49 +1756,37 @@
 }
 
 
-/* Like strcpy, but can handle overlapping src and dest. */
-static gchar *utils_str_copy(gchar *dest, const gchar *src)
-{
-	gchar *cpy;
+/* @warning Doesn't include null terminating character. */
+#define foreach_str(char_ptr, string) \
+	for (char_ptr = string; *char_ptr; char_ptr++)
 
-	/* strcpy might not handle overlaps, so make a copy */
-	cpy = utils_strdupa(src);
-	return strcpy(dest, cpy);
-}
-
-
 /**
- *  Remove characters from a string.
+ *  Replace or remove characters from a string in place.
  *
- *  @param string The original string
+ *  @param string String to search.
  *  @param chars Characters to remove.
  *
- *  @return A newly-allocated copy of @a str without the characters in @a chars,
- *          should be freed when no longer needed.
+ *  @return @a string - return value is only useful when nesting function calls, e.g.:
+ *  @code str = utils_str_remove_chars(g_strdup("f_o_o"), "_"); @endcode
+ *
+ *  @see @c g_strdelimit.
  **/
-gchar *utils_str_remove_chars(const gchar *string, const gchar *chars)
+gchar *utils_str_remove_chars(gchar *string, const gchar *chars)
 {
-	gchar *ptr;
-	gchar *result;
-	gsize len;
+	const gchar *r;
+	gchar *w = string;
 
 	g_return_val_if_fail(string, NULL);
+	if (!NZV(chars))
+		return string;
 
-	result = g_strdup(string);
-	len = strlen(result);
-	ptr = result;
-
-	while (ptr < result + len)
+	foreach_str(r, string)
 	{
-		if (strchr(chars, *ptr))
-		{
-			utils_str_copy(ptr, ptr + 1);
-			len--;
-		}
-		else
-			ptr++;
+		if (!strchr(chars, *r))
+			*w++ = *r;
 	}
-	return result;
+	*w = 0x0;
+	return string;
 }
 
 

Modified: trunk/src/utils.h
===================================================================
--- trunk/src/utils.h	2009-07-22 03:04:17 UTC (rev 4011)
+++ trunk/src/utils.h	2009-07-23 11:00:32 UTC (rev 4012)
@@ -198,6 +198,6 @@
 
 gchar *utils_str_middle_truncate(const gchar *string, guint truncate_length);
 
-gchar *utils_str_remove_chars(const gchar *string, const gchar *chars);
+gchar *utils_str_remove_chars(gchar *string, const gchar *chars);
 
 #endif


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