[Patches] add string precisions to PyErr_Format calls

Greg Stein gstein@lyra.org
Mon, 10 Apr 2000 05:30:29 -0700 (PDT)


Wouldn't it be best to simply fix PyErr_Format so that we don't have to
continue to worry about buffer overruns?

Cheers,
-g

On Mon, 10 Apr 2000, Skip Montanaro wrote:

> 
>     Skip> [ATTACHMENT ~/src/python/dist/src/Objects/object.c.diff, text/plain]
>     Skip> [ATTACHMENT ~/src/python/dist/src/Objects/unicodeobject.c.diff, text/plain]
>     Skip> [ATTACHMENT ~/src/python/dist/src/Modules/pyexpat.c.diff, text/plain]
>     Skip> [ATTACHMENT ~/src/python/dist/src/Python/dynload_next.c.diff, text/plain]
>     Skip> [ATTACHMENT ~/src/python/dist/src/Python/ceval.c.diff, text/plain]
> 
> Ack!  The attachments didn't attach!  Here they are as plain text appended
> one after the other...
> 
> Skip
> 
> *** /tmp/object.c.~2.65~	Mon Apr 10 06:01:08 2000
> --- /tmp/object.c	Mon Apr 10 06:01:08 2000
> ***************
> *** 236,242 ****
>   			return NULL;
>   		if (!PyString_Check(res)) {
>   			PyErr_Format(PyExc_TypeError,
> ! 				     "__repr__ returned non-string (type %s)",
>   				     res->ob_type->tp_name);
>   			Py_DECREF(res);
>   			return NULL;
> --- 236,242 ----
>   			return NULL;
>   		if (!PyString_Check(res)) {
>   			PyErr_Format(PyExc_TypeError,
> ! 				     "__repr__ returned non-string (type %.200s)",
>   				     res->ob_type->tp_name);
>   			Py_DECREF(res);
>   			return NULL;
> ***************
> *** 273,279 ****
>   		return NULL;
>   	if (!PyString_Check(res)) {
>   		PyErr_Format(PyExc_TypeError,
> ! 			     "__str__ returned non-string (type %s)",
>   			     res->ob_type->tp_name);
>   		Py_DECREF(res);
>   		return NULL;
> --- 273,279 ----
>   		return NULL;
>   	if (!PyString_Check(res)) {
>   		PyErr_Format(PyExc_TypeError,
> ! 			     "__str__ returned non-string (type %.200s)",
>   			     res->ob_type->tp_name);
>   		Py_DECREF(res);
>   		return NULL;
> 
> *** /tmp/unicodeobject.c.~2.8~	Mon Apr 10 06:04:11 2000
> --- /tmp/unicodeobject.c	Mon Apr 10 06:04:11 2000
> ***************
> *** 365,371 ****
>           goto onError;
>       if (!PyUnicode_Check(unicode)) {
>           PyErr_Format(PyExc_TypeError,
> !                      "decoder did not return an unicode object (type=%s)",
>                        unicode->ob_type->tp_name);
>           Py_DECREF(unicode);
>           goto onError;
> --- 365,371 ----
>           goto onError;
>       if (!PyUnicode_Check(unicode)) {
>           PyErr_Format(PyExc_TypeError,
> !                      "decoder did not return an unicode object (type=%.400s)",
>                        unicode->ob_type->tp_name);
>           Py_DECREF(unicode);
>           goto onError;
> ***************
> *** 416,422 ****
>       /* XXX Should we really enforce this ? */
>       if (!PyString_Check(v)) {
>           PyErr_Format(PyExc_TypeError,
> !                      "encoder did not return a string object (type=%s)",
>                        v->ob_type->tp_name);
>           Py_DECREF(v);
>           goto onError;
> --- 416,422 ----
>       /* XXX Should we really enforce this ? */
>       if (!PyString_Check(v)) {
>           PyErr_Format(PyExc_TypeError,
> !                      "encoder did not return a string object (type=%.400s)",
>                        v->ob_type->tp_name);
>           Py_DECREF(v);
>           goto onError;
> ***************
> *** 484,490 ****
>       if ((errors == NULL) ||
>           (strcmp(errors,"strict") == 0)) {
>           PyErr_Format(PyExc_UnicodeError,
> !                      "UTF-8 decoding error: %s",
>                        details);
>           return -1;
>       }
> --- 484,490 ----
>       if ((errors == NULL) ||
>           (strcmp(errors,"strict") == 0)) {
>           PyErr_Format(PyExc_UnicodeError,
> !                      "UTF-8 decoding error: %.400s",
>                        details);
>           return -1;
>       }
> ***************
> *** 500,506 ****
>       }
>       else {
>           PyErr_Format(PyExc_ValueError,
> !                      "UTF-8 decoding error; unknown error handling code: %s",
>                        errors);
>           return -1;
>       }
> --- 500,506 ----
>       }
>       else {
>           PyErr_Format(PyExc_ValueError,
> !                      "UTF-8 decoding error; unknown error handling code: %.400s",
>                        errors);
>           return -1;
>       }
> ***************
> *** 607,613 ****
>       if ((errors == NULL) ||
>   	(strcmp(errors,"strict") == 0)) {
>   	PyErr_Format(PyExc_UnicodeError,
> ! 		     "UTF-8 encoding error: %s",
>   		     details);
>   	return -1;
>       }
> --- 607,613 ----
>       if ((errors == NULL) ||
>   	(strcmp(errors,"strict") == 0)) {
>   	PyErr_Format(PyExc_UnicodeError,
> ! 		     "UTF-8 encoding error: %.400s",
>   		     details);
>   	return -1;
>       }
> ***************
> *** 622,628 ****
>       else {
>   	PyErr_Format(PyExc_ValueError,
>   		     "UTF-8 encoding error; "
> ! 		     "unknown error handling code: %s",
>   		     errors);
>   	return -1;
>       }
> --- 622,628 ----
>       else {
>   	PyErr_Format(PyExc_ValueError,
>   		     "UTF-8 encoding error; "
> ! 		     "unknown error handling code: %.400s",
>   		     errors);
>   	return -1;
>       }
> ***************
> *** 728,734 ****
>       if ((errors == NULL) ||
>           (strcmp(errors,"strict") == 0)) {
>           PyErr_Format(PyExc_UnicodeError,
> !                      "UTF-16 decoding error: %s",
>                        details);
>           return -1;
>       }
> --- 728,734 ----
>       if ((errors == NULL) ||
>           (strcmp(errors,"strict") == 0)) {
>           PyErr_Format(PyExc_UnicodeError,
> !                      "UTF-16 decoding error: %.400s",
>                        details);
>           return -1;
>       }
> ***************
> *** 744,750 ****
>       }
>       else {
>           PyErr_Format(PyExc_ValueError,
> !                      "UTF-16 decoding error; unknown error handling code: %s",
>                        errors);
>           return -1;
>       }
> --- 744,750 ----
>       }
>       else {
>           PyErr_Format(PyExc_ValueError,
> !                      "UTF-16 decoding error; unknown error handling code: %.400s",
>                        errors);
>           return -1;
>       }
> ***************
> *** 918,924 ****
>       if ((errors == NULL) ||
>           (strcmp(errors,"strict") == 0)) {
>           PyErr_Format(PyExc_UnicodeError,
> !                      "Unicode-Escape decoding error: %s",
>                        details);
>           return -1;
>       }
> --- 918,924 ----
>       if ((errors == NULL) ||
>           (strcmp(errors,"strict") == 0)) {
>           PyErr_Format(PyExc_UnicodeError,
> !                      "Unicode-Escape decoding error: %.400s",
>                        details);
>           return -1;
>       }
> ***************
> *** 932,938 ****
>       else {
>           PyErr_Format(PyExc_ValueError,
>                        "Unicode-Escape decoding error; "
> !                      "unknown error handling code: %s",
>                        errors);
>           return -1;
>       }
> --- 932,938 ----
>       else {
>           PyErr_Format(PyExc_ValueError,
>                        "Unicode-Escape decoding error; "
> !                      "unknown error handling code: %.400s",
>                        errors);
>           return -1;
>       }
> ***************
> *** 1296,1302 ****
>       if ((errors == NULL) ||
>   	(strcmp(errors,"strict") == 0)) {
>   	PyErr_Format(PyExc_UnicodeError,
> ! 		     "Latin-1 encoding error: %s",
>   		     details);
>   	return -1;
>       }
> --- 1296,1302 ----
>       if ((errors == NULL) ||
>   	(strcmp(errors,"strict") == 0)) {
>   	PyErr_Format(PyExc_UnicodeError,
> ! 		     "Latin-1 encoding error: %.400s",
>   		     details);
>   	return -1;
>       }
> ***************
> *** 1310,1316 ****
>       else {
>   	PyErr_Format(PyExc_ValueError,
>   		     "Latin-1 encoding error; "
> ! 		     "unknown error handling code: %s",
>   		     errors);
>   	return -1;
>       }
> --- 1310,1316 ----
>       else {
>   	PyErr_Format(PyExc_ValueError,
>   		     "Latin-1 encoding error; "
> ! 		     "unknown error handling code: %.400s",
>   		     errors);
>   	return -1;
>       }
> ***************
> *** 1366,1372 ****
>       if ((errors == NULL) ||
>   	(strcmp(errors,"strict") == 0)) {
>   	PyErr_Format(PyExc_UnicodeError,
> ! 		     "ASCII decoding error: %s",
>   		     details);
>   	return -1;
>       }
> --- 1366,1372 ----
>       if ((errors == NULL) ||
>   	(strcmp(errors,"strict") == 0)) {
>   	PyErr_Format(PyExc_UnicodeError,
> ! 		     "ASCII decoding error: %.400s",
>   		     details);
>   	return -1;
>       }
> ***************
> *** 1381,1387 ****
>       else {
>   	PyErr_Format(PyExc_ValueError,
>   		     "ASCII decoding error; "
> ! 		     "unknown error handling code: %s",
>   		     errors);
>   	return -1;
>       }
> --- 1381,1387 ----
>       else {
>   	PyErr_Format(PyExc_ValueError,
>   		     "ASCII decoding error; "
> ! 		     "unknown error handling code: %.400s",
>   		     errors);
>   	return -1;
>       }
> ***************
> *** 1429,1435 ****
>       if ((errors == NULL) ||
>   	(strcmp(errors,"strict") == 0)) {
>   	PyErr_Format(PyExc_UnicodeError,
> ! 		     "ASCII encoding error: %s",
>   		     details);
>   	return -1;
>       }
> --- 1429,1435 ----
>       if ((errors == NULL) ||
>   	(strcmp(errors,"strict") == 0)) {
>   	PyErr_Format(PyExc_UnicodeError,
> ! 		     "ASCII encoding error: %.400s",
>   		     details);
>   	return -1;
>       }
> ***************
> *** 1443,1449 ****
>       else {
>   	PyErr_Format(PyExc_ValueError,
>   		     "ASCII encoding error; "
> ! 		     "unknown error handling code: %s",
>   		     errors);
>   	return -1;
>       }
> --- 1443,1449 ----
>       else {
>   	PyErr_Format(PyExc_ValueError,
>   		     "ASCII encoding error; "
> ! 		     "unknown error handling code: %.400s",
>   		     errors);
>   	return -1;
>       }
> ***************
> *** 1558,1564 ****
>       if ((errors == NULL) ||
>   	(strcmp(errors,"strict") == 0)) {
>   	PyErr_Format(PyExc_UnicodeError,
> ! 		     "charmap decoding error: %s",
>   		     details);
>   	return -1;
>       }
> --- 1558,1564 ----
>       if ((errors == NULL) ||
>   	(strcmp(errors,"strict") == 0)) {
>   	PyErr_Format(PyExc_UnicodeError,
> ! 		     "charmap decoding error: %.400s",
>   		     details);
>   	return -1;
>       }
> ***************
> *** 1573,1579 ****
>       else {
>   	PyErr_Format(PyExc_ValueError,
>   		     "charmap decoding error; "
> ! 		     "unknown error handling code: %s",
>   		     errors);
>   	return -1;
>       }
> --- 1573,1579 ----
>       else {
>   	PyErr_Format(PyExc_ValueError,
>   		     "charmap decoding error; "
> ! 		     "unknown error handling code: %.400s",
>   		     errors);
>   	return -1;
>       }
> ***************
> *** 1674,1680 ****
>       if ((errors == NULL) ||
>   	(strcmp(errors,"strict") == 0)) {
>   	PyErr_Format(PyExc_UnicodeError,
> ! 		     "charmap encoding error: %s",
>   		     details);
>   	return -1;
>       }
> --- 1674,1680 ----
>       if ((errors == NULL) ||
>   	(strcmp(errors,"strict") == 0)) {
>   	PyErr_Format(PyExc_UnicodeError,
> ! 		     "charmap encoding error: %.400s",
>   		     details);
>   	return -1;
>       }
> ***************
> *** 1689,1695 ****
>       else {
>   	PyErr_Format(PyExc_ValueError,
>   		     "charmap encoding error; "
> ! 		     "unknown error handling code: %s",
>   		     errors);
>   	return -1;
>       }
> --- 1689,1695 ----
>       else {
>   	PyErr_Format(PyExc_ValueError,
>   		     "charmap encoding error; "
> ! 		     "unknown error handling code: %.400s",
>   		     errors);
>   	return -1;
>       }
> ***************
> *** 1806,1812 ****
>       if ((errors == NULL) ||
>   	(strcmp(errors,"strict") == 0)) {
>   	PyErr_Format(PyExc_UnicodeError,
> ! 		     "translate error: %s",
>   		     details);
>   	return -1;
>       }
> --- 1806,1812 ----
>       if ((errors == NULL) ||
>   	(strcmp(errors,"strict") == 0)) {
>   	PyErr_Format(PyExc_UnicodeError,
> ! 		     "translate error: %.400s",
>   		     details);
>   	return -1;
>       }
> ***************
> *** 1821,1827 ****
>       else {
>   	PyErr_Format(PyExc_ValueError,
>   		     "translate error; "
> ! 		     "unknown error handling code: %s",
>   		     errors);
>   	return -1;
>       }
> --- 1821,1827 ----
>       else {
>   	PyErr_Format(PyExc_ValueError,
>   		     "translate error; "
> ! 		     "unknown error handling code: %.400s",
>   		     errors);
>   	return -1;
>       }
> 
> *** /tmp/pyexpat.c.~2.2~	Mon Apr 10 06:01:33 2000
> --- /tmp/pyexpat.c	Mon Apr 10 06:01:33 2000
> ***************
> *** 307,313 ****
>                   return NULL;
>           }
>           else if (rv == 0) {
> !                 PyErr_Format(ErrorObject, "%s: line %i, column %i",
>                                XML_ErrorString( XML_GetErrorCode(self->itself) ),
>                                XML_GetErrorLineNumber(self->itself),
>                                XML_GetErrorColumnNumber(self->itself) );
> --- 307,313 ----
>                   return NULL;
>           }
>           else if (rv == 0) {
> !                 PyErr_Format(ErrorObject, "%.200s: line %i, column %i",
>                                XML_ErrorString( XML_GetErrorCode(self->itself) ),
>                                XML_GetErrorLineNumber(self->itself),
>                                XML_GetErrorColumnNumber(self->itself) );
> 
> *** /tmp/dynload_next.c.~2.2~-YCj-2	Mon Apr 10 06:00:09 2000
> --- /tmp/dynload_next.c-YCVIG	Mon Apr 10 06:00:09 2000
> ***************
> *** 185,191 ****
>   		if (!NSIsSymbolNameDefined(funcname)) {
>   			/* UnlinkModule() isn't implimented in current versions, but calling it does no harm */
>   			NSUnLinkModule(newModule, FALSE);
> ! 			PyErr_Format(PyExc_ImportError, "Loaded module does not contain symbol %s", funcname);
>   			return NULL;
>   		}
>   		theSym = NSLookupAndBindSymbol(funcname);
> --- 185,193 ----
>   		if (!NSIsSymbolNameDefined(funcname)) {
>   			/* UnlinkModule() isn't implimented in current versions, but calling it does no harm */
>   			NSUnLinkModule(newModule, FALSE);
> ! 			PyErr_Format(PyExc_ImportError,
> ! 				     "Loaded module does not contain symbol %.200s",
> ! 				     funcname);
>   			return NULL;
>   		}
>   		theSym = NSLookupAndBindSymbol(funcname);
> 
> *** /tmp/ceval.c.~2.174~-YCW7k	Mon Apr 10 06:00:41 2000
> --- /tmp/ceval.c-YCjFr	Mon Apr 10 06:00:41 2000
> ***************
> *** 2513,2519 ****
>   	else {
>   		if (!PyFunction_Check(func)) {
>   			PyErr_Format(PyExc_TypeError,
> ! 				     "call of non-function (type %s)",
>   				     func->ob_type->tp_name);
>   			return NULL;
>   		}
> --- 2513,2519 ----
>   	else {
>   		if (!PyFunction_Check(func)) {
>   			PyErr_Format(PyExc_TypeError,
> ! 				     "call of non-function (type %.200s)",
>   				     func->ob_type->tp_name);
>   			return NULL;
>   		}
> 
> _______________________________________________
> Patches mailing list
> Patches@python.org
> http://www.python.org/mailman/listinfo/patches
> 

-- 
Greg Stein, http://www.lyra.org/