reflog.c: fix memory leaks Signed-off-by: schu <schu-github@schulog.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
diff --git a/src/reflog.c b/src/reflog.c
index a2dea88..7a056a0 100644
--- a/src/reflog.c
+++ b/src/reflog.c
@@ -102,8 +102,12 @@ static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
git_reflog_entry *entry;
#define seek_forward(_increase) { \
- if (_increase >= buf_size) \
+ if (_increase >= buf_size) { \
+ if (entry->committer) \
+ free(entry->committer); \
+ free(entry); \
return git__throw(GIT_ERROR, "Failed to seek forward. Buffer size exceeded"); \
+ } \
buf += _increase; \
buf_size -= _increase; \
}
@@ -112,13 +116,18 @@ static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
entry = git__malloc(sizeof(git_reflog_entry));
if (entry == NULL)
return GIT_ENOMEM;
+ entry->committer = NULL;
- if (git_oid_fromstrn(&entry->oid_old, buf, GIT_OID_HEXSZ) < GIT_SUCCESS)
+ if (git_oid_fromstrn(&entry->oid_old, buf, GIT_OID_HEXSZ) < GIT_SUCCESS) {
+ free(entry);
return GIT_ERROR;
+ }
seek_forward(GIT_OID_HEXSZ + 1);
- if (git_oid_fromstrn(&entry->oid_cur, buf, GIT_OID_HEXSZ) < GIT_SUCCESS)
+ if (git_oid_fromstrn(&entry->oid_cur, buf, GIT_OID_HEXSZ) < GIT_SUCCESS) {
+ free(entry);
return GIT_ERROR;
+ }
seek_forward(GIT_OID_HEXSZ + 1);
ptr = buf;
@@ -128,11 +137,16 @@ static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
seek_forward(1);
entry->committer = git__malloc(sizeof(git_signature));
- if (entry->committer == NULL)
+ if (entry->committer == NULL) {
+ free(entry);
return GIT_ENOMEM;
+ }
- if ((error = git_signature__parse(entry->committer, &ptr, buf + 1, NULL, *buf)) < GIT_SUCCESS)
- goto cleanup;
+ if ((error = git_signature__parse(entry->committer, &ptr, buf + 1, NULL, *buf)) < GIT_SUCCESS) {
+ free(entry->committer);
+ free(entry);
+ return git__rethrow(error, "Failed to parse reflog. Could not parse signature");
+ }
if (*buf == '\t') {
/* We got a message. Read everything till we reach LF. */
@@ -150,12 +164,11 @@ static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
seek_forward(1);
if ((error = git_vector_insert(&log->entries, entry)) < GIT_SUCCESS)
- goto cleanup;
+ return git__rethrow(error, "Failed to parse reflog. Could not add new entry");
}
#undef seek_forward
-cleanup:
return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to parse reflog");
}