Problem 1655

Summary: G4VPhysicalVolume: unsafe handling of rotation matrix for unrotated volumes
Product: Geant4 Reporter: Nicola Mori <mori>
Component: geometry/volumesAssignee: Gabriele Cosmo <Gabriele.Cosmo>
Status: RESOLVED WONTFIX    
Severity: trivial CC: mori
Priority: P5    
Version: 10.0   
Hardware: All   
OS: All   

Description Nicola Mori 2014-07-11 15:47:47 CEST
In G4VPhysicalVolume::GetObjectRotation a pointer to the statically initialized identity matrix IdentityRM is returned for unrotated objects. However, the calling function might modify the static object pointed by the return pointer, so that a subsequent call to G4VPhysicalVolume::GetObjectRotation will return a non-identity matrix. The fact that &IdentityRM is returned as a non-const pointer does not generate an error at compile time since IdentityRM is not a member object of G4VPhysicalVolume. The const signature

This problem might be solved either by defining a non-static IdentityRM with a performance penalty or by changing the function signature to:

  const G4RotationMatrix* G4VPhysicalVolume::GetObjectRotation() const;

with all the cons of an API break.
Comment 1 Nicola Mori 2014-07-11 15:51:36 CEST
Sorry, I posted before completing a sentence. Here's the complete report:

In G4VPhysicalVolume::GetObjectRotation a pointer to the statically initialized
identity matrix IdentityRM is returned for unrotated objects. However, the
calling function might modify the static object pointed by the return pointer,
so that a subsequent call to G4VPhysicalVolume::GetObjectRotation will return a
non-identity matrix. The fact that &IdentityRM is returned as a non-const
pointer does not generate an error at compile time since IdentityRM is not a
member object of G4VPhysicalVolume. The const signature for the method is then uneffective in forbidding the modification of a static variable returned by address.

This problem might be solved either by defining a non-static IdentityRM with a
performance penalty or by changing the function signature to:

  const G4RotationMatrix* G4VPhysicalVolume::GetObjectRotation() const;

with all the cons of an API break.
Comment 2 Gabriele Cosmo 2014-07-11 16:52:36 CEST
The method GetObjectRotation() in G4VPhysicalVolume is in fact obsolete as reported in the declaration (the issue you mention are known and unfortunate, and your first suggested solution won't work as you cannot return the address of a non-static local object to the function...); the method is kept as is for backwards compatibility.
The method GetObjectRotationValue() should be used instead.