Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

odb: provide a free function for custom backends #5005

Merged
merged 2 commits into from
Mar 2, 2019

Conversation

ethomson
Copy link
Member

The git_odb_backend_malloc name is a system function that is provided for custom ODB backends and allows them to allocate memory for an ODB object in the read callback. This is important so that libgit2 can later free the memory used by an ODB object that was read from the custom backend.

(Simply taking data from the backend that it allocated means that we don't know how to free it, we could be using two different allocators if the library was built "normally" and the app in question has an instrumented allocator for debugging then libgit2 could not free the data allocated by the custom backend.)

However, if an error occurs in the custom backend after the memory has been allocated for the custom object but before it's returned to libgit2, the custom backend has no way to free that memory and it must be leaked.

Provide a free function that corresponds to the alloc function so that custom backends have an opportunity to free memory before they return an error.

While creating this free function and searching for a name for it, I tweaked the name of the allocation function to avoid sounding like we allocated a backend instead of a bit of backend data. I created a proxy for safe deprecation, but kept the definition in sys/odb_backend.h to avoid cluttering deprecated.h with data about a function that is only included explicitly (by including sys/odb_backend.h).

The `git_odb_backend_malloc` name is a system function that is provided
for custom ODB backends and allows them to allocate memory for an ODB
object in the read callback.  This is important so that libgit2 can
later free the memory used by an ODB object that was read from the
custom backend.

However, the name _suggests_ that it actually allocates a
`git_odb_backend`.  It does not; rename it to make it clear that it
actually allocates backend _data_.
Custom backends can allocate memory when reading objects and providing
them to libgit2.  However, if an error occurs in the custom backend
after the memory has been allocated for the custom object but before
it's returned to libgit2, the custom backend has no way to free that
memory and it must be leaked.

Provide a free function that corresponds to the alloc function so that
custom backends have an opportunity to free memory before they return an
error.
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.

None yet

1 participant