UTF-8 validation tests and fixes.

pull/1022/head
Roger A. Light 7 years ago
parent 8c7220d7a5
commit c9d9ad8e72

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

@ -4,6 +4,7 @@
#include <CUnit/Basic.h>
int init_datatype_read_tests(void);
int init_datatype_write_tests(void);
int init_utf8_tests(void);
int main(int argc, char *argv[])

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

Loading…
Cancel
Save