Skip to content

Return the ring with the basis from the test parser, so it cannot leak - #96

Merged
d-torrance merged 1 commit into
Macaulay2:masterfrom
d-torrance:ring-and-basis-from-string
Sep 25, 2026
Merged

d-torrance merged 1 commit into
Macaulay2:masterfrom
d-torrance:ring-and-basis-from-string

Conversation

@d-torrance

Copy link
Copy Markdown
Member

basisParseFromString, the test helper that parses a ring and a basis from a
string, returned only the basis:

  auto p = MathicIO<>().readRing(true, in);
  auto& ring = *p.first.release(); // todo: fix leak
  return make_unique<Basis>(MathicIO<>().readBasis(ring, false, in));

A Basis refers to its ring without owning it, so once the unique_ptr was
released nothing owned the ring. That todo dates from bbb09cb in 2013.

  • IO.ideal and Ideal.readwrite leaked the ring. That was the whole of
    the suite's ASan leak report: 1216 byte(s) leaked in 8 allocation(s).
  • Poly.lead avoided the leak by taking ownership itself, through the const
    pointer getPolyRing() returns -- and declared that owner after the basis,
    so the ring was destroyed before the basis that refers to it.

The fix

It is now ringAndBasisFromString, returning both:

std::pair<std::unique_ptr<PolyRing>, std::unique_ptr<Basis>>
ringAndBasisFromString(std::string str);

The three tests bind them with auto [ring, I] = ..., so the ring lives exactly
as long as the basis and, since std::pair destroys its members in reverse, is
destroyed after it. Poly.lead drops its workaround.

Why rename it, not just change its return type

C++ names do not encode the return type. Changing only the return type would
have kept the exported symbol mgb::basisParseFromString(std::string), so a
binary built against the old header would still link and then read one pointer
where two were written. With the old name gone, such a binary fails to load
with an undefined symbol instead.

io-util.hpp is installed and the function is exported from libmathicgb.so,
so this is an ABI change to note for the release. Nothing outside this
repository is known to call it: its only reverse dependency packaged in Ubuntu
is Macaulay2, which does not, and a GitHub code search finds only copies of
mathicgb.

Verification

  • 254/254 in cmake Release and Debug, GCC 13.3, no build warnings.
  • The full suite now runs clean under ASan -- no leak report at all, where
    master reports 1216 bytes in 8 allocations.
  • clang 18 with -Wall -Wextra raises nothing new in the changed files.

No autotools distcheck run locally; the commit touches no build system and CI
covers it.

🤖 Generated with Claude Code

basisParseFromString read a ring and a basis from a string and returned
only the basis:

  auto& ring = *p.first.release(); // todo: fix leak

A Basis refers to its ring without owning it, so nothing owned the
ring.  That comment dates from bbb09cb in 2013.  Two of its three
callers, the IO.ideal and Ideal.readwrite tests, leaked the ring, and
that was the whole of the suite's ASan leak report: 1216 bytes in 8
allocations.  The third, Poly.lead, avoided the leak by taking
ownership itself, through the const pointer getPolyRing() returns, and
declared that owner after the basis, so the ring was destroyed before
the basis that refers to it.

It is now ringAndBasisFromString, returning both as
pair<unique_ptr<PolyRing>, unique_ptr<Basis>>.  The callers bind them
with a structured binding, so the ring lives exactly as long as the
basis and is destroyed after it, and Poly.lead drops its workaround.
The suite now runs clean under ASan, with no leak report at all.

The rename is deliberate.  C++ names do not encode the return type, so
changing only the return type would have kept the exported symbol
mgb::basisParseFromString(std::string), and a binary built against the
old header would still link, then read one pointer where two were
written.  With the old name gone, such a binary fails to load with an
undefined symbol instead.  Nothing outside this repository is known to
call it: its only reverse dependency packaged in Ubuntu is Macaulay2,
which does not, and a GitHub code search finds only copies of
mathicgb.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@d-torrance
d-torrance merged commit b6b2dc9 into Macaulay2:master Sep 25, 2026
18 checks passed
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