From c9d9ad8e72b914872d590f586ac47b6106f2e470 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Wed, 3 Oct 2018 18:42:15 +0100 Subject: [PATCH] UTF-8 validation tests and fixes. --- lib/utf8_mosq.c | 10 +++++++--- test/unit/test.c | 1 + test/unit/utf8.c | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/utf8_mosq.c b/lib/utf8_mosq.c index d188d3db..b08c9a43 100644 --- a/lib/utf8_mosq.c +++ b/lib/utf8_mosq.c @@ -80,9 +80,13 @@ int mosquitto_validate_utf8(const char *str, int len) } /* Check for overlong or out of range encodings */ - if(codelen == 2 && codepoint < 0x0080){ - return MOSQ_ERR_MALFORMED_UTF8; - }else if(codelen == 3 && codepoint < 0x0800){ + /* Checking codelen == 2 isn't necessary here, because it is already + * covered above in the C0 and C1 checks. + * if(codelen == 2 && codepoint < 0x0080){ + * return MOSQ_ERR_MALFORMED_UTF8; + * }else + */ + if(codelen == 3 && codepoint < 0x0800){ return MOSQ_ERR_MALFORMED_UTF8; }else if(codelen == 4 && (codepoint < 0x10000 || codepoint > 0x10FFFF)){ return MOSQ_ERR_MALFORMED_UTF8; diff --git a/test/unit/test.c b/test/unit/test.c index cb25e3e3..e89fa4d2 100644 --- a/test/unit/test.c +++ b/test/unit/test.c @@ -4,6 +4,7 @@ #include int init_datatype_read_tests(void); +int init_datatype_write_tests(void); int init_utf8_tests(void); int main(int argc, char *argv[]) diff --git a/test/unit/utf8.c b/test/unit/utf8.c index 532df519..a96ebaea 100644 --- a/test/unit/utf8.c +++ b/test/unit/utf8.c @@ -35,6 +35,22 @@ static void TEST_utf8_valid(void) } +static void TEST_utf8_truncated(void) +{ + char buf[4]; + + /* As per boundary condition tests, but less one character */ + buf[0] = 0xC2; buf[1] = 0; + utf8_helper(buf, MOSQ_ERR_MALFORMED_UTF8); + + buf[0] = 0xE0; buf[1] = 0xA0; buf[2] = 0; + utf8_helper(buf, MOSQ_ERR_MALFORMED_UTF8); + + buf[0] = 0xF0; buf[1] = 0x90; buf[2] = 0x80; buf[3] = 0; + utf8_helper(buf, MOSQ_ERR_MALFORMED_UTF8); +} + + static void TEST_utf8_boundary_conditions(void) { /* 2 Boundary condition test cases */ @@ -285,6 +301,7 @@ int init_utf8_tests(void) if(0 || !CU_add_test(test_suite, "UTF-8 empty", TEST_utf8_empty) || !CU_add_test(test_suite, "UTF-8 valid", TEST_utf8_valid) + || !CU_add_test(test_suite, "UTF-8 truncated", TEST_utf8_truncated) || !CU_add_test(test_suite, "UTF-8 boundary conditions", TEST_utf8_boundary_conditions) || !CU_add_test(test_suite, "UTF-8 malformed sequences", TEST_utf8_malformed_sequences) || !CU_add_test(test_suite, "UTF-8 overlong encoding", TEST_utf8_overlong_encoding)