Skip to content

Return only actually-read nodes when gmsh $Nodes over-declares total_num_nodes - #1586

Open
1915rising wants to merge 1 commit into
nschloe:mainfrom
1915rising:fix/1562-gmsh-overdeclared-nodes
Open

1915rising wants to merge 1 commit into
nschloe:mainfrom
1915rising:fix/1562-gmsh-overdeclared-nodes

Conversation

@1915rising

Copy link
Copy Markdown

Fixes #1562.

What broke: In the gmsh 4.1 reader, _read_nodes preallocates points, tags and dim_tags with the total_num_nodes declared in the $Nodes header and returns them in full. When a file declares more nodes than its entity blocks actually contain (the reporter's gmsh-produced file declares 120 nodes with tags only spanning 1-102), the tail of those arrays is uninitialised memory. _read_elements then takes np.max(point_tags) over the garbage tags and tries to allocate an inv_tags array of absurd size — the reporter saw requests for ~903 TB; whether it explodes or silently returns phantom points depends on whatever happens to be in that memory.

Reproduction: A minimal ASCII MSH 4.1 file whose $Nodes header declares 10 nodes while its single block contains 4 (tags 1-4), plus one point element. On current main this fails with MemoryError: Unable to allocate 1.23 EiB for an array with shape (177612303740502052,). The same file must read as exactly the 4 real nodes.

Fix: _read_nodes in _gmsh41.py now returns points[:idx], tags[:idx], dim_tags[:idx] — only the nodes actually read (this is also the fix shape the reporter verified against their file). The gmsh 4.0 ASCII reader in _gmsh40.py has the identical preallocate-and-return-full pattern, so it gets the same slice; its binary path already concatenates only actual data and is untouched.

Tests: New regression test test_gmsh41_overdeclared_num_nodes in tests/test_gmsh.py. tests/test_gmsh.py: 91 passed / 22 failed before, 92 passed / 22 failed after — the 22 failures are pre-existing Git-LFS pointer fixtures, identical before and after. Full suite: 518 passed / 174 failed before, 519 passed / 174 failed after, failure count unchanged (pre-existing environment/LFS failures).

Overlap: None that I can see — checked the open PR list; the other open gmsh-related PRs touch MED/ANSYS, and #1584/#1585 (also mine) fix VTU/OBJ issues.

Disclosure: this fix was prepared with AI assistance and verified locally as described above.

This branch has not been deployed

No deployments
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.

[BUG] request for 903TB of memory if gmsh provides wrong total_num_nodes

1 participant