From: Martin v. Löwis Date: Thu, 6 Nov 2003 20:47:57 +0000 (+0000) Subject: Overallocate target buffer for normalization more early. Fixes #834676. X-Git-Tag: v2.4a1~1323 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d2171d2ba414def2ecf27b694ea27c2e9fde0fcf;p=python Overallocate target buffer for normalization more early. Fixes #834676. Backported to 2.3. --- diff --git a/Lib/test/test_normalization.py b/Lib/test/test_normalization.py index 046dca6c85..0cbc2b49e9 100644 --- a/Lib/test/test_normalization.py +++ b/Lib/test/test_normalization.py @@ -84,5 +84,8 @@ def test_main(): continue assert X == NFC(X) == NFD(X) == NFKC(X) == NFKD(X), c + # Check for bug 834676 + normalize('NFC',u'\ud55c\uae00') + if __name__ == "__main__": test_main() diff --git a/Modules/unicodedata.c b/Modules/unicodedata.c index d266ad7e0b..311db296bd 100644 --- a/Modules/unicodedata.c +++ b/Modules/unicodedata.c @@ -311,12 +311,14 @@ nfd_nfkd(PyObject *input, int k) stack[stackptr++] = *i++; while(stackptr) { Py_UNICODE code = stack[--stackptr]; - if (!space) { - space = PyString_GET_SIZE(result) + 10; - if (PyUnicode_Resize(&result, space) == -1) + /* Hangul Decomposition adds three characters in + a single step, so we need atleast that much room. */ + if (space < 3) { + int newsize = PyString_GET_SIZE(result) + 10; + space += 10; + if (PyUnicode_Resize(&result, newsize) == -1) return NULL; - o = PyUnicode_AS_UNICODE(result) + space - 10; - space = 10; + o = PyUnicode_AS_UNICODE(result) + newsize - space; } /* Hangul Decomposition. */ if (SBase <= code && code < (SBase+SCount)) {