Skip to content
Snippets Groups Projects
Commit 334258743b5c authored by Bob Ippolito's avatar Bob Ippolito
Browse files

correctly calculate max size for wide build

git-svn-id: http://simplejson.googlecode.com/svn/trunk@47 a4795897-2c25-0410-b006-0d3caba88fa1
parent 0b63b759242c
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,13 @@ ...@@ -17,6 +17,13 @@
#define S_CHAR(c) (c >= ' ' && c <= '~' && c != '\\' && c != '/' && c != '"') #define S_CHAR(c) (c >= ' ' && c <= '~' && c != '\\' && c != '/' && c != '"')
#define MIN_EXPANSION 6
#ifdef Py_UNICODE_WIDE
#define MAX_EXPANSION (2 * MIN_EXPANSION)
#else
#define MAX_EXPANSION MIN_EXPANSION
#endif
static Py_ssize_t static Py_ssize_t
ascii_escape_char(Py_UNICODE c, char *output, Py_ssize_t chars) { ascii_escape_char(Py_UNICODE c, char *output, Py_ssize_t chars) {
Py_UNICODE x; Py_UNICODE x;
...@@ -75,7 +82,7 @@ ...@@ -75,7 +82,7 @@
input_chars = PyUnicode_GET_SIZE(pystr); input_chars = PyUnicode_GET_SIZE(pystr);
input_unicode = PyUnicode_AS_UNICODE(pystr); input_unicode = PyUnicode_AS_UNICODE(pystr);
/* One char input can be up to 6 chars output, estimate 4 of these */ /* One char input can be up to 6 chars output, estimate 4 of these */
output_size = 32 + input_chars; output_size = 2 + (MIN_EXPANSION * 4) + input_chars;
rval = PyString_FromStringAndSize(NULL, output_size); rval = PyString_FromStringAndSize(NULL, output_size);
if (rval == NULL) { if (rval == NULL) {
return NULL; return NULL;
...@@ -90,6 +97,6 @@ ...@@ -90,6 +97,6 @@
} else { } else {
chars = ascii_escape_char(c, output, chars); chars = ascii_escape_char(c, output, chars);
} }
if (output_size - chars < 7) { if (output_size - chars < (1 + MAX_EXPANSION)) {
/* There's more than four, so let's resize by a lot */ /* There's more than four, so let's resize by a lot */
output_size *= 2; output_size *= 2;
...@@ -94,7 +101,8 @@ ...@@ -94,7 +101,8 @@
/* There's more than four, so let's resize by a lot */ /* There's more than four, so let's resize by a lot */
output_size *= 2; output_size *= 2;
if (output_size > 2 + (input_chars * 6)) { /* This is an upper bound */
output_size = 2 + (input_chars * 6); if (output_size > 2 + (input_chars * MAX_EXPANSION)) {
output_size = 2 + (input_chars * MAX_EXPANSION);
} }
if (_PyString_Resize(&rval, output_size) == -1) { if (_PyString_Resize(&rval, output_size) == -1) {
return NULL; return NULL;
...@@ -122,7 +130,7 @@ ...@@ -122,7 +130,7 @@
input_chars = PyString_GET_SIZE(pystr); input_chars = PyString_GET_SIZE(pystr);
input_str = PyString_AS_STRING(pystr); input_str = PyString_AS_STRING(pystr);
/* One char input can be up to 6 chars output, estimate 4 of these */ /* One char input can be up to 6 chars output, estimate 4 of these */
output_size = 32 + input_chars; output_size = 2 + (MIN_EXPANSION * 4) + input_chars;
rval = PyString_FromStringAndSize(NULL, output_size); rval = PyString_FromStringAndSize(NULL, output_size);
if (rval == NULL) { if (rval == NULL) {
return NULL; return NULL;
...@@ -148,6 +156,7 @@ ...@@ -148,6 +156,7 @@
} else { } else {
chars = ascii_escape_char(c, output, chars); chars = ascii_escape_char(c, output, chars);
} }
if (output_size - chars < 7) { /* An ASCII char can't possibly expand to a surrogate! */
if (output_size - chars < (1 + MIN_EXPANSION)) {
/* There's more than four, so let's resize by a lot */ /* There's more than four, so let's resize by a lot */
output_size *= 2; output_size *= 2;
...@@ -152,7 +161,7 @@ ...@@ -152,7 +161,7 @@
/* There's more than four, so let's resize by a lot */ /* There's more than four, so let's resize by a lot */
output_size *= 2; output_size *= 2;
if (output_size > 2 + (input_chars * 6)) { if (output_size > 2 + (input_chars * MIN_EXPANSION)) {
output_size = 2 + (input_chars * 6); output_size = 2 + (input_chars * MIN_EXPANSION);
} }
if (_PyString_Resize(&rval, output_size) == -1) { if (_PyString_Resize(&rval, output_size) == -1) {
return NULL; return NULL;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment