compute_average_mesh_edge_length: out-of-bounds read and divide-by-zero for an empty mesh (num_tris == 0)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 7.1k
- Forks
- 624
- Avg merge
- 3d 17h
- Merged PRs (30d)
- 5
Description
Describe the bug
compute_average_mesh_edge_length in warp/native/mesh.cu unconditionally indexes sum_edge_lengths[n - 1] and divides by 3 * n:
__global__ void compute_average_mesh_edge_length(int n, float* sum_edge_lengths, Mesh* m)
{
m->average_edge_length = sum_edge_lengths[n - 1] / (3 * n);
}
For an empty mesh (n == 0, i.e. no triangles), this reads sum_edge_lengths[-1] — an out-of-bounds device read (and sum_edge_lengths may itself be a zero-length / null buffer) — and divides by zero, producing a NaN average_edge_length, which is then used as the welding epsilon.
Location
warp/native/mesh.cu, compute_average_mesh_edge_length (≈ lines 57–60).
Reachability
Constructing a wp.Mesh with empty indices on a CUDA device.
Suggested fix
Guard the empty case — either skip the three launches when num_tris == 0 on the host side, or in-kernel:
m->average_edge_length = (n > 0) ? sum_edge_lengths[n - 1] / (3.0f * n) : 0.0f;
This removes the out-of-bounds read and the divide-by-zero, and aligns the CUDA path with the CPU path for a degenerate (empty) mesh.
Note on verification
Found by static analysis; I don't have a Warp build locally, so I have not reproduced the OOB at runtime. A maintainer could confirm by creating a wp.Mesh with empty indices on CUDA under compute-sanitizer --tool memcheck. Filing in case it's useful; happy to open a PR if the suggested fix looks right.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in warp/native/mesh.cu at compute_average_mesh_edge_length and trace the CUDA launches reached when a wp.Mesh has empty indices. Verify the empty case with compute-sanitizer --tool memcheck and confirm that average_edge_length is finite and matches the CPU path without an out-of-bounds read or divide-by-zero.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- backend, testing-qa
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 76/100