fix an odd edge-case where there is only a byte left in the destination string, but we're parsing a utf8 character.

This commit is contained in:
Enno Rehling 2012-05-19 13:10:19 -07:00
parent b44737387a
commit 71f1e35f6d
2 changed files with 11 additions and 5 deletions

View file

@ -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;

View file

@ -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)