Problem 854

Summary: infinte loop in G4RadioactiveDecay.cc line 687
Product: Geant4 Reporter: jasondet
Component: processes/hadronic/modelsAssignee: dennis.herbert.wright
Status: RESOLVED FIXED    
Severity: normal CC: mmarino
Priority: P2    
Version: other   
Hardware: All   
OS: All   

Description jasondet 2006-03-28 13:43:22 CEST
We have traced an infinite loop problem to line 687 in G4RadioactiveDecay.cc.
The infinte loop occurs, for example, when DecaySchemeFile corresponds to
z81.a188. This bug is probably not related to bug 843, as the infinite loop does
not occur for z81.a194. Here is some test code that exposes the bug (replace the
path to the data file with the appropriate path for your system):

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
  ifstream f("/usr/local/geant4/geant4.8.0/data/RadiativeDecay/z81.a188");
  if(!f) {
    cout << "no file" << endl;
    return 1;
  }
  char inputChars[80]={' '};
  while (-f.getline(inputChars, 80).eof() != EOF) {
    cout << inputChars << endl;
  }
  return 0;
}

The problem is with the test for EOF, as noted at
http://mathbits.com/MathBits/CompSci/Files/End.htm

As stated on this webpage, the recommended fix is to replace the test for EOF
with just a boolean test on getline(), i.e.

while(f.getline(inputChars, 80)) { ... }

This solves the infinite loop in the sample code here. We recommend the same
modification be made to line 687 in G4RadioactiveDecay.cc, so that it reads

  while (!complete && DecaySchemeFile.getline(inputChars, 80))

Thanks,
Jason Detwiler and Mike Marino
Comment 1 mmarino 2006-03-29 19:16:59 CEST
There is also similar code within G4IsotopTable.cc:line 201 which we recommend
should be changed in the same manner:

while (!found && -DecaySchemeFile.getline(inputChars, 80).eof() != EOF)

should become

while (!found && DecaySchemeFile.getline(inputChars, 80))

Best, Mike M and Jason D
Comment 2 dennis.herbert.wright 2006-04-10 16:45:59 CEST
Thanks for finding this.  However, I tried your fix and found that the output
was missing the last five lines of the file.   Looking at my copy of
RadiativeDecay3.0, I see that there is a problem with the z81.a188 file:
there is a missing carriage return at the end of the line

MshellEC    2680.90000  8.311E-03  3119.10000

near the bottom of the file.   When I insert a carriage return at this point,
the file is read correctly without truncation and without a code change.
I will report this to the people responsible for maintaining the file, but in
the meantime you can edit your copy.