Commit c94bc192e36f7a13cda8496d5e8e70ad8cbb967c

Vicent Martí 2011-08-17T17:33:44

Merge pull request #375 from schu/cleanup cleanup: some nitpicking and missing free's.

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, &current_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;
 }