From 459ac856d94d4a59ee47b2807aa8e4b029499582 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Sat, 23 Feb 2019 18:42:53 +0000 Subject: odb: provide a free function for custom backends 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. --- include/git2/sys/odb_backend.h | 11 +++++++++++ src/odb.c | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/include/git2/sys/odb_backend.h b/include/git2/sys/odb_backend.h index 15c741577..6614dcf30 100644 --- a/include/git2/sys/odb_backend.h +++ b/include/git2/sys/odb_backend.h @@ -131,6 +131,17 @@ GIT_EXTERN(int) git_odb_init_backend( */ GIT_EXTERN(void *) git_odb_backend_data_alloc(git_odb_backend *backend, size_t len); +/** + * Frees custom allocated ODB data. This should only be called when + * memory allocated using git_odb_backend_data_alloc is not returned + * to libgit2 because the backend encountered an error in the read + * function after allocation and did not return this data to libgit2. + * + * @param backend the ODB backend that is freeing this memory + * @param data the buffer to free + */ +GIT_EXTERN(void) git_odb_backend_data_free(git_odb_backend *backend, void *data); + /* * Users can avoid deprecated functions by defining `GIT_DEPRECATE_HARD`. diff --git a/src/odb.c b/src/odb.c index c5af38919..1c923c5e6 100644 --- a/src/odb.c +++ b/src/odb.c @@ -1508,6 +1508,12 @@ void *git_odb_backend_malloc(git_odb_backend *backend, size_t len) return git_odb_backend_data_alloc(backend, len); } +void git_odb_backend_data_free(git_odb_backend *backend, void *data) +{ + GIT_UNUSED(backend); + git__free(data); +} + int git_odb_refresh(struct git_odb *db) { size_t i; -- cgit v1.2.1