Reputation: 132
This is a method where I am using the while loop. I strongly suspect that an infinite loop is possible. How to check and eliminate that?
I am using two while loops here. Sould I totally dismantle the while loop?
public class ReferenceListSaveHandler
{
public PublishReferenceListUpdates populateResponse(SearchQueryResponse pSearchQueryResponse,SearchQueryResponse pListResponse, ObjectFactory objFactory) throws IOException, ParserConfigurationException, SAXException,SipException, Exception
{
ReferenceDataProcessor lRefPro = new ReferenceDataProcessor();
PublishReferenceListUpdates lResponse = null;
Record listRecord = null;
ReferenceDataListItemListType lReferenceDataListItemListType = objFactory
.createReferenceDataListItemListType();
ReferenceDataListType lReferenceDataListType = null;
ReferenceDataListItemType lReferenceDataListItemType = null;
boolean ifSynonym = false;
String lRowIdObject = null;
final int lRowIdLength = 14;
if (refListItemItr.hasNext())
{
Record record = (Record)refListItemItr.next();
boolean continueProcessing = true;
boolean lastRecord = false;
while (continueProcessing)
{ // first use of while loop
if (refListItemItr.hasNext() || lastRecord)
{
continueProcessing = true;
lastRecord = false;
}
else
{
continueProcessing = false;
}
if (continueProcessing)
{
lSynonymListType = objFactory
.createSynonymListType();
Field itemLSIDField = record
.getField(ReferenceDataConstants.FIELD_COMPOUND_ASSET_ID);
if (itemLSIDField == null)
{
continue;
}
else
{
currentItemLSID = itemLSIDField
.getStringValue().trim();
}
lReferenceDataListItemType = objFactory
.createReferenceDataListItemType();
lReferenceDataListItemType = setListDetails(record,
lReferenceDataListItemType,
lId, lName, objFactory);
while (refListItemItr.hasNext()
&& continueProcessing)
{ // second use of while loop
SynonymType lSynonymType = null;
if (continueProcessing)
{
if (lSynonymType != null)
lSynonymListType.getSynonym().add(lSynonymType);
}
}
continueProcessing = true;
}
}//while loop
}
}
}
Please help.
Upvotes: 0
Views: 374
Reputation: 7858
Problem number one: refListItemItr
, whatever that is (its definition is not shown), has a .next()
method but it's never called inside the loop.
Problem number two: The second while
loop is always infinite if you enter it:
while (refListItemItr.hasNext()
&& continueProcessing)
{
SynonymType lSynonymType = null;
if (continueProcessing)
{
if (lSynonymType != null)
lSynonymListType.getSynonym().add(lSynonymType);
}
}
Neither refListItemItr.hasNext()
, nor continueProcessing
can change inside this loop. That's why it's an infinite loop.
EDIT: I can't tell you how to change the code because there are many possible ways to change it, depending on what's supposed to happen. You can try this but it may or may not work:
Add this line just before the end of the first loop:
record = (Record)refListItemItr.next(); // ADD THIS LINE
} //while loop BEFORE THIS LINE
Upvotes: 1
Reputation: 23300
I throw in a loop counter whenever I write the word while
, like this:
int loopCounter = 0, maxLoops = 1000; //just an example
while((loopCounter++) < maxLoops && (myConditions))
{
// loop away, this can't ever be infinite
}
or this
int loopCounter = 0, maxLoops = 1000; //just an example
do
{
// loop away, this can't ever be infinite
}
while((loopCounter++) < maxLoops && (myConditions))
Of course, maxLoops
is always sized to be orders of magnitude greater than any reasonable number of iterations... Then I add a check:
if (loopCounter == maxLoops) { throw new InvalidOperationException("Infinite loop detected!"); }
Upvotes: 0
Reputation: 37576
This is an infinit loop , the outer loop depends on lastrecord
which is not beeing set to true anywhere in the program
Upvotes: 0