unicode : reject overlong UTF-8 and out-of-range codepoints (#30485)

* unicode : reject overlong UTF-8 and out-of-range codepoints

* Update tests/test-unicode.cpp

Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@huggingface.co>

* tests: document byte-wise UTF-8 error recovery

* break out of first test on failure

---------

Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@huggingface.co>
This commit is contained in:
Simone IacominoandSigbjørn Skjæret authored and GitHub committed 2026-10-15 10:12:56 +03:00
1 parent 68ac765726
commit 2ce20d22fd
2 files changed
+52 -3

No files matched your search

+9
View File
@@ -42,6 +42,9 @@ uint32_t unicode_cpt_from_utf8(const std::string & utf8, size_t & offset) {
throw std::invalid_argument("invalid character");
}
auto result = ((utf8[offset + 0] & 0x1f) << 6) | (utf8[offset + 1] & 0x3f);
if (result < 0x80) {
throw std::invalid_argument("invalid character");
}
offset += 2;
return result;
}
@@ -50,6 +53,9 @@ uint32_t unicode_cpt_from_utf8(const std::string & utf8, size_t & offset) {
throw std::invalid_argument("invalid character");
}
auto result = ((utf8[offset + 0] & 0x0f) << 12) | ((utf8[offset + 1] & 0x3f) << 6) | (utf8[offset + 2] & 0x3f);
if (result < 0x800) {
throw std::invalid_argument("invalid character");
}
offset += 3;
return result;
}
@@ -58,6 +64,9 @@ uint32_t unicode_cpt_from_utf8(const std::string & utf8, size_t & offset) {
throw std::invalid_argument("invalid character");
}
auto result = ((utf8[offset + 0] & 0x07) << 18) | ((utf8[offset + 1] & 0x3f) << 12) | ((utf8[offset + 2] & 0x3f) << 6) | (utf8[offset + 3] & 0x3f);
if (result < 0x10000 || result > 0x10ffff) {
throw std::invalid_argument("invalid character");
}
offset += 4;
return result;
}