1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 19:27:36 +00:00

ICC: Strip trailing nul characters from MultiLocalizedUnicodeTagData

Having those trailing nuls is invalid per spec, but it happens in
practice (in already checked-in test files, no less).
This commit is contained in:
Nico Weber 2023-06-26 13:44:13 -04:00 committed by Sam Atkins
parent dedbc17160
commit 3dd6638177
2 changed files with 19 additions and 0 deletions

View file

@ -719,6 +719,13 @@ ErrorOr<NonnullRefPtr<MultiLocalizedUnicodeTagData>> MultiLocalizedUnicodeTagDat
return Error::from_string_literal("ICC::Profile: multiLocalizedUnicodeType string offset out of bounds");
StringView utf_16be_data { bytes.data() + record.string_offset_in_bytes, record.string_length_in_bytes };
// Despite the "should not be NULL terminated" in the spec, some files in the wild have trailing NULLs.
// Fix up this case here, so that application code doesn't have to worry about it.
// (If this wasn't hit in practice, we'd return an Error instead.)
while (utf_16be_data.length() >= 2 && utf_16be_data.ends_with(StringView("\0", 2)))
utf_16be_data = utf_16be_data.substring_view(0, utf_16be_data.length() - 2);
records[i].text = TRY(utf_16be_decoder.to_utf8(utf_16be_data));
}