From b670d5f372dcb622307c974784ddd5269f934032 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 24 Feb 2019 13:55:08 +0100 Subject: [PATCH] - fixed utf8_decode. This function was written for already validated UTF-8 but not for text that can be mixed with ISO-8859-1. To handle that properly it needs to do a bit more validation to avoid mangling its output and instead reject invalid input. --- src/utility/utf8.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/utility/utf8.cpp b/src/utility/utf8.cpp index 14f7b7983..ca2a4733a 100644 --- a/src/utility/utf8.cpp +++ b/src/utility/utf8.cpp @@ -94,7 +94,9 @@ int utf8_decode(const uint8_t *src, int *size) return c; } - int c1 = src[1] & 0x3f; + int c1 = src[1]; + if (c1 < 0x80 || c1 >= 0xc0) return -1; + c1 &= 0x3f; if ((c & 0xE0) == 0xC0) { @@ -107,7 +109,9 @@ int utf8_decode(const uint8_t *src, int *size) return -1; } - int c2 = src[2] & 0x3f; + int c2 = src[2]; + if (c2 < 0x80 || c2 >= 0xc0) return -1; + c2 &= 0x3f; if ((c & 0xF0) == 0xE0) { @@ -120,7 +124,9 @@ int utf8_decode(const uint8_t *src, int *size) return -1; } - int c3 = src[3] & 0x3f; + int c3 = src[3]; + if (c3 < 0x80 || c1 >= 0xc0) return -1; + c3 &= 0x3f; if ((c & 0xF8) == 0xF0) {