Skip to content

Fix Zonotope copy/move operations and add regression tests - #526

Open
divyani-22 wants to merge 1 commit into
GeomScale:developfrom
divyani-22:fix-zonotope-copy-move
Open

divyani-22 wants to merge 1 commit into
GeomScale:developfrom
divyani-22:fix-zonotope-copy-move

Conversation

@divyani-22

Copy link
Copy Markdown

Fixes #511

What was wrong

Zonotope (include/convex_bodies/zpolytope.h) maintained five raw scratch arrays (conv_comb, row_mem, row, colno, colno_mem) handed to the lp_solve based oracles. Its hand-written copy and move operations suffered from the exact defects previously diagnosed in #503 and fixed for VPolytope in #519:

  1. Copy assignment (operator=): copy_array(T* source, T* result, size_t count) passed result by value. It freed the member's buffer (delete [] result;) but never stored the allocated copy back into this. The member pointers dangled, causing heap-use-after-free and double-free on destruction.
  2. Move constructor and move assignment: colno = other.colno; colno = nullptr; and colno_mem = other.colno_mem; colno_mem = nullptr; nulled this->colno and this->colno_mem rather than the moved-from object's members. The moved-to object was left with null pointers, crashing on subsequent oracle calls. Additionally, move assignment did not free existing buffers before reassigning pointers.
  3. Loss of inner ball: _inner_ball was dropped across copy and move operations.

The Fix

  • Replaced the five raw pointer members with mutable std::vector<REAL> (conv_comb, row_mem, row) and mutable std::vector<int> (colno, colno_mem).
  • Passed the buffer pointers to oracles via .data().
  • Replaced *(conv_comb + j) pointer arithmetic with direct index access conv_comb[j].
  • Removed hand-written copy/move constructors, assignment operators, and destructor to adhere to the Rule of Zero. The compiler-generated special member functions now safely and correctly manage all buffers and preserve _inner_ball.

Tests

Added test/zonotope_test.cpp (registered as zonotope_copy_move in CTest) mirroring vpolytope_test.cpp:

  • Tests copy constructor and copy assignment.
  • Tests move constructor and move assignment.
  • Tests default-construct-then-assign pattern.
  • Verifies preservation of _inner_ball.
  • Evaluates ray shooting and polytope membership (line_intersect and is_in) on each result.

Copilot AI balanced review requested due to automatic review settings November 10, 2026 13:28

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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.

Zonotope copy assignment leaves dangling buffers; move operations null the wrong pointers

2 participants