Python segfault

Thomas Wouters thomas at xs4all.net
Fri Jun 16 08:58:37 EDT 2000


On Fri, Jun 16, 2000 at 02:19:03PM +0200, Hans-Joachim Widmaier wrote:
> Thomas Wouters wrote:

> > The problem is not the 'height' of this graph, or the total number of
> > 'nodes' (which is limited only by memory), but the 'width' of the first
> > tree. The 'node' struct is defined like this:
> > 
> > typedef struct _node { 
> > short n_type; 
> > char *n_str; 
> > short n_lineno; 
> > short n_nchildren; 
> > struct _node *n_child; 
> > } node; 
> > 
> > and the 'n_nchildren' member is the number of immediate subexpressions
> > (not grandchildren, only children) -- and it is limited to a short,
> > which is 16 bits on most systems. And the short is signed, which means
> > the maximum number of children is 32767. This includes the operators
> > itself, so you can operate on just 16384 items in a single expression.
> > If you try to operate on more, the short wraps, and the parser tries to
> > index node->n_child[-32768], which usually segfaults.

> Why not simply make 'n_nchildren' an (32 bit) int? And, while at it,
> 'n_lineno' also? It would cost some memory, though.

Why bother ? It would do nothing to solve the real problem, which is that
there is a finite number of direct children to a node. The current number is
practically unreachable -- the example case of ('+'.join(['2+2'] * 8193)) is
hardly reminiscent of real code.

Fixing the parser so it doesn't crash, and instead says 'add a few
parentheses' would be the better solution, IMHO ;)

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list