SlideShare a Scribd company logo
Going On with the Check of Geant4 
Author: Andrey Karpov 
Date: 14.11.2013 
This is the correct article about the results of checking the Geant4 project, which I have written after the 
previous incorrect one. Let me remind you the whole story. I have recently checked an old version of the 
Geant4 library and reported the results in the article "Copy-Paste and Muons". Why old version? Nobody is 
perfect, and we finally made a mistake ourselves. To find out which exactly, see the previous article. This 
time I offer you a brief report about checking Geant4 of the version 10.0-beta. 
Summary of the previous article 
In the article "Copy-Paste and muons" I was reasoning about how useful the static analysis methodology is 
and about the PVS-Studio analyzer's diagnostic abilities. I checked an ancient version of the Geant4 project 
(version 4.9.4), found a number of supposedly incorrect code fragments and described them in the article. 
Geant4 (for GEometry ANd Tracking) is a platform for "the simulation of the passage of particles through 
matter," using Monte Carlo methods. It is the successor of the GEANT series of software toolkits developed 
by CERN, and the first to use object oriented programming (in C++). Its development, maintenance and user 
support are taken care by the international Geant4 Collaboration. Application areas include high energy 
physics and nuclear experiments, medical, accelerator and space physics studies. The software is used by a 
number of research projects around the world. 
The project website: http://geant4.org. 
In the previous article, I described at least 16 suspicious code fragments. The recent check of the new 
version has revealed only 10 of those. The rest are either fixed or discarded together with the pieces of 
code. I won't discuss those defects once again in this article; if you want to see them, please refer to the 
previous article (there are comments below each sample telling if a particular bug is fixed in the new version 
or not.) 
Sorry for such a strange format of my investigation, but I hope that it won't in any way prevent the 
developers from fixing some defects in the Geant4 project and that the PVS-Studio will attract their 
attention. 
If I'm correct, the previous library version dates back to 2011. Much has changed since then, and it's no 
wonder that some new strange code fragments were found. Let's see if there is anything new or anything I 
missed during the previous check. 
New suspicious code fragments 
A complete list of all the suspicious fragments that caught my eye is saved in the file geant4_new.txt. But 
please don't rely solely on this list; the developers should check the project themselves and study all the
warnings. We can grant a free registration key to the Geant4 developers for some time so that they could 
check their code: see the feedback page. 
Identical functions 
G4double G4CsvAnalysisManager::GetH2Xmin(G4int /*id*/) const 
{ 
ExceptionForHistograms("GetH2Xmin"); 
return 0; 
} 
G4double G4CsvAnalysisManager::GetH2Xmax(G4int /*id*/) const 
{ 
ExceptionForHistograms("GetH2Xmin"); 
return 0; 
} 
PVS-Studio's diagnostic message: V524 It is odd that the body of 'GetH2Xmax' function is fully equivalent to 
the body of 'GetH2Xmin' function. _G4analysis-archive g4csvanalysismanager.cc 933 
The GetH2Xmax() function should probably call the ExceptionForHistograms() function with a different 
parameter: 
ExceptionForHistograms("GetH2Xmax"); 
This bug doesn't look serious. As far as I understand, this is a construct to handle an exception. However, I 
decided to mention this Copy-Paste bug anyway. 
Zero energy 
The function CalculateTotalEnergy () sums up values in the 'Etot' variable which suddenly gets zeroed. That's 
a very strange thing. 
G4double G4RKFieldIntegrator::CalculateTotalEnergy(const 
G4KineticTrackVector& Barions) 
{ 
G4double Etot = 0; 
.... 
for(G4int c2 = c1 + 1; c2 < nBarion; c2++)
{ 
.... 
// Esk2 
Etot += t1*std::pow(Alpha/pi, 3/2)* 
std::exp(-Alpha*r12*r12); 
// Eyuk 
Etot += ....; 
// Ecoul 
Etot += 1.44*p1->GetDefinition()->GetPDGCharge()* 
p2->GetDefinition()->GetPDGCharge()/r12* 
Erf(std::sqrt(Alpha)*r12); 
// Epaul 
Etot = 0; 
.... 
} 
.... 
} 
PVS-Studio's diagnostic message: V519 The 'Etot' variable is assigned values twice successively. Perhaps this 
is a mistake. Check lines: 80, 83. _G4processes-archive g4rkfieldintegrator.cc 83 
Strange 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's diagnostic message: V646 Consider inspecting the application's logic. It's possible that 'else' 
keyword is missing. _G4processes-archive g4embiasingmanager.cc 299 
The code formatting suggests that the programmer was using the "else if" construct. But I can't see any 
here. If we format the code properly, we'll get the following: 
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); 
}
Notice that the block referring to the 'else' branch is executed every time whenever the "1 != nsplit" 
condition is true. It suspect that the programmer wanted the program logic to be somewhat different. 
A similar issue can be found in the following fragment: 
V646 Consider inspecting the application's logic. It's possible that 'else' keyword is missing. _G4processes-archive 
g4embiasingmanager.cc 347 
Incomplete 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's diagnostic message: V607 Ownerless expression 'fDecayChannelsMap[label]'. _G4processes-archive 
g4moleculardecaytable.cc 140 
The end of the function is very strange: 
fDecayChannelsMap[label] ; 
What's it? Something is missing? What did the programmer intend to do with an array cell?
Kinematics 
The next sample is rather lengthy. I shortened it as much as I could, but unfortunately it is still pretty large. 
Read it and note the values the 'id' variable takes. 
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's diagnostic message: V621 Consider inspecting the 'for' operator. It's possible that the loop will 
be executed incorrectly or won't be executed at all. _G4processes-archive g4qmdcollision.cc 228 
If the "if ( secs )" condition is false, the variable 'id' will remain equal to zero. In this case we might get the 
following loop: 
for ( G4int i0i = 0 ; i0i < -1 ; i0i++ ) 
And that will be a very strange loop indeed. I guess something is wrong with the 
CalKinematicsOfBinaryCollisions() function's logic. 
Miscellaneous 
There are a few more warnings I didn't describe in the previous article. Neither will I in this one. Just one 
sample: 
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's diagnostic message. V596 The object was created but it is not being used. The 'throw' keyword 
could be missing: throw G4HadronicException(FOO); _G4processes-archive g4generalphasespacedecay.hh 
116
There are some mistakes with missing 'throw' operators. They result in an object of the 
'G4HadronicException' type being created and at once destroyed, the program going on to work with 
incorrect data. 
For other examples of such typos, see the file geant4_new.txt. There you will also find some warnings 
related to microoptimizations. 
Conclusion 
Me checking obsolete code is a nice story, huh? Yeah, I've finally slipped up myself. :) 
A good opportunity to troll me, isn't it?

More Related Content

What's hot (20)

PDF
Date Processing Attracts Bugs or 77 Defects in Qt 6
Andrey Karpov
 
PDF
Dusting the globe: analysis of NASA World Wind project
PVS-Studio
 
PDF
CppCat Static Analyzer Review
Andrey Karpov
 
PDF
Checking VirtualDub
Andrey Karpov
 
PDF
A Spin-off: CryEngine 3 SDK Checked with CppCat
Andrey Karpov
 
PDF
Linux version of PVS-Studio couldn't help checking CodeLite
PVS-Studio
 
PDF
Checking Bitcoin
Andrey Karpov
 
PDF
CppCat Checks OpenMW: Not All is Fine in the Morrowind Universe
Andrey Karpov
 
PDF
Analysis of the Trans-Proteomic Pipeline (TPP) project
PVS-Studio
 
PDF
Checking Notepad++: five years later
PVS-Studio
 
PDF
The CppCat Analyzer Checks TortoiseGit
Andrey Karpov
 
PPTX
Дмитрий Демчук. Кроссплатформенный краш-репорт
Sergey Platonov
 
PDF
Clang tidy
Yury Yafimachau
 
PDF
Checking the Cross-Platform Framework Cocos2d-x
Andrey Karpov
 
PPTX
Qunit Java script Un
akanksha arora
 
PDF
TensorFlow XLA RPC
Mr. Vengineer
 
PDF
Top 10 bugs in C++ open source projects, checked in 2016
PVS-Studio
 
PDF
Re-analysis of Umbraco code
PVS-Studio
 
PDF
The Ring programming language version 1.5.3 book - Part 89 of 184
Mahmoud Samir Fayed
 
PDF
Heading for a Record: Chromium, the 5th Check
PVS-Studio
 
Date Processing Attracts Bugs or 77 Defects in Qt 6
Andrey Karpov
 
Dusting the globe: analysis of NASA World Wind project
PVS-Studio
 
CppCat Static Analyzer Review
Andrey Karpov
 
Checking VirtualDub
Andrey Karpov
 
A Spin-off: CryEngine 3 SDK Checked with CppCat
Andrey Karpov
 
Linux version of PVS-Studio couldn't help checking CodeLite
PVS-Studio
 
Checking Bitcoin
Andrey Karpov
 
CppCat Checks OpenMW: Not All is Fine in the Morrowind Universe
Andrey Karpov
 
Analysis of the Trans-Proteomic Pipeline (TPP) project
PVS-Studio
 
Checking Notepad++: five years later
PVS-Studio
 
The CppCat Analyzer Checks TortoiseGit
Andrey Karpov
 
Дмитрий Демчук. Кроссплатформенный краш-репорт
Sergey Platonov
 
Clang tidy
Yury Yafimachau
 
Checking the Cross-Platform Framework Cocos2d-x
Andrey Karpov
 
Qunit Java script Un
akanksha arora
 
TensorFlow XLA RPC
Mr. Vengineer
 
Top 10 bugs in C++ open source projects, checked in 2016
PVS-Studio
 
Re-analysis of Umbraco code
PVS-Studio
 
The Ring programming language version 1.5.3 book - Part 89 of 184
Mahmoud Samir Fayed
 
Heading for a Record: Chromium, the 5th Check
PVS-Studio
 

Similar to Going On with the Check of Geant4 (20)

PDF
Static analysis should be used regularly
PVS-Studio
 
PDF
Handling False Positives in PVS-Studio and CppCat
Andrey Karpov
 
PDF
Tesseract. Recognizing Errors in Recognition Software
Andrey Karpov
 
PDF
Can We Trust the Libraries We Use?
Andrey Karpov
 
PDF
Errors that static code analysis does not find because it is not used
Andrey Karpov
 
PDF
LibRaw, Coverity SCAN, PVS-Studio
Andrey Karpov
 
PDF
Re-checking the ReactOS project - a large report
PVS-Studio
 
PDF
ChakraCore: analysis of JavaScript-engine for Microsoft Edge
PVS-Studio
 
PDF
Mathematicians: Trust, but Verify
Andrey Karpov
 
PDF
A Unicorn Seeking Extraterrestrial Life: Analyzing SETI@home's Source Code
PVS-Studio
 
PDF
Headache from using mathematical software
PVS-Studio
 
PDF
Checking OpenCV with PVS-Studio
PVS-Studio
 
PDF
Analyzing the Blender project with PVS-Studio
PVS-Studio
 
PDF
Anomalies in X-Ray Engine
PVS-Studio
 
PDF
Checking Intel IPP Samples for Windows - Continuation
PVS-Studio
 
PDF
Analyzing FreeCAD's Source Code and Its "Sick" Dependencies
PVS-Studio
 
PDF
Characteristics of PVS-Studio Analyzer by the Example of EFL Core Libraries, ...
PVS-Studio
 
PDF
Critical errors in CryEngine V code
PVS-Studio
 
PDF
How to find 56 potential vulnerabilities in FreeBSD code in one evening
PVS-Studio
 
PDF
Zero, one, two, Freddy's coming for you
Andrey Karpov
 
Static analysis should be used regularly
PVS-Studio
 
Handling False Positives in PVS-Studio and CppCat
Andrey Karpov
 
Tesseract. Recognizing Errors in Recognition Software
Andrey Karpov
 
Can We Trust the Libraries We Use?
Andrey Karpov
 
Errors that static code analysis does not find because it is not used
Andrey Karpov
 
LibRaw, Coverity SCAN, PVS-Studio
Andrey Karpov
 
Re-checking the ReactOS project - a large report
PVS-Studio
 
ChakraCore: analysis of JavaScript-engine for Microsoft Edge
PVS-Studio
 
Mathematicians: Trust, but Verify
Andrey Karpov
 
A Unicorn Seeking Extraterrestrial Life: Analyzing SETI@home's Source Code
PVS-Studio
 
Headache from using mathematical software
PVS-Studio
 
Checking OpenCV with PVS-Studio
PVS-Studio
 
Analyzing the Blender project with PVS-Studio
PVS-Studio
 
Anomalies in X-Ray Engine
PVS-Studio
 
Checking Intel IPP Samples for Windows - Continuation
PVS-Studio
 
Analyzing FreeCAD's Source Code and Its "Sick" Dependencies
PVS-Studio
 
Characteristics of PVS-Studio Analyzer by the Example of EFL Core Libraries, ...
PVS-Studio
 
Critical errors in CryEngine V code
PVS-Studio
 
How to find 56 potential vulnerabilities in FreeBSD code in one evening
PVS-Studio
 
Zero, one, two, Freddy's coming for you
Andrey Karpov
 
Ad

More from Andrey Karpov (20)

PDF
60 антипаттернов для С++ программиста
Andrey Karpov
 
PDF
60 terrible tips for a C++ developer
Andrey Karpov
 
PPTX
Ошибки, которые сложно заметить на code review, но которые находятся статичес...
Andrey Karpov
 
PDF
PVS-Studio in 2021 - Error Examples
Andrey Karpov
 
PDF
PVS-Studio in 2021 - Feature Overview
Andrey Karpov
 
PDF
PVS-Studio в 2021 - Примеры ошибок
Andrey Karpov
 
PDF
PVS-Studio в 2021
Andrey Karpov
 
PPTX
Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...
Andrey Karpov
 
PPTX
Best Bugs from Games: Fellow Programmers' Mistakes
Andrey Karpov
 
PPTX
Does static analysis need machine learning?
Andrey Karpov
 
PPTX
Typical errors in code on the example of C++, C#, and Java
Andrey Karpov
 
PPTX
How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)
Andrey Karpov
 
PPTX
Game Engine Code Quality: Is Everything Really That Bad?
Andrey Karpov
 
PPTX
C++ Code as Seen by a Hypercritical Reviewer
Andrey Karpov
 
PPTX
The Use of Static Code Analysis When Teaching or Developing Open-Source Software
Andrey Karpov
 
PPTX
Static Code Analysis for Projects, Built on Unreal Engine
Andrey Karpov
 
PPTX
Safety on the Max: How to Write Reliable C/C++ Code for Embedded Systems
Andrey Karpov
 
PPTX
The Great and Mighty C++
Andrey Karpov
 
PPTX
Static code analysis: what? how? why?
Andrey Karpov
 
PDF
PVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOps
Andrey Karpov
 
60 антипаттернов для С++ программиста
Andrey Karpov
 
60 terrible tips for a C++ developer
Andrey Karpov
 
Ошибки, которые сложно заметить на code review, но которые находятся статичес...
Andrey Karpov
 
PVS-Studio in 2021 - Error Examples
Andrey Karpov
 
PVS-Studio in 2021 - Feature Overview
Andrey Karpov
 
PVS-Studio в 2021 - Примеры ошибок
Andrey Karpov
 
PVS-Studio в 2021
Andrey Karpov
 
Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...
Andrey Karpov
 
Best Bugs from Games: Fellow Programmers' Mistakes
Andrey Karpov
 
Does static analysis need machine learning?
Andrey Karpov
 
Typical errors in code on the example of C++, C#, and Java
Andrey Karpov
 
How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)
Andrey Karpov
 
Game Engine Code Quality: Is Everything Really That Bad?
Andrey Karpov
 
C++ Code as Seen by a Hypercritical Reviewer
Andrey Karpov
 
The Use of Static Code Analysis When Teaching or Developing Open-Source Software
Andrey Karpov
 
Static Code Analysis for Projects, Built on Unreal Engine
Andrey Karpov
 
Safety on the Max: How to Write Reliable C/C++ Code for Embedded Systems
Andrey Karpov
 
The Great and Mighty C++
Andrey Karpov
 
Static code analysis: what? how? why?
Andrey Karpov
 
PVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOps
Andrey Karpov
 
Ad

Recently uploaded (20)

PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PPTX
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
PPT
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
PDF
Troubleshooting Virtual Threads in Java!
Tier1 app
 
PDF
SAP GUI Installation Guide for macOS (iOS) | Connect to SAP Systems on Mac
SAP Vista, an A L T Z E N Company
 
PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PPTX
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
PDF
WatchTraderHub - Watch Dealer software with inventory management and multi-ch...
WatchDealer Pavel
 
PDF
Why Are More Businesses Choosing Partners Over Freelancers for Salesforce.pdf
Cymetrix Software
 
PPTX
Explanation about Structures in C language.pptx
Veeral Rathod
 
PDF
Step-by-Step Guide to Install SAP HANA Studio | Complete Installation Tutoria...
SAP Vista, an A L T Z E N Company
 
PDF
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
PDF
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
PDF
Protecting the Digital World Cyber Securit
dnthakkar16
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PDF
AI Image Enhancer: Revolutionizing Visual Quality”
docmasoom
 
PDF
How Agentic AI Networks are Revolutionizing Collaborative AI Ecosystems in 2025
ronakdubey419
 
PDF
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
PPT
Brief History of Python by Learning Python in three hours
adanechb21
 
PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
Troubleshooting Virtual Threads in Java!
Tier1 app
 
SAP GUI Installation Guide for macOS (iOS) | Connect to SAP Systems on Mac
SAP Vista, an A L T Z E N Company
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
WatchTraderHub - Watch Dealer software with inventory management and multi-ch...
WatchDealer Pavel
 
Why Are More Businesses Choosing Partners Over Freelancers for Salesforce.pdf
Cymetrix Software
 
Explanation about Structures in C language.pptx
Veeral Rathod
 
Step-by-Step Guide to Install SAP HANA Studio | Complete Installation Tutoria...
SAP Vista, an A L T Z E N Company
 
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
Protecting the Digital World Cyber Securit
dnthakkar16
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
AI Image Enhancer: Revolutionizing Visual Quality”
docmasoom
 
How Agentic AI Networks are Revolutionizing Collaborative AI Ecosystems in 2025
ronakdubey419
 
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
Brief History of Python by Learning Python in three hours
adanechb21
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 

Going On with the Check of Geant4

  • 1. Going On with the Check of Geant4 Author: Andrey Karpov Date: 14.11.2013 This is the correct article about the results of checking the Geant4 project, which I have written after the previous incorrect one. Let me remind you the whole story. I have recently checked an old version of the Geant4 library and reported the results in the article "Copy-Paste and Muons". Why old version? Nobody is perfect, and we finally made a mistake ourselves. To find out which exactly, see the previous article. This time I offer you a brief report about checking Geant4 of the version 10.0-beta. Summary of the previous article In the article "Copy-Paste and muons" I was reasoning about how useful the static analysis methodology is and about the PVS-Studio analyzer's diagnostic abilities. I checked an ancient version of the Geant4 project (version 4.9.4), found a number of supposedly incorrect code fragments and described them in the article. Geant4 (for GEometry ANd Tracking) is a platform for "the simulation of the passage of particles through matter," using Monte Carlo methods. It is the successor of the GEANT series of software toolkits developed by CERN, and the first to use object oriented programming (in C++). Its development, maintenance and user support are taken care by the international Geant4 Collaboration. Application areas include high energy physics and nuclear experiments, medical, accelerator and space physics studies. The software is used by a number of research projects around the world. The project website: http://geant4.org. In the previous article, I described at least 16 suspicious code fragments. The recent check of the new version has revealed only 10 of those. The rest are either fixed or discarded together with the pieces of code. I won't discuss those defects once again in this article; if you want to see them, please refer to the previous article (there are comments below each sample telling if a particular bug is fixed in the new version or not.) Sorry for such a strange format of my investigation, but I hope that it won't in any way prevent the developers from fixing some defects in the Geant4 project and that the PVS-Studio will attract their attention. If I'm correct, the previous library version dates back to 2011. Much has changed since then, and it's no wonder that some new strange code fragments were found. Let's see if there is anything new or anything I missed during the previous check. New suspicious code fragments A complete list of all the suspicious fragments that caught my eye is saved in the file geant4_new.txt. But please don't rely solely on this list; the developers should check the project themselves and study all the
  • 2. warnings. We can grant a free registration key to the Geant4 developers for some time so that they could check their code: see the feedback page. Identical functions G4double G4CsvAnalysisManager::GetH2Xmin(G4int /*id*/) const { ExceptionForHistograms("GetH2Xmin"); return 0; } G4double G4CsvAnalysisManager::GetH2Xmax(G4int /*id*/) const { ExceptionForHistograms("GetH2Xmin"); return 0; } PVS-Studio's diagnostic message: V524 It is odd that the body of 'GetH2Xmax' function is fully equivalent to the body of 'GetH2Xmin' function. _G4analysis-archive g4csvanalysismanager.cc 933 The GetH2Xmax() function should probably call the ExceptionForHistograms() function with a different parameter: ExceptionForHistograms("GetH2Xmax"); This bug doesn't look serious. As far as I understand, this is a construct to handle an exception. However, I decided to mention this Copy-Paste bug anyway. Zero energy The function CalculateTotalEnergy () sums up values in the 'Etot' variable which suddenly gets zeroed. That's a very strange thing. G4double G4RKFieldIntegrator::CalculateTotalEnergy(const G4KineticTrackVector& Barions) { G4double Etot = 0; .... for(G4int c2 = c1 + 1; c2 < nBarion; c2++)
  • 3. { .... // Esk2 Etot += t1*std::pow(Alpha/pi, 3/2)* std::exp(-Alpha*r12*r12); // Eyuk Etot += ....; // Ecoul Etot += 1.44*p1->GetDefinition()->GetPDGCharge()* p2->GetDefinition()->GetPDGCharge()/r12* Erf(std::sqrt(Alpha)*r12); // Epaul Etot = 0; .... } .... } PVS-Studio's diagnostic message: V519 The 'Etot' variable is assigned values twice successively. Perhaps this is a mistake. Check lines: 80, 83. _G4processes-archive g4rkfieldintegrator.cc 83 Strange logic G4double G4EmBiasingManager::ApplySecondaryBiasing(....) { .... if(0 == nsplit) {
  • 4. 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's diagnostic message: V646 Consider inspecting the application's logic. It's possible that 'else' keyword is missing. _G4processes-archive g4embiasingmanager.cc 299 The code formatting suggests that the programmer was using the "else if" construct. But I can't see any here. If we format the code properly, we'll get the following: 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); }
  • 5. Notice that the block referring to the 'else' branch is executed every time whenever the "1 != nsplit" condition is true. It suspect that the programmer wanted the program logic to be somewhat different. A similar issue can be found in the following fragment: V646 Consider inspecting the application's logic. It's possible that 'else' keyword is missing. _G4processes-archive g4embiasingmanager.cc 347 Incomplete 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's diagnostic message: V607 Ownerless expression 'fDecayChannelsMap[label]'. _G4processes-archive g4moleculardecaytable.cc 140 The end of the function is very strange: fDecayChannelsMap[label] ; What's it? Something is missing? What did the programmer intend to do with an array cell?
  • 6. Kinematics The next sample is rather lengthy. I shortened it as much as I could, but unfortunately it is still pretty large. Read it and note the values the 'id' variable takes. 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 ); } .... } .... }
  • 7. PVS-Studio's diagnostic message: V621 Consider inspecting the 'for' operator. It's possible that the loop will be executed incorrectly or won't be executed at all. _G4processes-archive g4qmdcollision.cc 228 If the "if ( secs )" condition is false, the variable 'id' will remain equal to zero. In this case we might get the following loop: for ( G4int i0i = 0 ; i0i < -1 ; i0i++ ) And that will be a very strange loop indeed. I guess something is wrong with the CalKinematicsOfBinaryCollisions() function's logic. Miscellaneous There are a few more warnings I didn't describe in the previous article. Neither will I in this one. Just one sample: 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's diagnostic message. V596 The object was created but it is not being used. The 'throw' keyword could be missing: throw G4HadronicException(FOO); _G4processes-archive g4generalphasespacedecay.hh 116
  • 8. There are some mistakes with missing 'throw' operators. They result in an object of the 'G4HadronicException' type being created and at once destroyed, the program going on to work with incorrect data. For other examples of such typos, see the file geant4_new.txt. There you will also find some warnings related to microoptimizations. Conclusion Me checking obsolete code is a nice story, huh? Yeah, I've finally slipped up myself. :) A good opportunity to troll me, isn't it?