diff options
author | Max Kellermann <max@duempel.org> | 2009-01-03 14:52:49 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2009-01-03 14:52:49 +0100 |
commit | 03524fb81c81cf41b10fe1e478cd6a029afe1f29 (patch) | |
tree | be03afa018683a914741301eba5f16efa3b836db /src | |
parent | d342e338db085e8e8f6eded2aef48c7ca41239ae (diff) | |
download | mpd-03524fb81c81cf41b10fe1e478cd6a029afe1f29.tar.gz mpd-03524fb81c81cf41b10fe1e478cd6a029afe1f29.tar.xz mpd-03524fb81c81cf41b10fe1e478cd6a029afe1f29.zip |
tag: use g_strescape() to eliminate evil characters
This always allocates strings on the heap, but we can delete the
stripReturnChar() function now.
Diffstat (limited to 'src')
-rw-r--r-- | src/tag.c | 40 | ||||
-rw-r--r-- | src/utils.c | 7 | ||||
-rw-r--r-- | src/utils.h | 2 |
3 files changed, 22 insertions, 27 deletions
@@ -19,7 +19,6 @@ #include "tag.h" #include "tag_internal.h" #include "tag_pool.h" -#include "utils.h" #include "log.h" #include "conf.h" #include "song.h" @@ -358,7 +357,7 @@ int tag_equal(const struct tag *tag1, const struct tag *tag2) } static char * -fix_utf8(const char *str, size_t *length_r) +fix_utf8(const char *str, size_t length) { char *temp; GError *error = NULL; @@ -366,18 +365,17 @@ fix_utf8(const char *str, size_t *length_r) assert(str != NULL); - if (g_utf8_validate(str, *length_r, NULL)) + if (g_utf8_validate(str, length, NULL)) return NULL; DEBUG("not valid utf8 in tag: %s\n",str); - temp = g_convert(str, *length_r, "utf-8", "iso-8859-1", + temp = g_convert(str, length, "utf-8", "iso-8859-1", NULL, &written, &error); if (temp == NULL) { g_error_free(error); return NULL; } - *length_r = written; return temp; } @@ -413,22 +411,28 @@ void tag_end_add(struct tag *tag) #endif } +static char * +fix_tag_value(const char *p, size_t length) +{ + char *utf8, *escaped; + + utf8 = fix_utf8(p, length); + if (utf8 == NULL) + utf8 = g_strndup(p, length); + + escaped = g_strescape(utf8, NULL); + g_free(utf8); + + return escaped; +} + static void appendToTagItems(struct tag *tag, enum tag_type type, const char *value, size_t len) { unsigned int i = tag->numOfItems; - char *duplicated; - const char *p; - - p = duplicated = fix_utf8(value, &len); - if (p == NULL) - p = value; - if (memchr(p, '\n', len) != NULL) { - if (duplicated == NULL) - p = duplicated = g_strndup(p, len); + char *p; - stripReturnChar(duplicated); - } + p = fix_tag_value(value, len); tag->numOfItems++; @@ -445,10 +449,10 @@ static void appendToTagItems(struct tag *tag, enum tag_type type, } g_mutex_lock(tag_pool_lock); - tag->items[i] = tag_pool_get_item(type, p, len); + tag->items[i] = tag_pool_get_item(type, p, strlen(p)); g_mutex_unlock(tag_pool_lock); - g_free(duplicated); + g_free(p); } void tag_add_item_n(struct tag *tag, enum tag_type itemType, diff --git a/src/utils.c b/src/utils.c index 4815a0bc1..c30712660 100644 --- a/src/utils.c +++ b/src/utils.c @@ -40,13 +40,6 @@ #include <windows.h> #endif -void stripReturnChar(char *string) -{ - while (string && (string = strchr(string, '\n'))) { - *string = ' '; - } -} - void my_usleep(long usec) { #ifdef WIN32 diff --git a/src/utils.h b/src/utils.h index 0e210cd5f..dff41bab6 100644 --- a/src/utils.h +++ b/src/utils.h @@ -36,8 +36,6 @@ } while (0) #endif /* !assert_static */ -void stripReturnChar(char *string); - void my_usleep(long usec); /* trivial functions, keep them inlined */ |