[Python-checkins] r51226 - python/trunk/Modules/unicodedata.c

neal.norwitz python-checkins at python.org
Sat Aug 12 03:57:47 CEST 2006


Author: neal.norwitz
Date: Sat Aug 12 03:57:47 2006
New Revision: 51226

Modified:
   python/trunk/Modules/unicodedata.c
Log:
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


Modified: python/trunk/Modules/unicodedata.c
==============================================================================
--- python/trunk/Modules/unicodedata.c	(original)
+++ python/trunk/Modules/unicodedata.c	Sat Aug 12 03:57:47 2006
@@ -1078,6 +1078,7 @@
 {
     Py_UCS4 code;
     Py_UNICODE str[1];
+    char errbuf[256];
 
     char* name;
     int namelen;
@@ -1085,11 +1086,19 @@
         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;
     }
 


More information about the Python-checkins mailing list