py3: Fix conversion of 1-byte python strings to ICU strings

This commit is contained in:
Kovid Goyal 2019-04-02 15:56:06 +05:30
parent bea653d594
commit 8bb15338f5
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -134,19 +134,18 @@ static UChar* python_to_icu(PyObject *obj, int32_t *osz) {
switch(PyUnicode_KIND(obj)) { switch(PyUnicode_KIND(obj)) {
case PyUnicode_1BYTE_KIND: case PyUnicode_1BYTE_KIND: {
ans = (UChar*) malloc((sz+1) * sizeof(UChar)); Py_ssize_t data_sz;
if (ans == NULL) { const char *utf8_data = PyUnicode_AsUTF8AndSize(obj, &data_sz);
PyErr_NoMemory(); if (!utf8_data) return NULL;
return NULL; size_t buf_sz = (sz > data_sz ? sz : data_sz) + 1;
} ans = (UChar*) malloc(buf_sz * sizeof(UChar));
u_strFromUTF8( if (ans == NULL) { PyErr_NoMemory(); return NULL; }
ans, sz + 1, u_strFromUTF8Lenient(ans, buf_sz, (int32_t*) osz, utf8_data, (int32_t)data_sz, &status);
(int32_t*) osz, // add null terminator
(char*) PyUnicode_1BYTE_DATA(obj), ans[buf_sz-1] = 0;
(int32_t) sz,
&status);
break; break;
}
case PyUnicode_2BYTE_KIND: case PyUnicode_2BYTE_KIND:
ans = (UChar*) malloc((sz+1) * sizeof(UChar)); ans = (UChar*) malloc((sz+1) * sizeof(UChar));
data = PyUnicode_2BYTE_DATA(obj); data = PyUnicode_2BYTE_DATA(obj);