summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorNeal Norwitz <nnorwitz@gmail.com>2006-08-12 01:57:47 (GMT)
committerNeal Norwitz <nnorwitz@gmail.com>2006-08-12 01:57:47 (GMT)
commitb45f351832b00c80bf9881e92b12c330324e3116 (patch)
tree76f0d4900b48dfc93dd2c6a40f0aa9e531038005 /Modules
parentef0de023db9bc520312e5f0f59bbc9a47f0f204e (diff)
downloadcpython-b45f351832b00c80bf9881e92b12c330324e3116.zip
cpython-b45f351832b00c80bf9881e92b12c330324e3116.tar.gz
cpython-b45f351832b00c80bf9881e92b12c330324e3116.tar.bz2
I'm not sure why this code allocates this string for the error message.
I think it would be better to always use snprintf and have the format limit the size of the name appropriately (like %.200s). Klocwork #340
Diffstat (limited to 'Modules')
-rw-r--r--Modules/unicodedata.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/Modules/unicodedata.c b/Modules/unicodedata.c
index 7f8592f..a11a0b7 100644
--- a/Modules/unicodedata.c
+++ b/Modules/unicodedata.c
@@ -1078,6 +1078,7 @@ unicodedata_lookup(PyObject* self, PyObject* args)
{
Py_UCS4 code;
Py_UNICODE str[1];
+ char errbuf[256];
char* name;
int namelen;
@@ -1085,11 +1086,19 @@ unicodedata_lookup(PyObject* self, PyObject* args)
return NULL;
if (!_getcode(self, name, namelen, &code)) {
+ /* XXX(nnorwitz): why are we allocating for the error msg?
+ Why not always use snprintf? */
char fmt[] = "undefined character name '%s'";
char *buf = PyMem_MALLOC(sizeof(fmt) + namelen);
- sprintf(buf, fmt, name);
+ if (buf)
+ sprintf(buf, fmt, name);
+ else {
+ buf = errbuf;
+ PyOS_snprintf(buf, sizeof(errbuf), fmt, name);
+ }
PyErr_SetString(PyExc_KeyError, buf);
- PyMem_FREE(buf);
+ if (buf != errbuf)
+ PyMem_FREE(buf);
return NULL;
}