mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Patch by Serhiy Storchaka.
This commit is contained in:
parent
20b8d992b0
commit
6f80f5d444
4 changed files with 135 additions and 3 deletions
|
@ -5250,12 +5250,36 @@ PyObject *PyUnicode_DecodeCharmap(const char *s,
|
|||
/* Apply mapping */
|
||||
if (PyLong_Check(x)) {
|
||||
long value = PyLong_AS_LONG(x);
|
||||
if (value < 0 || value > 65535) {
|
||||
if (value < 0 || value > 0x10FFFF) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"character mapping must be in range(65536)");
|
||||
"character mapping must be in range(0x110000)");
|
||||
Py_DECREF(x);
|
||||
goto onError;
|
||||
}
|
||||
|
||||
#ifndef Py_UNICODE_WIDE
|
||||
if (value > 0xFFFF) {
|
||||
/* see the code for 1-n mapping below */
|
||||
if (extrachars < 2) {
|
||||
/* resize first */
|
||||
Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v);
|
||||
Py_ssize_t needed = 10 - extrachars;
|
||||
extrachars += needed;
|
||||
/* XXX overflow detection missing */
|
||||
if (_PyUnicode_Resize(&v,
|
||||
PyUnicode_GET_SIZE(v) + needed) < 0) {
|
||||
Py_DECREF(x);
|
||||
goto onError;
|
||||
}
|
||||
p = PyUnicode_AS_UNICODE(v) + oldpos;
|
||||
}
|
||||
value -= 0x10000;
|
||||
*p++ = 0xD800 | (value >> 10);
|
||||
*p++ = 0xDC00 | (value & 0x3FF);
|
||||
extrachars -= 2;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
*p++ = (Py_UNICODE)value;
|
||||
}
|
||||
else if (x == Py_None) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue