Problem 2182 - G4PhysicsTable::insertAt() tries to insert on empty vectors
Summary: G4PhysicsTable::insertAt() tries to insert on empty vectors
Status: RESOLVED FIXED
Alias: None
Product: Bugzilla
Classification: Unclassified
Component: general (show other problems)
Version: other
Hardware: All All
: P4 minor
Assignee: asai
URL:
Depends on:
Blocks:
 
Reported: 2019-08-01 08:31 CEST by Akimoto Daichi
Modified: 2021-11-05 02:26 CET (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this problem.
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.