diff Parser/node.c @ 24141:40bda3e090f6 legacy-trunk

PyNode_AddChild() and fancy_roundup(): Be paranoid about int overflow.
author Tim Peters <tim.peters@gmail.com>
date Mon, 08 Jul 2002 19:11:07 +0000
parents 76e9a14fb7c5
children fc27ffe2036a
line wrap: on
line diff
--- a/Parser/node.c	Mon Jul 08 14:47:12 2002 +0000
+++ b/Parser/node.c	Mon Jul 08 19:11:07 2002 +0000
@@ -18,15 +18,18 @@
 	return n;
 }
 
-/* See comments at XXXROUNDUP below. */
+/* See comments at XXXROUNDUP below.  Returns -1 on overflow. */
 static int
 fancy_roundup(int n)
 {
 	/* Round up to the closest power of 2 >= n. */
 	int result = 256;
 	assert(n > 128);
-	while (result < n)
+	while (result < n) {
 		result <<= 1;
+		if (result <= 0)
+			return -1;
+	}
 	return result;
 }
 
@@ -62,6 +65,8 @@
 
 	current_capacity = XXXROUNDUP(nch);
 	required_capacity = XXXROUNDUP(nch + 1);
+	if (current_capacity < 0 || required_capacity < 0)
+		return E_OVERFLOW;
 	if (current_capacity < required_capacity) {
 		n = n1->n_child;
 		PyMem_RESIZE(n, node, required_capacity);