changeset 4606:6b76f8589f5d

Added a temporary function str_assert_utf8() for finding points in code where string data should be UTF-8 but might not be. In case of valid UTF-8 a copy of string is returned. If string is NOT valid UTF-8, a call backtrace is printed and str_to_utf() is called instead.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 04 Jun 2008 23:17:47 +0300
parents 9df19c4900ba
children 829c30fc87ba
files src/audacious/strings.c src/audacious/strings.h
diffstat 2 files changed, 42 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/audacious/strings.c	Wed Jun 04 22:40:03 2008 +0300
+++ b/src/audacious/strings.c	Wed Jun 04 23:17:47 2008 +0300
@@ -297,6 +297,47 @@
 }
 
 
+/* This function is here to ASSERT that a given string IS valid UTF-8.
+ * If it is, a copy of the string is returned (use g_free() to deallocate it.)
+ *
+ * However, if the string is NOT valid UTF-8, a warning is printed and a
+ * callstack backtrace is printed in order to see where the problem occured.
+ *
+ * This is a temporary measure for removing useless str_to_utf8 etc. calls
+ * and will be eventually removed...
+ * -- ccr
+ */
+#include <execinfo.h>
+
+gchar *
+str_assert_utf8(const gchar * str)
+{
+    /* NULL in NULL out */
+    if (!str)
+        return NULL;
+
+    /* already UTF-8? */
+    if (!g_utf8_validate(str, -1, NULL)) {
+		gint i, nsymbols;
+		const gint nsymmax = 50;
+		void *addrbuf[nsymmax];
+		gchar **symbols;
+		nsymbols = backtrace(addrbuf, nsymmax);
+		symbols = backtrace_symbols(addrbuf, nsymbols);
+		
+		fprintf(stderr, "WARNING! String '%s' was not UTF-8! Backtrace (%d):\n", str, nsymbols);
+		
+		for (i = 0; i < nsymbols; i++)
+			fprintf(stderr, "#%d > %s\n", i, symbols[i]);
+		
+		free(symbols);
+		
+		return str_to_utf8(str);
+    } else
+    	return g_strdup(str);
+}
+
+
 const gchar *
 str_skip_chars(const gchar * str, const gchar * chars)
 {
--- a/src/audacious/strings.h	Wed Jun 04 22:40:03 2008 +0300
+++ b/src/audacious/strings.h	Wed Jun 04 23:17:47 2008 +0300
@@ -40,6 +40,7 @@
 gboolean str_has_suffix_nocase(const gchar * str, const gchar * suffix);
 gboolean str_has_suffixes_nocase(const gchar * str, gchar * const *suffixes);
 
+gchar *str_assert_utf8(const gchar *str);
 gchar *str_to_utf8(const gchar * str);
 gchar *str_to_utf8_fallback(const gchar * str);