Skip to content

Commit 3e8a17b

Browse files
committed
buffer: fix memory leak if unable to grow buffer
If growing a buffer fails, we set its pointer to the static `git_buf__oom` structure. While we correctly free the old pointer if `git__malloc` returned an error, we do not free it if there was an integer overflow while calculating the new allocation size. Fix this issue by freeing the pointer to plug the memory leak.
1 parent 68cfb58 commit 3e8a17b

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/buffer.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,11 @@ int git_buf_try_grow(
7070
new_size = (new_size + 7) & ~7;
7171

7272
if (new_size < buf->size) {
73-
if (mark_oom)
73+
if (mark_oom) {
74+
if (buf->ptr && buf->ptr != git_buf__initbuf)
75+
git__free(buf->ptr);
7476
buf->ptr = git_buf__oom;
77+
}
7578

7679
git_error_set_oom();
7780
return -1;

0 commit comments

Comments
 (0)