Mercurial > cpython-lru_cache-weakref
diff Modules/python.c @ 54427:ec5d228a83ef
Issue #6097: Escape UTF-8 surrogates resulting from mbstocs conversion
of the command line.
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Fri, 29 May 2009 16:22:26 +0000 |
| parents | 7c29bfbfd352 |
| children | f197407bdba2 62d11785204f |
line wrap: on
line diff
--- a/Modules/python.c Fri May 29 16:01:34 2009 +0000 +++ b/Modules/python.c Fri May 29 16:22:26 2009 +0000 @@ -38,8 +38,16 @@ if (!res) goto oom; count = mbstowcs(res, arg, argsize+1); - if (count != (size_t)-1) - return res; + if (count != (size_t)-1) { + wchar_t *tmp; + /* Only use the result if it contains no + surrogate characters. */ + for (tmp = res; *tmp != 0 && + (*tmp < 0xd800 || *tmp > 0xdfff); tmp++) + ; + if (*tmp == 0) + return res; + } PyMem_Free(res); } /* Conversion failed. Fall back to escaping with surrogateescape. */ @@ -75,6 +83,14 @@ memset(&mbs, 0, sizeof mbs); continue; } + if (*out >= 0xd800 && *out <= 0xdfff) { + /* Surrogate character. Escape the original + byte sequence with surrogateescape. */ + argsize -= converted; + while (converted--) + *out++ = 0xdc00 + *in++; + continue; + } /* successfully converted some bytes */ in += converted; argsize -= converted;
