Can you write a program to check to see if another program performs an infinite loop? There are many ‘good enough’ hacks/solutions out there to address this problem, but the true answer is a simple “no”. There are many proofs out there for this, and I would point you too look up the halting problem for more pointed discourse on this matter. This difficulty in detection is a core issue for a different and more annoying programming problem.
An infinite loop is, fortunately, a relatively obvious thing for a human programmer to spot and what is far more dangerous is a program that is ‘almost’ an infinite loop. In other words, a program that runs for a horribly long time but does actually conclude itself eventually. The most treacherous of these types of programs are problems that are termed NP-Complete.