Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d4ab4a5

Browse files
committed
Marc-Andre Lemburg <[email protected]>:
Fixed %c formatting to check for one character arguments. Thanks to Finn Bock for finding this bug. Added a fix for bug PR#348 which originated from not resetting the globals correctly in _PyUnicode_Fini().
1 parent bfa36f5 commit d4ab4a5

1 file changed

Lines changed: 29 additions & 8 deletions

File tree

Objects/unicodeobject.c

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,19 @@ Unicode Integration Proposal (see file Misc/unicode.txt).
108108
# define BYTEORDER_IS_LITTLE_ENDIAN
109109
#endif
110110

111-
/* --- Globals ------------------------------------------------------------ */
111+
/* --- Globals ------------------------------------------------------------
112+
113+
The globals are initialized by the _PyUnicode_Init() API and should
114+
not be used before calling that API.
115+
116+
*/
112117

113118
/* The empty Unicode object */
114-
static PyUnicodeObject *unicode_empty = NULL;
119+
static PyUnicodeObject *unicode_empty;
115120

116121
/* Free list for Unicode objects */
117-
static PyUnicodeObject *unicode_freelist = NULL;
118-
static int unicode_freelist_size = 0;
122+
static PyUnicodeObject *unicode_freelist;
123+
static int unicode_freelist_size;
119124

120125
/* Default encoding to use and assume when NULL is passed as encoding
121126
parameter; it is initialized by _PyUnicode_Init().
@@ -4262,22 +4267,33 @@ static int
42624267
formatchar(Py_UNICODE *buf,
42634268
PyObject *v)
42644269
{
4265-
if (PyUnicode_Check(v))
4270+
if (PyUnicode_Check(v)) {
4271+
if (PyUnicode_GET_SIZE(v) != 1)
4272+
goto onError;
42664273
buf[0] = PyUnicode_AS_UNICODE(v)[0];
4274+
}
42674275

4268-
else if (PyString_Check(v))
4269-
buf[0] = (Py_UNICODE) PyString_AS_STRING(v)[0];
4276+
else if (PyString_Check(v)) {
4277+
if (PyString_GET_SIZE(v) != 1)
4278+
goto onError;
4279+
buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0];
4280+
}
42704281

42714282
else {
42724283
/* Integer input truncated to a character */
42734284
long x;
42744285
x = PyInt_AsLong(v);
42754286
if (x == -1 && PyErr_Occurred())
4276-
return -1;
4287+
goto onError;
42774288
buf[0] = (char) x;
42784289
}
42794290
buf[1] = '\0';
42804291
return 1;
4292+
4293+
onError:
4294+
PyErr_SetString(PyExc_TypeError,
4295+
"%c requires int or char");
4296+
return -1;
42814297
}
42824298

42834299
PyObject *PyUnicode_Format(PyObject *format,
@@ -4709,6 +4725,8 @@ void _PyUnicode_Init()
47094725
"sizeof(Py_UNICODE) != 2 bytes");
47104726

47114727
/* Init the implementation */
4728+
unicode_freelist = NULL;
4729+
unicode_freelist_size = 0;
47124730
unicode_empty = _PyUnicode_New(0);
47134731
strcpy(unicode_default_encoding, "ascii");
47144732
}
@@ -4728,5 +4746,8 @@ _PyUnicode_Fini()
47284746
Py_XDECREF(v->utf8str);
47294747
PyObject_DEL(v);
47304748
}
4749+
unicode_freelist = NULL;
4750+
unicode_freelist_size = 0;
47314751
Py_XDECREF(unicode_empty);
4752+
unicode_empty = NULL;
47324753
}

0 commit comments

Comments
 (0)