Wrote the right article about checking the Geant4 project. Let me remind you the background. Recently, the old version of the Geant4 library was tested and the article "
Copy-Paste and Muons " was written. Why was the old version checked? People are not perfect. The essence of the mistake can be found in the previous article. Now, however, we offer you a brief report on the verification of Geant4 version 10.0-beta.
Summary of the previous article
The article “Copy-Paste and Muons” describes the benefits of using the
static analysis methodology and the capabilities of
PVS-Studio . The bearded project Geant4 version 4.9.4 was tested. Many suspicious code fragments were found, which were described in the article.
')
Geant4 is a program for simulating the passage of elementary particles through matter using Monte Carlo methods. Developed at CERN in the object-oriented programming language C ++. It is a further development of previous versions of GEANT, substantially reworked and expanded.
Project website:
http://geant4.org .
In the previous article, 16 highly suspicious code fragments were described. New check revealed only 10 of them. The rest are either visible or fixed, or this code no longer exists. I will not re-describe these code fragments in a new article. They can be seen in the previous article. It indicates whether the analyzer warning disappeared or not in the new version.
I apologize for such a strange research format. But I hope this does not hurt to correct a number of flaws in the Geant4 project and to draw people's attention to the PVS-Studio tool.
If I'm not mistaken, the previous version of the library refers to 2011. During this time, much has changed, and it is not surprising that some new strange places have been found in the code. Let's see what is new or what I did not notice earlier.
New suspicious code snippets
A complete list of suspicious places that I noticed is in the
geant4_new.txt file. I want to emphasize that you should not rely solely on this list. It is much better to check the project yourself and analyze all the warnings. We are ready to provide a key to the developers of Geant4 for this:
a feedback form .
Same functions
G4double G4CsvAnalysisManager::GetH2Xmin(G4int ) const { ExceptionForHistograms("GetH2Xmin"); return 0; } G4double G4CsvAnalysisManager::GetH2Xmax(G4int ) const { ExceptionForHistograms("GetH2Xmin"); return 0; }
PVS-Studio warning: V524 It is odd that the body of the GetH2Xmax function is fully equivalent to the body of the GetH2Xmin function. _G4analysis-archive g4csvanalalysismanager.cc 933
The GetH2Xmax () function, most likely, should call ExceptionForHistograms () with a different parameter:
ExceptionForHistograms("GetH2Xmax");
The error does not look serious. As I understand it, this is an exception handling. Nevertheless, I decided to mention this Copy-Paste error.
Energy is zero
The functions CalculateTotalEnergy () summarizes the values ​​in the 'Etot' variable. Suddenly, this variable is reset. This is very suspicious.
G4double G4RKFieldIntegrator::CalculateTotalEnergy(const G4KineticTrackVector& Barions) { G4double Etot = 0; .... for(G4int c2 = c1 + 1; c2 < nBarion; c2++) { ....
PVS-Studio warning: V519 The 'Etot' variable is assigned values ​​twice successively. Perhaps this is a mistake. Check lines: 80, 83. _G4processes-archive g4rkfieldintegrator.cc 83
Suspicious logic
G4double G4EmBiasingManager::ApplySecondaryBiasing(....) { .... if(0 == nsplit) { if(safety > fSafetyMin) .... } if(1 == nsplit) { weight = ApplyRussianRoulette(vd, index); } else { G4double tmpEnergy = pPartChange->GetProposedKineticEnergy(); G4ThreeVector tmpMomDir = .... weight = ApplySplitting(vd, track, currentModel, index, tcut); pPartChange->SetProposedKineticEnergy(tmpEnergy); pPartChange->ProposeMomentumDirection(tmpMomDir); } .... }
PVS-Studio warning: V646 Consider inspecting the application's logic. It's possible that 'else' keyword is missing. _G4processes-archive g4embiasingmanager.cc 299
The code is designed as if the “else if” construct is used. But it is not. If we arrange the code correctly, we get this:
if(0 == nsplit) { if(safety > fSafetyMin) .... } if(1 == nsplit) { weight = ApplyRussianRoulette(vd, index); } else { G4double tmpEnergy = pPartChange->GetProposedKineticEnergy(); G4ThreeVector tmpMomDir = .... weight = ApplySplitting(vd, track, currentModel, index, tcut); pPartChange->SetProposedKineticEnergy(tmpEnergy); pPartChange->ProposeMomentumDirection(tmpMomDir); }
Note that the block related to the 'else' branch is always executed if "1! = Nsplit". There is a suspicion that a different logic of work was conceived.
A similar situation can be seen here:
V646 Consider inspecting the application's logic. It's possible that 'else' keyword is missing. _G4processes-archive g4embiasingmanager.cc 347
Unfinished code?
void G4MolecularDecayTable::AddExcitedState(const G4String& label) { channelsMap::iterator channelsIter = fDecayChannelsMap.find(label); if(channelsIter != fDecayChannelsMap.end()) { G4String errMsg = "Excited state" + label + " already registered in the decay table."; G4Exception("G4MolecularDecayTable::AddExcitedState", "G4MolecularDecayTable003", FatalErrorInArgument, errMsg); return; } fDecayChannelsMap[label] ; }
PVS-Studio warning: V607 Ownerless expression 'fDecayChannelsMap [label]'. _G4processes-archive g4moleculardecaytable.cc 140
The end of the function looks very suspicious:
fDecayChannelsMap[label] ;
What is it? Is something missing here? What did you want to do with the cell array?
Kinematics
The following example is quite long. Unfortunately, it is difficult for me to cut it even more. See what values ​​the variable 'id' can take.
void G4QMDCollision::CalKinematicsOfBinaryCollisions( G4double dt) { .... G4int id = 0; .... if ( secs ) { .... id++; .... } if ( std::abs ( eini - efin ) < fepse*10 ) .... else { .... for ( G4int i0i = 0 ; i0i < id-1 ; i0i++ ) { theSystem->DeleteParticipant( i0i+n0 ); } .... } .... }
PVS-Studio warning: V621 Consider inspecting the 'for' operator. It is possible that you will not be executed at all. _G4processes-archive g4qmdcollision.cc 228
If the condition "if (secs)" fails, the variable 'id' will remain zero. Then, a loop of the form may occur:
for ( G4int i0i = 0 ; i0i < -1 ; i0i++ )
It will be a very strange cycle. I think it is worth checking the logic of the function CalKinematicsOfBinaryCollisions ().
Other
There are a number of warnings that I did not describe in the last article. I will not be in this. For example, I will show one case:
class G4HadronicException : public std::exception { .... }; inline G4double G4GeneralPhaseSpaceDecay::Pmx( G4double e, G4double p1, G4double p2) { if (e-p1-p2 < 0 ) { G4HadronicException(__FILE__, __LINE__, "G4GeneralPhaseSpaceDecay::Pmx " "energy in cms > mass1+mass2"); } .... }
PVS-Studio warning. V596 The object was not used. The 'throw' keyword could be missing: throw G4HadronicException (FOO); _G4processes-archive g4generalphasespacedecay.hh 116
There are several errors when the 'throw' statement is forgotten. As a result, an object having the type 'G4HadronicException' is created and immediately destroyed. And then the program continues to work with incorrect data.
These are the typos that are not described in the article, can be found in the file
geant4_new.txt . There are also warnings related to micro-optimizations.
Conclusion
On the pointlessness of checking the old project. You see, and I cut it. :)
A good reason to potrolit.