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.

@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.70%. Comparing base (3e34909) to head (3f591b8).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2065   +/-   ##
=======================================
  Coverage   95.70%   95.70%           
=======================================
  Files          30       30           
  Lines        9447     9447           
  Branches     1406     1407    +1     
=======================================
  Hits         9041     9041           
  Misses        252      252           
  Partials      154      154           

☔ 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.

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.
@HaozheZhang6
HaozheZhang6 force-pushed the fix/assembly-load-segfault branch from aa9b454 to 3f591b8 Compare July 14, 2026 05:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants