diff Python/compile.c @ 48330:ac854cde3135

Merged revisions 64114 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r64114 | gregory.p.smith | 2008-06-11 09:41:16 +0200 (mer., 11 juin 2008) | 6 lines Merge in release25-maint r60793: Added checks for integer overflows, contributed by Google. Some are only available if asserts are left in the code, in cases where they can't be triggered from Python code. ........
author Amaury Forgeot d'Arc <amauryfa@gmail.com>
date Wed, 18 Jun 2008 00:47:36 +0000
parents 212a1fee6bf9
children 85b67fb5672b
line wrap: on
line diff
--- a/Python/compile.c	Tue Jun 17 23:16:28 2008 +0000
+++ b/Python/compile.c	Wed Jun 18 00:47:36 2008 +0000
@@ -227,6 +227,10 @@
 		return ident; /* Don't mangle if class is just underscores */
 	}
 	plen = Py_UNICODE_strlen(p);
+
+	assert(1 <= PY_SSIZE_T_MAX - nlen);
+	assert(1 + nlen <= PY_SSIZE_T_MAX - plen);
+
 	ident = PyUnicode_FromStringAndSize(NULL, 1 + nlen + plen);
 	if (!ident)
 		return 0;
@@ -635,6 +639,12 @@
 		size_t oldsize, newsize;
 		oldsize = b->b_ialloc * sizeof(struct instr);
 		newsize = oldsize << 1;
+
+		if (oldsize > (PY_SIZE_MAX >> 1)) {
+			PyErr_NoMemory();
+			return -1;
+		}
+
 		if (newsize == 0) {
 			PyErr_NoMemory();
 			return -1;
@@ -3711,6 +3721,10 @@
 	a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
 	if (!a->a_lnotab)
 		return 0;
+	if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
+		PyErr_NoMemory();
+		return 0;
+	}
 	a->a_postorder = (basicblock **)PyObject_Malloc(
 					    sizeof(basicblock *) * nblocks);
 	if (!a->a_postorder) {
@@ -3819,10 +3833,14 @@
 		nbytes = a->a_lnotab_off + 2 * ncodes;
 		len = PyBytes_GET_SIZE(a->a_lnotab);
 		if (nbytes >= len) {
-			if (len * 2 < nbytes)
+			if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
 				len = nbytes;
-			else
+			else if (len <= INT_MAX / 2)
 				len *= 2;
+			else {
+				PyErr_NoMemory();
+				return 0;
+			}
 			if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
 				return 0;
 		}
@@ -3841,10 +3859,14 @@
 		nbytes = a->a_lnotab_off + 2 * ncodes;
 		len = PyBytes_GET_SIZE(a->a_lnotab);
 		if (nbytes >= len) {
-			if (len * 2 < nbytes)
+			if ((len <= INT_MAX / 2) && len * 2 < nbytes)
 				len = nbytes;
-			else
+			else if (len <= INT_MAX / 2)
 				len *= 2;
+			else {
+				PyErr_NoMemory();
+				return 0;
+			}
 			if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
 				return 0;
 		}
@@ -3903,6 +3925,8 @@
 	if (i->i_lineno && !assemble_lnotab(a, i))
 		return 0;
 	if (a->a_offset + size >= len) {
+		if (len > PY_SSIZE_T_MAX / 2)
+			return 0;
 		if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
 		    return 0;
 	}