Skip to content

Commit 459ac85

Browse files
committed
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.
1 parent 790aae7 commit 459ac85

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

include/git2/sys/odb_backend.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ GIT_EXTERN(int) git_odb_init_backend(
131131
*/
132132
GIT_EXTERN(void *) git_odb_backend_data_alloc(git_odb_backend *backend, size_t len);
133133

134+
/**
135+
* Frees custom allocated ODB data. This should only be called when
136+
* memory allocated using git_odb_backend_data_alloc is not returned
137+
* to libgit2 because the backend encountered an error in the read
138+
* function after allocation and did not return this data to libgit2.
139+
*
140+
* @param backend the ODB backend that is freeing this memory
141+
* @param data the buffer to free
142+
*/
143+
GIT_EXTERN(void) git_odb_backend_data_free(git_odb_backend *backend, void *data);
144+
134145

135146
/*
136147
* Users can avoid deprecated functions by defining `GIT_DEPRECATE_HARD`.

src/odb.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,12 @@ void *git_odb_backend_malloc(git_odb_backend *backend, size_t len)
15081508
return git_odb_backend_data_alloc(backend, len);
15091509
}
15101510

1511+
void git_odb_backend_data_free(git_odb_backend *backend, void *data)
1512+
{
1513+
GIT_UNUSED(backend);
1514+
git__free(data);
1515+
}
1516+
15111517
int git_odb_refresh(struct git_odb *db)
15121518
{
15131519
size_t i;

0 commit comments

Comments
 (0)