Merge pull request #375 from schu/cleanup cleanup: some nitpicking and missing free's.
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
diff --git a/src/config_file.c b/src/config_file.c
index 044a243..837d42d 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;
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");
}
diff --git a/src/refs.c b/src/refs.c
index ecd53a6..77521bc 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;
}
diff --git a/src/tsort.c b/src/tsort.c
index bf8bff9..14b15c2 100644
--- a/src/tsort.c
+++ b/src/tsort.c
@@ -1,4 +1,5 @@
-#include <common.h>
+
+#include "common.h"
/**
* An array-of-pointers implementation of Python's Timsort
diff --git a/src/unix/posix.h b/src/unix/posix.h
index 16daf15..8eebbd4 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 <fnmatch.h>
#define p_lstat(p,b) lstat(p,b)
diff --git a/src/util.h b/src/util.h
index 18929af..f70bfe7 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;
}