<i>Tcl interprets two adjacent surrogate code points as a character encoded using UTF-16</i> status still Open with 3 other changes
by
.
D 2021-03-06T20:37:00.341
J comment This\sreport\swas\sinspired\sby\s[debd088e48998a75].\r\n\r\nVarious\sUnicode\scode\spoints\sdo\snot\scorrespond\sto\sany\sabstract\scharacter.\s\sSuch\r\ncode\spoints\sinclude\sprivate-use\scode\spoints,\ssurrogate\scode\spoints,\r\nnoncharacters,\sspecials,\stag\scharacters,\sand\sunassigned\scode\spoints.\s\sAlthough\r\nnone\sof\sthese\scode\spoints\scorrespond\sto\sany\sabstract\scharacter,\sthe\sUnicode\r\nstandard\stakes\scare\snot\sto\sforbid\sthese\scode\spoints\sin\sa\sUnicode\sstring.\r\nRather\sthan\ssaying\sthat\ssuch\scharacters\sand\snon-characters\scan\snot\soccur\sin\sa\r\ntext,\sit\ssimply\slabels\ssome\sof\sthem\sas\s"restricted\sinterchange".\s\sElsewhere,\r\nthe\sstandard\sexplicitly\spermits\stheir\suse\sfor\sinternal\spurposes.\r\n\r\nA\ssurrogate\spair\sis\sa\ssequence\sof\stwo\scode\spoints\sused\sin\sutf-16\sto\srepresent\sa\r\ncharacter\sbeyond\sthose\sthat\scould\sbe\srepresented\sin\sucs-2.\s\sWhen\sa\ssurrogate\r\npair\sis\sencountered\sduring\sthe\sdecoding\sof\stext\sencoded\sin\sutf-16,\sit\sis\r\ntranslated\sinto\sanother\scode\spoint.\r\n\r\nThese\ssurrogate\scode\spoints\swere\sadded\sto\sUnicode\sto\sensure\sno\soverlap\sbetween\r\ntheir\suse\sin\sutf-16\sand\sother\s**external**\suses.\r\n\r\nAccording\sto\sthe\sUnicode\sstandard,\r\n\r\n\s\s\s\s"Surrogate\s\scode\s\spoints\s\scannot\sbe\sconformantly\s\sinterchanged\s\susing\s\sUnicode\sencoding\sforms.\sThey\sdo\snot\scorrespond\sto\sUnicode\sscalar\svalues\sand\sthus\sdo\snot\shave\swell-formed\srepresentations\sin\sany\sUnicode\sencoding\sform."\r\n\r\nThe\sUnicode\sstandard\salso\sstates\sthat\r\n\r\n\s\s\s\s"A\sprocess\sshall\snot\sinterpret\sa\shigh-surrogate\scode\spoint\sor\sa\slow-surrogate\scode\spoint\sas\san\sabstract\scharacter."\r\n\r\nWhat\sit\sdoes\snot\ssay\sis\sthat\sa\ssurrogate\scode\spoint\scan\snot\sbe\sused\sinternally\r\nfor\ssome\sarbitrary\spurpose.\s\sWe\salso\sknow\sthat\sthe\s"abstract\scharacter"\r\nprohibition\sdoesn't\simply\sa\sprohibition\son\sinternal\suse\sbecause\sthe\r\nUnicode\sstandard\sexplicitly\ssays\sthis\sabout\snoncharacter\scode\spoints:\r\n\r\n\s\s\s\s"The\snoncharacter\scode\spoints\smay\sbe\sused\sinternally,\ssuch\sas\sfor\ssentinel\svalues\sor\sdelimiters,\sbut\sshould\snot\sbe\sexchanged\spublicly."\r\n\r\nTcl\scorrectly\shandles\sthe\scase\sof\sa\ssingle\ssurrogate\scode\spoint\sby\ssimply\r\nincluding\sthe\scode\spoint\sin\sthe\sstring\sas\srequested\sby\sthe\sscript\sauthor:\r\n\r\n<code><verbatim>\r\nstring\slength\sxxx\\ud83eyyy\r\n#->\s7\r\n</verbatim></code>\r\n\r\n\r\nBut\swhen\stwo\ssurrogate\scode\spoints\soccur\ssuccessively\sTcl\sdoes\ssomething\r\nbizarre:\s\sIt\stranslates\sthe\stwo\scode\spoints\sinto\sanother\scode\spoint\sas\sif\sit\r\nwas\sa\sutf-16\sdecoder.\r\n\r\n<code><verbatim>\r\nexpr\s{\r\n\s\s\s\s[encoding\sconvertto\sutf-8\s\\ud83e\\udd1d]\r\n\s\s\s\seq\r\n\s\s\s\s[encoding\sconvertto\sutf-8\s\\U1f91d]\r\n}\r\n#\s->\s1\r\n</verbatim></code>\r\n\r\nThis\sis\sa\sbig\smistake.\r\n\r\nTcl\sis\snot\sa\sutf-16\sdecoder.\s\sFor\stranscoding\sutf-16\sTcl\sprovides\s\r\n\r\n<code><verbatim>\r\nencoding\sconvertfrom/convertto\sutf-16\r\n</verbatim></code>\r\n\r\nTcl\sis\salso\snot\sa\sUnicode\sapplication.\s\sRather,\sit\sis\sa\stool\sfor\screating\r\nUnicode\sapplications.\s\sIn\sfact,\sit\sis\seven\sfurther\sremoved\sthan\sthat:\s\sIt\sis\sa\r\ntool\sfor\screating\stools\sthat\screate\sUnicode\sapplications.\s\sAs\ssuch,\sTcl\smust\sbe\r\n**Unicode-agnostic**,\snot\s**Unicode-conformant**.\s\sAs\san\sall-purpose\stool\sfor\r\ncreating\sUnicode-conformant\sapplications,\sTcl\smust\sprovide\sthe\scapability\sto\r\ncreate\sarbitrary\ssequences\sof\sUnicode\scode\spoints\sso\sthat\sapplication\sauthors\r\ncan\screate\ssystems\sthat\smeet\stheir\sneeds.\r\n\r\nWhat\sthis\smeans,\sfor\sexample,\sis\sthat\srather\sthan\sperforming\snormalization,\sTcl\r\nshould\sprovide\sroutines\sthat\sperform\swhatever\snormalization\sa\r\nparticular\sapplication\srequires.\s\sIn\sthe\scase\sat\shand,\sthough,\swhat\sit\smeans\r\nis\sthat\sTcl\s**should\snot**\stry\sto\sconform\sto\sthe\sUnicode\sstandard\sby\sbecoming\sa\r\nquasi\sutf-16\stranscoder.\s\sThe\sapplication\s(script)\sauthor\sshould\sbe\sfree\sto\r\ncraft\swhatever\sUnicode\sstrings\sthey\ssee\sfit\sto\scraft\sfor\sinternal\suse.\s\sIf\sthe\r\nauthor\swants\sthe\sHANDSHAKE\scharacter,\sthere\sis\san\seasy\sway\sto\sdo\sthat:\r\n\r\n<code><verbatim>\r\nset\sstring\s\\u1f91d\r\n</verbatim></code>\r\n\r\nLikewise,\sif\sthe\sauthor\swishes\sto\screate\sa\ssequence\sof\stwo\scode\spoints\swhich\r\nboth\shappen\sto\sbe\ssurrogates,\sthere\sshould\sbe\san\seasy\sway\sto\sdo\sthat:\r\n\r\n<code><verbatim>\r\nstring\slength\s\\ud83e\\udd1d\r\n#->\s2\r\n</verbatim></code>\r\n\r\nThese\stwo\scode\spoints\sshould\ssubsequently\scontinue\sto\sbe\streated\sas\stwo\scode\r\npoints.\s\sThis\smakes\sit\spossible\sto\s\r\n\r\nThe\sscript\slevel\sis\snot\sthe\splace\sto\sexpose\sdetails\sof\sutf-16\sencoding.\r\nRather,\sit\sshould\sremain\sa\spure\sand\sflexible\sUnicode\senvironment\swhere\sit\sis\r\npossible\sto\scraft\sany\ssequence\sof\scode\spoints.\s\sIt\sis\sthe\sresponsibility\sof\sthe\r\napplication\sauthor\sto\sultimately\sproduce\sconformant\stext.\s\sIf\sa\sscript\sattempts\r\nto\sconvert\sto\sutf-16\sa\ssequence\sof\scode\spoints\sthat\scan't\sbe\srepresented\sin\r\nutf-16,\sTcl\sshould\sthen\sproduce\sand\serror,\sas\sthis\sis\sthe\sboundary\sbetween\r\ninternal\sand\sexternal\suse.\r\n\r\nRecent\stickets\shint\sat\sthe\stroubles\sthis\snew\sbehaviour\swill\scause.\s\sOne\sexample\r\nis\s[22324bcbdf].\s\sThe\sincorrect\sassumption\sin\sthis\sticket\sis\sthat\r\n<code>\\ud83d\\udca3</code>\s\sshould\sbe\sinterpreted\sas\sthe\sBOMB\scharacter.\s\sIt\r\nshould\snot.\s\sRather,\sit\sshould\sbe\sinterpreted\sas\stwo\sUnicode\scode\spoints\sthat\r\neach\scorrespond\sto\sno\sabstract\scharacter.\s\sString\sreversal\sof\sthese\stwo\scode\r\npoints\sthen\sbecomes\strivial:\s\sThe\sresult\sof\sreversing\s<code>\\ud83d\\udca3</code>\r\nwould\sbe\s<code>\\udca3\\ud83d</code>\r\n\r\nIn\sshort,\sthe\sonly\stime\sTcl\sshould\sdo\sanything\sspecial\swith\ssurrogate\scode\r\npoints\sis\swhen\sit\sis\sconverting\sto/from\sutf-16.\s\sTreating\stwo\sconsecutive\r\nsurrogate\scode\spoints\sas\sthe\scharacter\sthey\sencode\sin\sutf-16\smakes\sas\smuch\r\nsense\sas\sas\streating\sthe\soccurrence\sof\s<code>\\xc0\\x80</code>\sin\sa\sscript\sas\sa\r\n<code>NULL</code>\scharacter,\sand\swould\shave\ssimilar\sconsequences.\s\sThis\smode\sof\r\noperation\scripples\sTcl\sas\sa\susable\sUnicode\senvironment.\s\sHopefully\sit\swill\sbe\r\nbacked\sout\sbefore\sit's\stoo\slate.
J login pooryorick
J mimetype text/plain
K 0d61d3a2bb905178694d6cde8147039974600274
U pooryorick
Z 7b9b967a2f11b77e9cce95d34f3c7bac