From 8728fccaec24940f35c4927d65f95b79aa80e664 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 2 Oct 2016 12:46:07 +0200 Subject: [PATCH] fix an endless loop in unicode_utf8_to_cp437 for characters not in the table. --- src/util/unicode.c | 12 ++++++++---- src/util/unicode.test.c | 11 +++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/util/unicode.c b/src/util/unicode.c index 94b6e3d5a..ffc170cc9 100644 --- a/src/util/unicode.c +++ b/src/util/unicode.c @@ -506,10 +506,14 @@ size_t * length) *cp_character = (char)xref[m].cp437; break; } - else if (xref[m].ucs4 < ucs4_character) - l = m; - else - r = m; + else if (xref[m].ucs4 < ucs4_character) { + if (l == m) l = r; + else l = m; + } + else { + if (r == m) r = l; + else r = m; + } } if (l == r) { *cp_character = '?'; diff --git a/src/util/unicode.test.c b/src/util/unicode.test.c index 958b695e2..b643775a9 100644 --- a/src/util/unicode.test.c +++ b/src/util/unicode.test.c @@ -16,9 +16,20 @@ static void test_unicode_tolower(CuTest * tc) CuAssertStrEquals(tc, "helloX", buffer); } +static void test_unicode_utf8_to_cp437(CuTest *tc) +{ + const char utf8_str[4] = { 0xc3, 0x98, 'l', 0 }; // Øl + char ch; + size_t sz; + CuAssertIntEquals(tc, 0, unicode_utf8_to_cp437(&ch, utf8_str, &sz)); + CuAssertIntEquals(tc, 2, sz); + CuAssertIntEquals(tc, '?', ch); +} + CuSuite *get_unicode_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_unicode_tolower); + SUITE_ADD_TEST(suite, test_unicode_utf8_to_cp437); return suite; }