From 7fade6c63a3b1bb5cab1a312d81b2b1d4b3321f2 Mon Sep 17 00:00:00 2001 From: schu Date: Wed, 17 Aug 2011 12:14:12 +0200 Subject: [PATCH 1/6] unix/posix.h: remove redundant include Signed-off-by: schu --- src/unix/posix.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/unix/posix.h b/src/unix/posix.h index 16daf15bd..8eebbd4d8 100644 --- a/src/unix/posix.h +++ b/src/unix/posix.h @@ -1,7 +1,6 @@ #ifndef INCLUDE_posix__w32_h__ #define INCLUDE_posix__w32_h__ -#include "common.h" #include #define p_lstat(p,b) lstat(p,b) From b6817692a6d1eb617bda96a64c66c8e02988fc12 Mon Sep 17 00:00:00 2001 From: schu Date: Wed, 17 Aug 2011 12:14:47 +0200 Subject: [PATCH 2/6] tsort.c: fix include of common.h Signed-off-by: schu --- src/tsort.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/tsort.c b/src/tsort.c index bf8bff9d8..14b15c232 100644 --- a/src/tsort.c +++ b/src/tsort.c @@ -1,4 +1,5 @@ -#include + +#include "common.h" /** * An array-of-pointers implementation of Python's Timsort From 5a0659fe3ba020181a07464c4c92b06a5c21b43a Mon Sep 17 00:00:00 2001 From: schu Date: Wed, 17 Aug 2011 14:05:41 +0200 Subject: [PATCH 3/6] config_file.c: fix memory leaks Signed-off-by: schu --- src/config_file.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/config_file.c b/src/config_file.c index 044a24310..837d42dbf 100644 --- a/src/config_file.c +++ b/src/config_file.c @@ -212,8 +212,10 @@ static int cvar_normalize_name(cvar_t *var, char **output) /* If there aren't any spaces in the section, it's easy */ if (section_sp == NULL) { ret = snprintf(name, len + 1, "%s.%s", var->section, var->name); - if (ret < 0) + if (ret < 0) { + free(name); return git__throw(GIT_EOSERR, "Failed to normalize name. OS err: %s", strerror(errno)); + } *output = name; return GIT_SUCCESS; @@ -701,12 +703,16 @@ static int parse_section_header(diskfile_backend *cfg, char **section_out) /* find the end of the variable's name */ name_end = strchr(line, ']'); - if (name_end == NULL) + if (name_end == NULL) { + free(line); return git__throw(GIT_EOBJCORRUPTED, "Failed to parse header. Can't find header name end"); + } name = (char *)git__malloc((size_t)(name_end - line) + 1); - if (name == NULL) + if (name == NULL) { + free(line); return GIT_ENOMEM; + } name_length = 0; pos = 0; @@ -738,8 +744,10 @@ static int parse_section_header(diskfile_backend *cfg, char **section_out) } while ((c = line[pos++]) != ']'); - if (line[pos - 1] != ']') - return git__throw(GIT_EOBJCORRUPTED, "Failed to parse header. Config file ended unexpectedly"); + if (line[pos - 1] != ']') { + error = git__throw(GIT_EOBJCORRUPTED, "Failed to parse header. Config file ended unexpectedly"); + goto error; + } name[name_length] = 0; free(line); @@ -957,7 +965,8 @@ static int config_write(diskfile_backend *cfg, cvar_t *var) * default case will take care of updating them. */ pre_end = post_start = cfg->reader.read_ptr; - free(current_section); + if (current_section) + free(current_section); error = parse_section_header(cfg, ¤t_section); if (error < GIT_SUCCESS) break; From 31e5909214cbfba0c43143288a77f2fc067004f7 Mon Sep 17 00:00:00 2001 From: schu Date: Wed, 17 Aug 2011 15:20:43 +0200 Subject: [PATCH 4/6] git__strndup: immediately return NULL when ENOMEM Signed-off-by: schu --- src/util.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/util.h b/src/util.h index 18929af8e..f70bfe743 100644 --- a/src/util.h +++ b/src/util.h @@ -47,11 +47,13 @@ GIT_INLINE(char *) git__strndup(const char *str, size_t n) length = n; ptr = (char*)malloc(length + 1); - if (!ptr) + if (!ptr) { git__throw(GIT_ENOMEM, "Out of memory. Failed to duplicate string"); + return NULL; + } memcpy(ptr, str, length); - ptr[length] = 0; + ptr[length] = '\0'; return ptr; } From e7a3b3171bcec5d1ec8b2d94e0c7d9c66468a7f2 Mon Sep 17 00:00:00 2001 From: schu Date: Wed, 17 Aug 2011 15:50:19 +0200 Subject: [PATCH 5/6] reflog.c: fix memory leaks Signed-off-by: schu --- src/reflog.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/reflog.c b/src/reflog.c index a2dea8830..7a056a056 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"); } From d4958b88483a0ac040e3c15392566ae0d398e2da Mon Sep 17 00:00:00 2001 From: schu Date: Wed, 17 Aug 2011 15:58:03 +0200 Subject: [PATCH 6/6] refs.c: remove two lines of dead code Signed-off-by: schu --- src/refs.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/refs.c b/src/refs.c index ecd53a69a..77521bc63 100644 --- a/src/refs.c +++ b/src/refs.c @@ -1659,8 +1659,6 @@ static int check_valid_ref_char(char ch) case '[': case '*': return GIT_ERROR; - break; - default: return GIT_SUCCESS; }