Problem 2182

Summary: G4PhysicsTable::insertAt() tries to insert on empty vectors
Product: Bugzilla Reporter: Akimoto Daichi <deltalphan>
Component: generalAssignee: asai
Status: RESOLVED FIXED    
Severity: minor CC: asai
Priority: P4    
Version: other   
Hardware: All   
OS: All   

Description Akimoto Daichi 2019-08-01 08:31:54 CEST
I'm using Geant4 10.04 patch01, but I think the code base hasn't changed on this topic.

When you invoke `G4PhysicsTable::insertAt()` it won't check the table size, so it sometimes tries to insert on empty vectors, and it will crash.

For example, the following code will crash at `physics_table_->insertAt(4, v);`.

```cpp
auto physics_table_ = new G4PhysicsTable(10); // specify the table size as 10 in its constructor, but it does only reserve() rather than resize()
auto v = new G4PhysicsOrderedFreeVector();
physics_table_->insertAt(4, v);
delete physics_table_;
delete v;
```

As a comparison, the following code works.

```cpp
auto physics_table_ = new G4PhysicsTable(); // not specify the table size
physics_table_->resize(10);  // make sure the table size is 10
auto v = new G4PhysicsOrderedFreeVector();
physics_table_->insertAt(4, v);
delete physics_table_;
delete v;
```
Comment 1 Akimoto Daichi 2019-08-01 08:43:08 CEST
Michael Kelsey gave me a comment on this.
https://geant4-forum.web.cern.ch/t/how-can-i-use-g4physicstable-insertat/635/2
Comment 2 asai 2019-08-02 18:35:24 CEST
Thank you for reporting this.
The further communication on the related Forum topic
https://geant4-forum.web.cern.ch/t/how-can-i-use-g4physicstable-insertat/635
showed the protection in insertAt() against idx > entries() is required. This fix will be included in the next patch release.