Skip to content

Fix segfault when loading an assembly with no children from xml/xbf#2065

Open
HaozheZhang6 wants to merge 1 commit into
CadQuery:masterfrom
HaozheZhang6:fix/assembly-load-segfault
Open

Fix segfault when loading an assembly with no children from xml/xbf#2065
HaozheZhang6 wants to merge 1 commit into
CadQuery:masterfrom
HaozheZhang6:fix/assembly-load-segfault

Conversation

@HaozheZhang6

Copy link
Copy Markdown
Contributor

Loading an assembly that holds a single shape and no children crashes the interpreter:

import cadquery as cq

assy = cq.Assembly(cq.Solid.makeBox(1, 1, 1))
assy.export("a.xml")
cq.Assembly.load("a.xml")     # Fatal Python error: Segmentation fault

Same for .xbf. Adding a single child makes it go away, which is why the existing round-trip tests do not hit it -- every fixture has children. STEP is unaffected.

Cause: such an assembly has no TDataStd_Name on its top-level label after the XCAF round trip, and _importDoc calls

name_attr = TDataStd_Name()
top_level_label.FindAttribute(TDataStd_Name.GetID_s(), name_attr)
assy.name = str(name_attr.Get().ToExtString())

FindAttribute segfaults on a label that does not carry the attribute rather than returning false:

Fatal Python error: Segmentation fault
  File "cadquery/occ_impl/importers/assembly.py", line 378 in _importDoc
  File "cadquery/occ_impl/importers/assembly.py", line 227 in importXml

The module already has a helper that gets this right -- _get_name checks IsAttribute before calling FindAttribute -- so use it, and keep the generated name when the file does not carry one.

Test: test_assembly_without_children_roundtrip parametrized over step/xml/xbf. On master the xml and xbf cases take the whole pytest process down with SIGSEGV.

An assembly holding a single shape and no children has no name attribute on
its top-level label after an XCAF round trip. _importDoc called FindAttribute
on that label unconditionally, which segfaults instead of returning false.

Use the existing _get_name helper, which checks IsAttribute first, and keep
the generated name when the file does not carry one.
@codecov

codecov Bot commented Jul 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.79%. Comparing base (6d741ee) to head (aa9b454).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2065   +/-   ##
=======================================
  Coverage   95.79%   95.79%           
=======================================
  Files          30       30           
  Lines        9435     9435           
  Branches     1405     1406    +1     
=======================================
  Hits         9038     9038           
  Misses        242      242           
  Partials      155      155           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant