diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/src/common/ucsnarrow.c | 19 | ||||
-rw-r--r-- | numpy/core/src/multiarray/arrayobject.c | 21 |
2 files changed, 21 insertions, 19 deletions
diff --git a/numpy/core/src/common/ucsnarrow.c b/numpy/core/src/common/ucsnarrow.c index 125235381..946a72257 100644 --- a/numpy/core/src/common/ucsnarrow.c +++ b/numpy/core/src/common/ucsnarrow.c @@ -107,12 +107,11 @@ PyUCS2Buffer_AsUCS4(Py_UNICODE const *ucs2, npy_ucs4 *ucs4, int ucs2len, int ucs * new_reference: PyUnicodeObject */ NPY_NO_EXPORT PyUnicodeObject * -PyUnicode_FromUCS4(char const *src, Py_ssize_t size, int swap, int align) +PyUnicode_FromUCS4(char const *src_char, Py_ssize_t size, int swap, int align) { Py_ssize_t ucs4len = size / sizeof(npy_ucs4); - /* FIXME: This is safe, but better to rewrite to not cast away const */ - npy_ucs4 *buf = (npy_ucs4 *)(char *)src; - int alloc = 0; + npy_ucs4 const *src = (npy_ucs4 const *)src_char; + npy_ucs4 *buf = NULL; PyUnicodeObject *ret; /* swap and align if needed */ @@ -122,22 +121,22 @@ PyUnicode_FromUCS4(char const *src, Py_ssize_t size, int swap, int align) PyErr_NoMemory(); goto fail; } - alloc = 1; memcpy(buf, src, size); if (swap) { byte_swap_vector(buf, ucs4len, sizeof(npy_ucs4)); } + src = buf; } /* trim trailing zeros */ - while (ucs4len > 0 && buf[ucs4len - 1] == 0) { + while (ucs4len > 0 && src[ucs4len - 1] == 0) { ucs4len--; } /* produce PyUnicode object */ #ifdef Py_UNICODE_WIDE { - ret = (PyUnicodeObject *)PyUnicode_FromUnicode((Py_UNICODE const*)buf, + ret = (PyUnicodeObject *)PyUnicode_FromUnicode((Py_UNICODE const*)src, (Py_ssize_t) ucs4len); if (ret == NULL) { goto fail; @@ -153,7 +152,7 @@ PyUnicode_FromUCS4(char const *src, Py_ssize_t size, int swap, int align) PyErr_NoMemory(); goto fail; } - ucs2len = PyUCS2Buffer_FromUCS4(tmp, buf, ucs4len); + ucs2len = PyUCS2Buffer_FromUCS4(tmp, src, ucs4len); ret = (PyUnicodeObject *)PyUnicode_FromUnicode(tmp, (Py_ssize_t) ucs2len); free(tmp); if (ret == NULL) { @@ -162,13 +161,13 @@ PyUnicode_FromUCS4(char const *src, Py_ssize_t size, int swap, int align) } #endif - if (alloc) { + if (buf) { free(buf); } return ret; fail: - if (alloc) { + if (buf) { free(buf); } return NULL; diff --git a/numpy/core/src/multiarray/arrayobject.c b/numpy/core/src/multiarray/arrayobject.c index 5a7f85b1a..82eda3464 100644 --- a/numpy/core/src/multiarray/arrayobject.c +++ b/numpy/core/src/multiarray/arrayobject.c @@ -706,35 +706,37 @@ static int _myunincmp(npy_ucs4 const *s1, npy_ucs4 const *s2, int len1, int len2) { npy_ucs4 const *sptr; - /* FIXME: Casting away const makes the below easier to write, but should - * still be safe. - */ - npy_ucs4 *s1t = (npy_ucs4 *)s1, *s2t = (npy_ucs4 *)s2; + npy_ucs4 *s1t = NULL; + npy_ucs4 *s2t = NULL; int val; npy_intp size; int diff; + /* Replace `s1` and `s2` with aligned copies if needed */ if ((npy_intp)s1 % sizeof(npy_ucs4) != 0) { size = len1*sizeof(npy_ucs4); s1t = malloc(size); memcpy(s1t, s1, size); + s1 = s1t; } if ((npy_intp)s2 % sizeof(npy_ucs4) != 0) { size = len2*sizeof(npy_ucs4); s2t = malloc(size); memcpy(s2t, s2, size); + s2 = s1t; } - val = PyArray_CompareUCS4(s1t, s2t, PyArray_MIN(len1,len2)); + + val = PyArray_CompareUCS4(s1, s2, PyArray_MIN(len1,len2)); if ((val != 0) || (len1 == len2)) { goto finish; } if (len2 > len1) { - sptr = s2t+len1; + sptr = s2+len1; val = -1; diff = len2-len1; } else { - sptr = s1t+len2; + sptr = s1+len2; val = 1; diff=len1-len2; } @@ -747,10 +749,11 @@ _myunincmp(npy_ucs4 const *s1, npy_ucs4 const *s2, int len1, int len2) val = 0; finish: - if (s1t != s1) { + /* Cleanup the aligned copies */ + if (s1t) { free(s1t); } - if (s2t != s2) { + if (s2t) { free(s2t); } return val; |