# HG changeset patch # User Matti Hamalainen # Date 1212610667 -10800 # Node ID 6b76f8589f5db31475422730bc11fb50ab04777c # Parent 9df19c4900ba0777df074dc8053a63e6e4d7a907 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. diff -r 9df19c4900ba -r 6b76f8589f5d src/audacious/strings.c --- 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 + +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) { diff -r 9df19c4900ba -r 6b76f8589f5d src/audacious/strings.h --- 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);