Skip to content

Fix: initialize facet to -1 in line_positive_intersect to prevent undefined behavior#457

Open
Dev-sh21 wants to merge 3 commits intoGeomScale:developfrom
Dev-sh21:fix/uninit-facet-line-positive-intersect
Open

Fix: initialize facet to -1 in line_positive_intersect to prevent undefined behavior#457
Dev-sh21 wants to merge 3 commits intoGeomScale:developfrom
Dev-sh21:fix/uninit-facet-line-positive-intersect

Conversation

@Dev-sh21
Copy link
Copy Markdown

hi @vissarion sir

In HPolytope::line_positive_intersect (the update_parameters overload, ~line 520 of hpolytope.h), the variable facet is declared but never initialized:

int m = num_of_hyperplanes(), facet;  // uninitialized

this by default takes garabge value, so it leads to error.
i have set this value to -1.

i have also attached all the proofs below.

Image

Image
Image
Image

In HPolytope::line_positive_intersect (update_parameters overload),
the variable 'facet' was declared but not initialized:

  int m = num_of_hyperplanes(), facet;

When no valid positive intersection exists (valid_plus is all-false),
the if-block is skipped so 'facet' is never assigned. The garbage
value then propagates into params.facet_prev, which is used in the
next reflection step to index into AA.col(params.facet_prev) causing
out-of-bounds access and undefined behavior.

Fixed by initializing facet = -1, consistent with all other overloads
of line_positive_intersect in this file. The guard
'if (params.facet_prev >= 0)' already handles the -1 sentinel.

Affected locations: 4 occurrences across hpolytope.h.
Copy link
Copy Markdown
Contributor

@Mohit-Lakra Mohit-Lakra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Dev-sh21
Copy link
Copy Markdown
Author

hi @vissarion sir, is there any change required?

Copy link
Copy Markdown
Member

@vissarion vissarion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR. Please provide the code triggered the bug. The screenshots are not that useful.

@Dev-sh21
Copy link
Copy Markdown
Author

Dev-sh21 commented Mar 2, 2026

hi @vissarion sir,
apologies for the confusion

here is the minimal reproducible c++ snippet that triggers the bug, if a ray originates at the boundary and points directly outwards, it never finds a positive intersection (lamda > 0 condition fails).

// Create a simple 1D polytope: x <= 1
Eigen::MatrixXd A(1, 1); A << 1.0;
Eigen::VectorXd b(1); b << 1.0;
HPolytope<Cartesian<double>> P(1, A, b);

// Origin point on the boundary (x = 1), pointing outwards (+1 direction)
Point r(1); r.set_coord(0, 1.0);
Point v(1); v.set_coord(0, 1.0);

Eigen::VectorXd Ar, Av;
// This will return an uninitialized (garbage) integer for facet
// because no lambda > 0 is found.
std::pair<double, int> intersection = P.line_positive_intersect(r, v, Ar, Av);


@Dev-sh21
Copy link
Copy Markdown
Author

Dev-sh21 commented Mar 2, 2026

in the [line_positive_intersect] function, the facet variable is declared without initialization
When a ray does not positively intersect the polytope (meaning lamda is never > 0), the (if (lamda < min_plus && lamda > 0)) block is never executed, this causes the function to return a garbage value

by explicitly initializing facet = -1 at declaration, we ensure that if no positive intersection is found, the function returns a deterministic invalid state -1 rather than triggering undefined behavior with a garbage value.

@vissarion
Copy link
Copy Markdown
Member

in the [line_positive_intersect] function, the facet variable is declared without initialization When a ray does not positively intersect the polytope (meaning lamda is never > 0), the (if (lamda < min_plus && lamda > 0)) block is never executed, this causes the function to return a garbage value

by explicitly initializing facet = -1 at declaration, we ensure that if no positive intersection is found, the function returns a deterministic invalid state -1 rather than triggering undefined behavior with a garbage value.

Thank you for the example. Out of curiosity how you discovered that issue? Do you have a sampling or volume example that triggers this behavior?

@Dev-sh21
Copy link
Copy Markdown
Author

in the [line_positive_intersect] function, the facet variable is declared without initialization When a ray does not positively intersect the polytope (meaning lamda is never > 0), the (if (lamda < min_plus && lamda > 0)) block is never executed, this causes the function to return a garbage value
by explicitly initializing facet = -1 at declaration, we ensure that if no positive intersection is found, the function returns a deterministic invalid state -1 rather than triggering undefined behavior with a garbage value.

Thank you for the example. Out of curiosity how you discovered that issue? Do you have a sampling or volume example that triggers this behavior?

hi @vissarion sir,
i found the uninitialized facet while running ghmc sampling on a 10d h-cube, where it caused occasional crashes during reflections. i've updated the pr to initialize facet to -1 and resolved gaussian walk signature mismatches to ensure all sampling tests pass locally

@Dev-sh21 Dev-sh21 requested a review from vissarion March 12, 2026 14:37
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.

3 participants