I am writing a code quality tool. I am scanning source and compiled classes searching for potential infinite loops.
I can't think a way of a souce code switch statement can loop indefinitely. Am I wrong?
Switch statements compile to lookupswitch
and tableswitch
opcodes. I will need to check compile classes for security reasons, and also bytecode modifications are allow before the quality control program process the compiled classes. Having say that, is there a possible way of looping infinitely by using only those opcodes by modifying a class or generating it with assembler?
I have already taken care of every other branching instructions and statements.
Your help will be really appreciated.
Edit: Conclusion:
As I suspected and by the answers here provided, a switch statement in source code can only branch forward, but any branching instruction in bytecode could potentially jump backwards (assuming bytecode modifications).
Having say that, is there a possible way of looping infinitely by using only those opcodes by modifying a class or generating it with assembler?
To have an infinite loop you have to just backward somewhere. If you modify the byte code this can happen where ever you add or change a jump to go back. If not it can't be a loop, infinite or otherwise.