From c8c1d8c27d1d2bd14a6ef5bf54319b90207df25c Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 19 May 2012 13:10:19 -0700 Subject: [PATCH] fix an odd edge-case where there is only a byte left in the destination string, but we're parsing a utf8 character. --- src/util/umlaut.c | 7 +++---- src/util/umlaut_test.c | 9 ++++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/util/umlaut.c b/src/util/umlaut.c index d1063b6d4..81f356a14 100644 --- a/src/util/umlaut.c +++ b/src/util/umlaut.c @@ -57,8 +57,8 @@ char * transliterate(char * out, size_t size, const char * in) } len = src-p; size -= len; - while (size>=2 && *src && (*src & 0x80)) { - int advance = 2; + while (size>0 && *src && (*src & 0x80)) { + unsigned int advance = 2; if (src[0]=='\xc3') { if (src[1]=='\xa4' || src[1]=='\x84') { memcpy(dst, "ae", 2); @@ -69,7 +69,6 @@ char * transliterate(char * out, size_t size, const char * in) } else if (src[1]=='\x9f') { memcpy(dst, "ss", 2); } else { - *dst++='?'; advance = 0; } } else if (src[0]=='\xe1') { @@ -83,7 +82,7 @@ char * transliterate(char * out, size_t size, const char * in) advance = 0; } - if (advance) { + if (advance && advance<=size) { src+=advance; dst+=advance; size-=advance; diff --git a/src/util/umlaut_test.c b/src/util/umlaut_test.c index 2b2b4e4e5..518c63930 100644 --- a/src/util/umlaut_test.c +++ b/src/util/umlaut_test.c @@ -16,7 +16,14 @@ static void test_transliterate(CuTest * tc) CuAssertStrEquals(tc, "haerpdaerp", transliterate(buffer, sizeof(buffer), "h\xc3\xa4rpd\xc3\xa4rp")); CuAssertStrEquals(tc, "aeoeuess", transliterate(buffer, sizeof(buffer), "\xc3\xa4\xc3\xb6\xc3\xbc\xc3\x9f")); CuAssertStrEquals(tc, "aeoeuess", transliterate(buffer, sizeof(buffer), "\xc3\x84\xc3\x96\xc3\x9c\xe1\xba\x9e")); - CuAssertStrEquals(tc, 0, transliterate(buffer, 4, "herpderp")); + + /* handle buffer that is too small */ + CuAssertStrEquals(tc, 0, transliterate(buffer, 1, "herpderp")); + CuAssertStrEquals(tc, "", buffer); + CuAssertStrEquals(tc, 0, transliterate(buffer, 3, "herpderp")); + CuAssertStrEquals(tc, "he", buffer); + CuAssertStrEquals(tc, 0, transliterate(buffer, 3, "h\xc3\xa4rpd\xc3\xa4rp")); + CuAssertStrEquals(tc, "h?", buffer); } static void test_umlaut(CuTest * tc)