Link to home
Start Free TrialLog in
Avatar of jeremyduj
jeremyduj

asked on

Detect infinite loops via software

Is there any software product that can detect potential infinite loops in  your code? In other words, something like an editor that I can open up (I use .NET) a .cs file and it would, say, underline any code (or something like that) that could potentially cause infinite loops?
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

there are hundreds of situations where you cannot detect infinite loop until program is running and some variables are set. This is a lazzy and inaccurate approach, better you focus in becoming a good programmer.
Avatar of jeremyduj
jeremyduj

ASKER

that was a pretty useless comment.
Consider the following:
while (!otherThreadComplete)
   sleep(1000);
// Now do something useful.

What happens if something blocks the other thread. This is reasonable code, but is a potential infinite loop. While it would be possible to detect a missing index incrementor in a simple loop, many loops involve variables and methods of other objects.

Because of the complexity of trying to obtain reasonable code coverage, I do not think anyone has made the attempt. I have certainly never seen one.

Jim
Avatar of evilrix
Hmm, well I'm not sure about C# but certainly C++ has a number of tools that can be used at build time to detect and report such possibilities (such as Coverity).

That said, as useless as you think the comment by @jaime_olivares is (I don't!) he does, in fact, raise a perfectly valid point! For example, say you read in a variable from a config file and your loop exit is predicated on a specific value of that config. It is conceivable that your code will enter an infinite loop unchecked. It is possible that a tool may not spot that and, as such, it is better to code defensively against such possibilities.

The way a lot of professional software companies try to mitigate against such situations is to implement code reviews. Basically, the source code is checked into a central review repository (such as CodeStriker) and reviewed by other developers. Comments are actioned and the process repeated until all reviewers are satisfied that the code is fit for purpose.

-Rx.

Well, I literally have about 10 running applications on the web server and I didn't write all the code for every single program, so I'm really not sure where the infinite loop is coming from. There are literally hundreds of thousands of lines of code ... just trying to find a quicker approach than to go through thousands of lines of code looking for a loop! Actually, have you guys tried looking at this program here http://www.iistools.com/en/iisguard.html - it monitors IIS and may be able to pinpoint what page of what program is causing the infinite loop... I'm going to go ahead and install this and perhaps I'll be able to find the culprit! But of course this requires that I await the next infinite loop - and everybody goes crazy at work because the applications are running too slow. I was thinking before that if I could run every .cs file through some kind of editor, that it could pick up potential infinite loops such as something extremely simple as While(1) - even though its simple to fix, you have to pinpoint that one line of code in about 100 .cs files that all have 5000 lines of code or more!  I'll keep looking. Thanks.
ASKER CERTIFIED SOLUTION
Avatar of JimBrandley
JimBrandley
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> something extremely simple as While(1) - even though its simple to fix,
Not necessarily! The body of the while loop may be very complex and contain numerous break and continue clauses (not that I advocate such heinous code)!

What you are asking for is mathematically impossible: http://en.wikipedia.org/wiki/Halting_problem