Skip to content

Commit

Permalink
fix: Avoid segfaults in bin adjustments (#3472)
Browse files Browse the repository at this point in the history
Previously, only the surface type was checked, and then a bounds type assumed. Then, the result of a `dynamic_cast` was used unchecked.
This changes this to ensure correcteness by checking the `dynamic_cast`s.
  • Loading branch information
benjaminhuth authored Aug 3, 2024
1 parent 5ca5ec0 commit 0b9ed36
Showing 1 changed file with 19 additions and 27 deletions.
46 changes: 19 additions & 27 deletions Core/include/Acts/Utilities/BinAdjustment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,37 +239,29 @@ static inline BinUtility adjustBinUtility(const BinUtility& bu,
static inline BinUtility adjustBinUtility(const BinUtility& bu,
const Surface& surface,
const GeometryContext& gctx) {
// The surface type is a cylinder
if (surface.type() == Surface::Cylinder) {
// Cast to Cylinder bounds and return
auto cBounds = dynamic_cast<const CylinderBounds*>(&(surface.bounds()));
// Return specific adjustment
return adjustBinUtility(bu, *cBounds, surface.transform(gctx));

} else if (surface.type() == Surface::Disc) {
// Cast to Cylinder bounds and return
auto rBounds = dynamic_cast<const RadialBounds*>(&(surface.bounds()));
// Return specific adjustment
return adjustBinUtility(bu, *rBounds, surface.transform(gctx));
} else if (surface.type() == Surface::Plane) {
if (surface.bounds().type() == SurfaceBounds::eRectangle) {
// Cast to Plane bounds and return
auto pBounds = dynamic_cast<const RectangleBounds*>(&(surface.bounds()));
// Return specific adjustment
return adjustBinUtility(bu, *pBounds, surface.transform(gctx));
} else if (surface.bounds().type() == SurfaceBounds::eTrapezoid) {
// Cast to Plane bounds and return
auto pBounds = dynamic_cast<const TrapezoidBounds*>(&(surface.bounds()));
// Return specific adjustment
return adjustBinUtility(bu, *pBounds, surface.transform(gctx));
} else {
throw std::invalid_argument(
"Bin adjustment not implemented for this type of plane surface yet!");
if (auto b = dynamic_cast<const CylinderBounds*>(&(surface.bounds()));
b != nullptr) {
return adjustBinUtility(bu, *b, surface.transform(gctx));
}
if (auto b = dynamic_cast<const RadialBounds*>(&(surface.bounds()));
b != nullptr) {
return adjustBinUtility(bu, *b, surface.transform(gctx));
}
if (surface.type() == Surface::Plane) {
if (auto b = dynamic_cast<const RectangleBounds*>(&(surface.bounds()));
b != nullptr) {
return adjustBinUtility(bu, *b, surface.transform(gctx));
}
if (auto b = dynamic_cast<const TrapezoidBounds*>(&(surface.bounds()));
b != nullptr) {
return adjustBinUtility(bu, *b, surface.transform(gctx));
}
}

std::stringstream ss;
ss << surface.toStream({});
throw std::invalid_argument(
"Bin adjustment not implemented for this surface yet!");
"Bin adjustment not implemented for this surface yet:\n" + ss.str());
}

} // namespace Acts

0 comments on commit 0b9ed36

Please sign in to comment.