[Python-bugs-list] [Bug #110615] another unbounded recursion bug

noreply@sourceforge.net noreply@sourceforge.net
Thu, 31 Aug 2000 12:29:58 -0700


Bug #110615, was updated on 2000-Jul-31 14:06
Here is a current snapshot of the bug.

Project: Python
Category: Core
Status: Closed
Resolution: Fixed
Bug Group: None
Priority: 9
Summary: another unbounded recursion bug

Details: Jitterbug-Id: 207
Submitted-By: vincent@ldsol.com
Date: Wed, 16 Feb 2000 10:31:02 -0500 (EST)
Version: 1.5.2
OS: Linux 2.2.x


Get the 2 files
http://www.ldsol.com/~vincent/misc/P.KICORE  (30 kb text data file)
and
http://www.ldsol.com/~vincent/misc/p2sql.py.KICORE (1kb script file)

If I run python p2sql.py.KICORE I get a segfault from the python interpreter.
I spotted the line that seems to cause the segfault (noted in the script).
Not sure if that line is correct, but at the very least I don't expect the
interpreted to abort.
the stack trace obtained from the core file seems to indicate that the
interpreter
has gone in an infinite loop...

Feel free to ask for any other info.

	Cordialement,


====================================================================
Audit trail:
Wed Feb 23 21:28:40 2000	guido	changed notes
Wed Feb 23 21:28:40 2000	guido	moved from incoming to open

Follow-Ups:

Date: 2000-Jul-31 14:06
By: none

Comment:
From: Guido van Rossum <guido@python.org>
Subject: Re: [Python-bugs-list] python dumps core on my script (PR#207)
Date: Wed, 16 Feb 2000 10:57:29 -0500

> Full_Name: vincent renardias
> Version: 1.5.2
> OS: Linux 2.2.x
> Submission from: (NULL) (62.161.210.241)
> 
> Get the 2 files
> http://www.ldsol.com/~vincent/misc/P.KICORE  (30 kb text data file)
> and
> http://www.ldsol.com/~vincent/misc/p2sql.py.KICORE (1kb script file)
> 
> If I run python p2sql.py.KICORE I get a segfault from the python interpreter.
> I spotted the line that seems to cause the segfault (noted in the script).
> Not sure if that line is correct, but at the very least I don't expect the
> interpreted to abort.
> the stack trace obtained from the core file seems to indicate that the
> interpreter
> has gone in an infinite loop...

Thanks for reporting this.

The problem is that the call to "print self" inside the __repr__()
method causes a recursive call to __repr__().  This causes an
unchecked stack overflow in C.

You can avoid this by not calling "print self" inside __repr__().

We'll mark this as an open problem because Python should have given
you a MemoryError as it tries to do with other stack overflows -- but
catching stack overflows is hard.

--Guido van Rossum (home page: http://www.python.org/~guido/)

-------------------------------------------------------

Date: 2000-Jul-31 14:06
By: none

Comment:
From: Vincent Renardias <vincent@ldsol.com>
Subject: Re: [Python-bugs-list] python dumps core on my script (PR#207)
Date: Wed, 16 Feb 2000 16:39:51 +0000 (GMT)


On Wed, 16 Feb 2000, Guido van Rossum wrote:

> The problem is that the call to "print self" inside the __repr__()
> method causes a recursive call to __repr__().  This causes an
> unchecked stack overflow in C.

I guessed that about 3 minutes after sending the bug report... ;)

> You can avoid this by not calling "print self" inside __repr__().
> 
> We'll mark this as an open problem because Python should have given
> you a MemoryError as it tries to do with other stack overflows -- but
> catching stack overflows is hard.

I only found this one inadvertantly, but there are many more potential
problems, ie: print self in __str__, creating an object in __init__, or
even a loop involving (for example) __repr__ calling
__hash__, itself calling __repr__
But I imagine these cases are not only very hard to detect but may also 
be hard to fix in an efficient way...

NB: the script above that shown this bug was my first 'serious' python
program, but I'm not discouraged... ;)

	Cordialement,

-- 
"Si ca sent bon : mange-le, sinon pisse dessus..."  [Proverbe chien]


-------------------------------------------------------

Date: 2000-Jul-31 14:06
By: none

Comment:
From: "Tim Peters" <tim_one@email.msn.com>
Subject: RE: [Python-bugs-list] python dumps core on my script (PR#207)
Date: Wed, 16 Feb 2000 20:36:03 -0500

[vincent@ldsol.com]
> If I run python p2sql.py.KICORE I get a segfault from the python
> interpreter.
> ...
> the stack trace obtained from the core file seems to indicate that the
> interpreter has gone in an infinite loop...

It's really that you've forced the interpreter into infinite recursion.
Thinking about this one should make it clear:

>>> class C:
	def __repr__(self):
		print "got into __repr__"
		return "OK"

>>> c = C()
>>> print c
got into __repr__
OK
>>>

That is, by putting "print self" into your __repr__ method, you force
never-ending recursion, because "print" implicitly invokes __repr__ again to
come up with a string representation for self, which again tries to do
"print self", which again invokes self.__repr__, etc etc.

The solution is to not do this <wink>.  It would be nice if the interpreter
caught this and raised an error, but the code doesn't make sense so you're
going to get a failure of one kind or another no matter what.




-------------------------------------------------------

Date: 2000-Aug-31 12:29
By: jhylton

Comment:
Fixed as best we can in time for 2.0b1 release: Set the recursion limit much lower, and allow the user to bump it back up if she needs to.

-------------------------------------------------------

For detailed info, follow this link:
http://sourceforge.net/bugs/?func=detailbug&bug_id=110615&group_id=5470