Commit e953c1606d0d7aea680c9b19db0b955b34ae63c2

Vicent Martí 2013-03-14T16:11:31

Merge pull request #1414 from arrbee/more-build-warning-fixes Fix various build warnings

diff --git a/src/refs.c b/src/refs.c
index 80307c9..41c6fd8 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -39,10 +39,13 @@ git_reference *git_reference__alloc(
 	const char *symbolic)
 {
 	git_reference *ref;
+	size_t namelen;
 
 	assert(refdb && name && ((oid && !symbolic) || (!oid && symbolic)));
-	
-	if ((ref = git__calloc(1, sizeof(git_reference) + strlen(name) + 1)) == NULL)
+
+	namelen = strlen(name);
+
+	if ((ref = git__calloc(1, sizeof(git_reference) + namelen + 1)) == NULL)
 		return NULL;
 
 	if (oid) {
@@ -51,13 +54,15 @@ git_reference *git_reference__alloc(
 	} else {
 		ref->type = GIT_REF_SYMBOLIC;
 
-		if ((ref->target.symbolic = git__strdup(symbolic)) == NULL)
+		if ((ref->target.symbolic = git__strdup(symbolic)) == NULL) {
+			git__free(ref);
 			return NULL;
+		}
 	}
-	
+
 	ref->db = refdb;
-	strcpy(ref->name, name);
-	
+	memcpy(ref->name, name, namelen + 1);
+
 	return ref;
 }
 
@@ -70,7 +75,7 @@ void git_reference_free(git_reference *reference)
 		git__free(reference->target.symbolic);
 		reference->target.symbolic = NULL;
 	}
-	
+
 	reference->db = NULL;
 	reference->type = GIT_REF_INVALID;
 
diff --git a/src/transports/winhttp.c b/src/transports/winhttp.c
index 970fa53..d4d0179 100644
--- a/src/transports/winhttp.c
+++ b/src/transports/winhttp.c
@@ -560,11 +560,11 @@ static int winhttp_stream_write_single(
 	return 0;
 }
 
-static int put_uuid_string(LPWSTR buffer, DWORD buffer_len_cch)
+static int put_uuid_string(LPWSTR buffer, size_t buffer_len_cch)
 {
 	UUID uuid;
 	RPC_STATUS status = UuidCreate(&uuid);
-	int result;
+	HRESULT result;
 
 	if (RPC_S_OK != status &&
 		RPC_S_UUID_LOCAL_ONLY != status &&
@@ -573,17 +573,19 @@ static int put_uuid_string(LPWSTR buffer, DWORD buffer_len_cch)
 		return -1;
 	}
 
-	if (buffer_len_cch < (UUID_LENGTH_CCH + 1)) {
-		giterr_set(GITERR_NET, "Buffer insufficient to generate temp file name");
+	if (buffer_len_cch < UUID_LENGTH_CCH + 1) {
+		giterr_set(GITERR_NET, "Buffer too small for name of temp file");
 		return -1;
 	}
 
-	result = wsprintfW(buffer, L"%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x",
+	result = StringCbPrintfW(
+		buffer, buffer_len_cch,
+		L"%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x",
 		uuid.Data1, uuid.Data2, uuid.Data3,
 		uuid.Data4[0], uuid.Data4[1], uuid.Data4[2], uuid.Data4[3],
 		uuid.Data4[4], uuid.Data4[5], uuid.Data4[6], uuid.Data4[7]);
 
-	if (result != UUID_LENGTH_CCH) {
+	if (FAILED(result)) {
 		giterr_set(GITERR_OS, "Unable to generate name for temp file");
 		return -1;
 	}
@@ -602,17 +604,10 @@ static int get_temp_file(LPWSTR buffer, DWORD buffer_len_cch)
 
 	len = wcslen(buffer);
 
-	/* 1 prefix character for the backslash, 1 postfix for
-	 * the null terminator */
-	if (buffer_len_cch - len < 1 + UUID_LENGTH_CCH + 1) {
-		giterr_set(GITERR_NET, "Buffer insufficient to generate temp file name");
-		return -1;
-	}
-
-	if (buffer[len - 1] != '\\')
+	if (buffer[len - 1] != '\\' && len < buffer_len_cch)
 		buffer[len++] = '\\';
 
-	if (put_uuid_string(&buffer[len], UUID_LENGTH_CCH + 1) < 0)
+	if (put_uuid_string(&buffer[len], (size_t)buffer_len_cch - len) < 0)
 		return -1;
 
 	return 0;
diff --git a/tests-clar/commit/parse.c b/tests-clar/commit/parse.c
index 37f27b0..95c6285 100644
--- a/tests-clar/commit/parse.c
+++ b/tests-clar/commit/parse.c
@@ -155,7 +155,7 @@ void test_commit_parse__signature(void)
       cl_git_pass(git_signature__parse(&person, &str, str + len, passcase->header, '\n'));
       cl_assert_equal_s(passcase->name, person.name);
       cl_assert_equal_s(passcase->email, person.email);
-      cl_assert_equal_i(passcase->time, person.when.time);
+      cl_assert_equal_i((int)passcase->time, (int)person.when.time);
       cl_assert_equal_i(passcase->offset, person.when.offset);
       git__free(person.name); git__free(person.email);
    }
diff --git a/tests-clar/refs/pack.c b/tests-clar/refs/pack.c
index 8da36cc..973abae 100644
--- a/tests-clar/refs/pack.c
+++ b/tests-clar/refs/pack.c
@@ -19,10 +19,10 @@ void test_refs_pack__cleanup(void)
    cl_git_sandbox_cleanup();
 }
 
-void packall()
+static void packall(void)
 {
 	git_refdb *refdb;
-	
+
 	cl_git_pass(git_repository_refdb(&refdb, g_repo));
 	cl_git_pass(git_refdb_compress(refdb));
 }
diff --git a/tests-clar/trace/trace.c b/tests-clar/trace/trace.c
index 712fe62..cc99cd1 100644
--- a/tests-clar/trace/trace.c
+++ b/tests-clar/trace/trace.c
@@ -5,6 +5,8 @@ static int written = 0;
 
 static void trace_callback(git_trace_level_t level, const char *message)
 {
+	GIT_UNUSED(level);
+
 	cl_assert(strcmp(message, "Hello world!") == 0);
 
 	written = 1;