Commit b45f3518 authored by Neal Norwitz's avatar Neal Norwitz

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
parent ef0de023
...@@ -1078,6 +1078,7 @@ unicodedata_lookup(PyObject* self, PyObject* args) ...@@ -1078,6 +1078,7 @@ unicodedata_lookup(PyObject* self, PyObject* args)
{ {
Py_UCS4 code; Py_UCS4 code;
Py_UNICODE str[1]; Py_UNICODE str[1];
char errbuf[256];
char* name; char* name;
int namelen; int namelen;
...@@ -1085,10 +1086,18 @@ unicodedata_lookup(PyObject* self, PyObject* args) ...@@ -1085,10 +1086,18 @@ unicodedata_lookup(PyObject* self, PyObject* args)
return NULL; return NULL;
if (!_getcode(self, name, namelen, &code)) { 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 fmt[] = "undefined character name '%s'";
char *buf = PyMem_MALLOC(sizeof(fmt) + namelen); char *buf = PyMem_MALLOC(sizeof(fmt) + namelen);
if (buf)
sprintf(buf, fmt, name); sprintf(buf, fmt, name);
else {
buf = errbuf;
PyOS_snprintf(buf, sizeof(errbuf), fmt, name);
}
PyErr_SetString(PyExc_KeyError, buf); PyErr_SetString(PyExc_KeyError, buf);
if (buf != errbuf)
PyMem_FREE(buf); PyMem_FREE(buf);
return NULL; return NULL;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment