--- a/sys/libiconv/iconv_ucs.c +++ b/sys/libiconv/iconv_ucs.c @@ -473,6 +473,17 @@ ucs4 |= *(src + i) & 0x3f; } + /* + * Reject overlong (non-shortest) encodings: the decoded value must + * not be representable in fewer bytes than the width actually used. + * Without this, e.g. "\xC0\xAF" decodes to U+002F ('/') and is + * written into converted filenames, and 4-byte encodings of BMP + * values make encode_surrogate() wrap below 0x10000. + */ + if ((w == 2 && ucs4 < 0x80) || (w == 3 && ucs4 < 0x800) || + (w == 4 && ucs4 < 0x10000)) + return (0); + *utf8width = w; return (ucs4); }