Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hi, I just want to confirm if the following scenario is in fact possible:
$a = (localtime)[1]; while (length($a) lt 2) { $a = "0$a"; }
Can this ever, under any circumstances, cause an infinite loop? Is there any circumstance under which Perl might be convinced that it should convert $a to an integer after the assignment, thereby causing the test to always fail?
Yes, I know this is horribly bad code... I didn't write this, but I'm trying to determine if this is the cause of the problem I'm seeing. This is in a CGI script which has been running all night at 100% CPU. Unfortunately I can't reproduce this problem, and the script wasn't running with debugging on, so all I could do was to use gdb to try to find out what happened.
I attached strace to the process, but see nothing: it doesn't seem to be making any syscalls. Attaching gdb to the process (and tediously interrupting/resuming it many times and collating the backtraces), I see that it's making the following calls (in unknown order; there are no debugging symbols so I don't know what order these functions are called in, and I don't know how complete this picture is):
(X -> Y in the above list means that a backtrace shows that function X called function Y at some point. Chained arrows indicate that at some point I saw that nested sequence of calls in the backtrace.)
From the (very) little I know of Perl internals, this seems to correspond with the code fragment listed above. I don't see any other place in the code that has a tight loop involving these operations. There are several places in the script with similar-looking code; I suggested to the writer of the script to replace them with sprintf, but I wanted to check here to make sure that I'm not totally off-base with my guess that these loops are probably the cause of the problem.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Can scalar representation cause infinite loops?
by ikegami (Patriarch) on Jul 19, 2007 at 22:05 UTC | |
by Anonymous Monk on Jul 19, 2007 at 22:20 UTC | |
by Anonymous Monk on Jul 19, 2007 at 22:22 UTC |