From f03e8f3b62619519e397cabd411f30801febf521 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 9 Nov 2016 14:58:51 +0100 Subject: [PATCH] return an error code if the string had any characters removed. --- src/util/unicode.c | 7 +++++-- src/util/unicode.test.c | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/util/unicode.c b/src/util/unicode.c index c4dfb40e2..6ae1a0676 100644 --- a/src/util/unicode.c +++ b/src/util/unicode.c @@ -34,13 +34,14 @@ int unicode_utf8_mkname(utf8_t * op, size_t outlen, const utf8_t * ip) { + int ret = 0; while (*ip) { ucs4_t ucs = *ip; size_t size = 1; bool isp = false; // bool iss = false; if (ucs & 0x80) { - int ret = unicode_utf8_to_ucs4(&ucs, ip, &size); + ret = unicode_utf8_to_ucs4(&ucs, ip, &size); if (ret !=0) { return ret; } @@ -57,6 +58,8 @@ int unicode_utf8_mkname(utf8_t * op, size_t outlen, const utf8_t * ip) memcpy(op, ip, size); op += size; outlen -= size; + } else { + ret = 1; } ip += size; } @@ -64,7 +67,7 @@ int unicode_utf8_mkname(utf8_t * op, size_t outlen, const utf8_t * ip) return ENOMEM; } *op = 0; - return 0; + return ret; } int unicode_utf8_tolower(utf8_t * op, size_t outlen, const utf8_t * ip) diff --git a/src/util/unicode.test.c b/src/util/unicode.test.c index 3cfa0615c..e70e03752 100644 --- a/src/util/unicode.test.c +++ b/src/util/unicode.test.c @@ -8,7 +8,10 @@ static void test_unicode_mkname(CuTest * tc) { char buffer[32]; - CuAssertIntEquals(tc, 0, unicode_utf8_mkname(buffer, sizeof(buffer), "HeLlO\nW0Rld")); + CuAssertIntEquals(tc, 0, unicode_utf8_mkname(buffer, sizeof(buffer), "HeLlO W0Rld")); + CuAssertStrEquals(tc, "HeLlO W0Rld", buffer); + memset(buffer, 0, sizeof(buffer)); + CuAssertIntEquals(tc, 1, unicode_utf8_mkname(buffer, sizeof(buffer), "HeLlO\nW0Rld")); CuAssertStrEquals(tc, "HeLlOW0Rld", buffer); memset(buffer, 0, sizeof(buffer)); buffer[5] = 'X';