Skip to content

intersect_line_with_prism: slist dedup never writes back #82

@Luochenghuang

Description

@Luochenghuang

In utils/geom.c around line 2234 (master cd191af), the duplicate-removal step at the end of intersect_line_with_prism looks like this:

double slist_unique[num_vertices+2];
slist_unique[0] = slist[0];
for (nv = 1; nv < num_intersections; nv++) {
  if (fabs(slist[nv] - slist[nv-1]) > duplicate_tolerance*fabs(slist[nv])) {
    slist_unique[num_unique_elements] = slist[nv];
    num_unique_elements++;
  }
}
slist = slist_unique;
num_intersections = num_unique_elements;
return num_intersections;

The dedup loop writes the deduped values into a local VLA slist_unique, then does slist = slist_unique; and returns the new count. But slist is a pass-by-value pointer parameter, reassigning it just rebinds the local variable. The caller's buffer is never touched, and slist_unique is destroyed when the function returns. So the caller gets the right count but reads the wrong values: they end up looking at the first count entries of the sorted-but-undeduped buffer.

The fix is a one-liner:

-    slist = slist_unique;
+    memcpy(slist, slist_unique, num_unique_elements * sizeof(double));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions