Solved

Detect infinite loops via software

Posted on 2007-11-27
8
989 Views
Last Modified: 2008-03-29
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?
0
Comment
Question by:jeremyduj
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20361382
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.
0
 
LVL 1

Author Comment

by:jeremyduj
ID: 20361498
that was a pretty useless comment.
0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 20363401
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
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 40

Expert Comment

by:evilrix
ID: 20365272
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.

0
 
LVL 1

Author Comment

by:jeremyduj
ID: 20366208
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.
0
 
LVL 22

Accepted Solution

by:
JimBrandley earned 500 total points
ID: 20366825
Can you duplicate the problem running through the debugger? If so, you can just break and check the call stack when you are pretty sure you are in it. Another way to detect it would be to start the app in a profiler (we use Ants from Redgate). After it runs for a time and you have encountered the loop problem, a snapshot will allow you to see the code that's executing more frequently than it should.

Jim
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20367125
>> 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)!

0
 
LVL 7

Expert Comment

by:photowhiz
ID: 20367180
What you are asking for is mathematically impossible: http://en.wikipedia.org/wiki/Halting_problem
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question