]> granicus.if.org Git - python/commitdiff
I'm not sure why this code allocates this string for the error message.
authorNeal Norwitz <nnorwitz@gmail.com>
Sat, 12 Aug 2006 01:57:47 +0000 (01:57 +0000)
committerNeal Norwitz <nnorwitz@gmail.com>
Sat, 12 Aug 2006 01:57:47 +0000 (01:57 +0000)
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

Modules/unicodedata.c

index 7f8592f03acbd209854bee05a6c7a348a306ed33..a11a0b70b7cbb87474456865f0b4a279a3b66969 100644 (file)
@@ -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;
     }