Skip to content

Commit

Permalink
PyNode_AddChild() and fancy_roundup(): Be paranoid about int overflow.
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-one committed Jul 8, 2002
1 parent cccd1e7 commit 623fdb9
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Parser/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ PyNode_New(int type)
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;
}

Expand Down Expand Up @@ -62,6 +65,8 @@ PyNode_AddChild(register node *n1, int type, char *str, int lineno)

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);
Expand Down

0 comments on commit 623fdb9

Please sign in to comment.