Skip to content

C Util API: Fix leak of demangled error name#14567

Merged
edolstra merged 2 commits intoNixOS:masterfrom
pkpbynum:pb/fix-c-api-ctx-err-leak
Nov 19, 2025
Merged

C Util API: Fix leak of demangled error name#14567
edolstra merged 2 commits intoNixOS:masterfrom
pkpbynum:pb/fix-c-api-ctx-err-leak

Conversation

@pkpbynum
Copy link
Contributor

Motivation

Fix leak of a demangled error name in C util API. Valgrind was flagging this and I noticed the TODO comment--not sure if there was a specific reason for this originally.

Context


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@pkpbynum pkpbynum requested a review from edolstra as a code owner November 14, 2025 16:09
@github-actions github-actions bot added the c api Nix as a C library with a stable interface label Nov 14, 2025
if (demangled) {
context->name = demangled;
// todo: free(demangled);
free((void*)demangled);
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know the exact details, but my intuition is that the damangled string's ownership is somewhat stdlib specific. Does this work with both libc++ (we have an llvm build via nix-everything-llvm flake output) and libstdc++?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, I was just using the "default"/non-suffixed packages. I just ran it again with the *-llvm version and it looks to be working there too. I'm not very familiar with the repo so let me know if there's other things I should test

Copy link
Member

Choose a reason for hiding this comment

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

The todo was written by @jlesquembre, maybe he remembers what the reason was?

Copy link
Member

Choose a reason for hiding this comment

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

Actually, it was written by @yorickvP:
4702317

I moved the file as part of a refactor

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't remember the reason. Maybe I was thinking the context stores a pointer to the error name, but it's actually copied there, so this free should be safe.

@edolstra edolstra added this pull request to the merge queue Nov 19, 2025
Merged via the queue into NixOS:master with commit 8b167ea Nov 19, 2025
16 checks passed
@edolstra edolstra mentioned this pull request Dec 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c api Nix as a C library with a stable interface

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants