Skip to content

Gecode 7: state and assert the heap size contract - #289

Draft
zayenz wants to merge 3 commits into
feature/breaking-7-05-value-paramsfrom
feature/breaking-7-06-heap-contract
Draft

zayenz wants to merge 3 commits into
feature/breaking-7-05-value-paramsfrom
feature/breaking-7-06-heap-contract

Conversation

@zayenz

@zayenz zayenz commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Across several modules, Gecode still requested zero bytes from the heap or a region, or passed null pointers with a zero element count to memcpy. What a zero-byte allocation returns is implementation-defined, and passing a null pointer to memcpy is undefined behavior in C and C++ even when the length is zero.

This PR removes the remaining zero-size requests and null-pointer copies, then makes the heap contract explicit:

  • An Action or CHB created over an empty array keeps its shared storage so it remains initialized with size zero, copyable, and (for Action) still accepts decay updates, but it no longer allocates a zero-size element buffer or posts a recorder propagator.
  • TupleSet, Archive, Space::rrealloc, the FlatZinc set and regular builtins, the example dictionary, DFA and TupleSet-from-DFA construction, and IntSet initializer lists no longer make zero-byte requests or pass null pointers to memcpy. Buffers that can now stay null are freed only when non-null; an empty initializer list constructs an empty IntSet, and a FlatZinc regular constraint with a reversed final-state range treats the range as empty.
  • Heap now documents its size contract: allocation sizes must be positive, realloc may grow from zero elements (while rrealloc no longer accepts zero), and copy requires non-null pointers even for zero elements. Debug builds assert the positive-size precondition in Heap::ralloc and Heap::rrealloc.

Stack

Merge in this order. #242 targets main, and each later PR builds on the one before it so its diff shows only its own commits. The first four PRs are standalone search and random-number changes; #284 through #274 are the Gecode 7 changes split out of what used to be a single branch.

  1. Rank solutions with Space::compare in best-solution search #242 Rank solutions with Space::compare
  2. Rename the meta-search hooks to hub and spoke #282 Rename the meta-search hooks to hub and spoke
  3. Give meta search hub and spoke contexts and report why search ended #283 Hub and spoke contexts and end reasons
  4. Add extensible random generators with reproducible stream splitting #241 Splittable random generators
  5. Gecode 7: CMake-only C++23 build baseline #284 Gecode 7: build baseline
  6. Gecode 7: format the sources with LLVM 22 #285 Gecode 7: reformat
  7. Gecode 7: normalized API names, LT constants and Clang checks #286 Gecode 7: API names, LT constants, checks
  8. Gecode 7: post nothing for empty arrays #287 Gecode 7: empty arrays
  9. Gecode 7: pass large parameters by const reference #288 Gecode 7: const& parameters
  10. Gecode 7: state and assert the heap size contract #289 Gecode 7: heap contract ← this PR
  11. Gecode 7: trivially copyable handles and defaulted special members #290 Gecode 7: modern classes
  12. Gecode 7: native float interval instead of Boost #291 Gecode 7: native float interval
  13. Gecode 7: bug fixes and Doxygen warnings #292 Gecode 7: bug fixes and Doxygen
  14. Gecode 7: scoped enums and descriptive constant names #293 Gecode 7: scoped enums
  15. Gecode 7: clang-tidy sweep #274 Gecode 7: clang-tidy sweep

An Action or CHB over an empty array keeps its shared storage, so it stays initialized with size zero, copyable, and an empty Action still accepts a decay change. It no longer requests or frees a zero-size element buffer and no longer posts a recorder propagator.

Zdev-Change-Id: Zd4e6b819bd4f6bcd64b83d6fcd1a94d1bdf26ec1dc2e580c53e90ed98a16a1c9
TupleSet, Archive, Space::rrealloc, the FlatZinc set and regular builtins, the example dictionary, DFA and TupleSet construction from a DFA, and IntSet initializer lists no longer request zero bytes from the heap or a region and no longer pass null pointers to memcpy. Buffers that can now be null are freed only when non-null. An empty initializer list gives the empty IntSet, and a FlatZinc regular constraint with a reversed final-state range treats it as empty.

Zdev-Change-Id: Z739fba34334ff1732238cd5ef32e1903b009ce0fe1dcc1ec967ee3213b53009f
Heap requests now document that allocation sizes are positive, that realloc may grow from zero elements, and that copy needs non-null pointers even for zero elements. Debug builds assert a positive size in Heap::ralloc and rrealloc, and rrealloc no longer accepts zero. Two lint findings are fixed: an intentional copy in the branch tests gets a NOLINT, and an assert in TupleSet finalization documents a count that clang-analyzer cannot follow.

Zdev-Change-Id: Z56a1f083f220acb099fd0bc9a9ac161b74b110451fa021068f6b7b92b1e3acca

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.

1 participant