Skip to content

[Low] Guard against null GetVisAttributes() in G4Ttree_item constructor #137

@zhaozhiwen

Description

@zhaozhiwen

G4Ttree_item's constructor dereferences lvolume->GetVisAttributes() without a null check. A logical volume with no vis attributes returns nullptr, crashing the tree viewer.

Affected files

  • gemc/gtree/gtree.cc:43-55 (G4Ttree_item::G4Ttree_item)

Details

// Read visualization attributes from the logical volume.
auto visAttributes = lvolume->GetVisAttributes();
auto gcolor = visAttributes->GetColour();   // <-- visAttributes may be nullptr

double red   = gcolor.GetRed();
double green = gcolor.GetGreen();
double blue  = gcolor.GetBlue();
auto alpha   = gcolor.GetAlpha();

color      = QColor::fromRgbF(red, green, blue);
opacity    = alpha;
is_visible = visAttributes->IsVisible();        // <-- second deref

G4LogicalVolume::GetVisAttributes() returns nullptr when no vis attributes have been assigned to the volume. GEMC builds vis attributes for its own volumes, but imported geometries (CAD/GDML) and any volume that skips createVisualAttributes can have a logical volume with nullptr attributes. Both visAttributes->GetColour() and visAttributes->IsVisible() then dereference a null pointer.

Impact

Suspected crash (segfault) in the interactive geometry tree viewer whenever it encounters a logical volume without vis attributes. The viewer builds a G4Ttree_item for every volume (gtree.cc:232), so a single such volume crashes the whole tree build.

Proposed fix

Null-check the pointer and fall back to a sensible default (visible, opaque white) when no vis attributes exist.

 	// Read visualization attributes from the logical volume.
 	auto visAttributes = lvolume->GetVisAttributes();
-	auto gcolor = visAttributes->GetColour();
-
-	double red = gcolor.GetRed();
-	double green = gcolor.GetGreen();
-	double blue = gcolor.GetBlue();
-	auto alpha = gcolor.GetAlpha();
-
-	color = QColor::fromRgbF(red, green, blue);
-
-	opacity = alpha;
-
-	is_visible = visAttributes->IsVisible();
+	if (visAttributes != nullptr) {
+		auto gcolor = visAttributes->GetColour();
+		color      = QColor::fromRgbF(gcolor.GetRed(), gcolor.GetGreen(), gcolor.GetBlue());
+		opacity    = gcolor.GetAlpha();
+		is_visible = visAttributes->IsVisible();
+	}
+	else {
+		// No vis attributes assigned: fall back to an opaque white, visible default.
+		color      = QColor::fromRgbF(1.0, 1.0, 1.0);
+		opacity    = 1.0;
+		is_visible = true;
+	}

Split out from #102 (one issue per bug). Found via an AI-assisted code review with manual verification against commit 5f8ce875.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Planned

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions