diff options
author | Armin Rigo <arigo@tunes.org> | 2017-06-04 14:24:35 +0200 |
---|---|---|
committer | Armin Rigo <arigo@tunes.org> | 2017-06-04 14:24:35 +0200 |
commit | 97bde49df02cc481b52426d757396e22df133c72 (patch) | |
tree | 7ee6384bb32eebb4aaa29ade2e0cced49d37a116 /c/wchar_helper.h | |
parent | bcb5c9cc821ea02fd47cfeb61bf485daad587b91 (diff) | |
download | cffi-97bde49df02cc481b52426d757396e22df133c72.tar.gz |
Detect and complain when trying to convert a char32_t to unicode if
the unicode uses 16-bit chars and the original char32_t is out of range
even for surrogates
Diffstat (limited to 'c/wchar_helper.h')
-rw-r--r-- | c/wchar_helper.h | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/c/wchar_helper.h b/c/wchar_helper.h index 945d32d..8e6ea58 100644 --- a/c/wchar_helper.h +++ b/c/wchar_helper.h @@ -195,9 +195,9 @@ static Py_ssize_t _my_PyUnicode_SizeAsChar32(PyObject *unicode) return result; } -static void _my_PyUnicode_AsChar16(PyObject *unicode, - cffi_char16_t *result, - Py_ssize_t resultlen) +static int _my_PyUnicode_AsChar16(PyObject *unicode, + cffi_char16_t *result, + Py_ssize_t resultlen) { Py_ssize_t len = PyUnicode_GET_SIZE(unicode); Py_UNICODE *u = PyUnicode_AS_UNICODE(unicode); @@ -208,9 +208,12 @@ static void _my_PyUnicode_AsChar16(PyObject *unicode, #else cffi_char32_t ordinal = u[i]; if (ordinal > 0xFFFF) { - /* NB. like CPython, ignore the problem of unicode string objects - * containing characters greater than sys.maxunicode. It is - * easier to not add exception handling here */ + if (ordinal > 0x10FFFF) { + PyErr_Format(PyExc_ValueError, + "unicode character out of range for " + "conversion to char16_t: 0x%x", (int)ordinal); + return -1; + } ordinal -= 0x10000; *result++ = 0xD800 | (ordinal >> 10); *result++ = 0xDC00 | (ordinal & 0x3FF); @@ -219,11 +222,12 @@ static void _my_PyUnicode_AsChar16(PyObject *unicode, #endif *result++ = ordinal; } + return 0; } -static void _my_PyUnicode_AsChar32(PyObject *unicode, - cffi_char32_t *result, - Py_ssize_t resultlen) +static int _my_PyUnicode_AsChar32(PyObject *unicode, + cffi_char32_t *result, + Py_ssize_t resultlen) { Py_UNICODE *u = PyUnicode_AS_UNICODE(unicode); Py_ssize_t i; @@ -238,4 +242,5 @@ static void _my_PyUnicode_AsChar32(PyObject *unicode, result[i] = ordinal; u++; } + return 0; } |