Commit 3f0358604e48432b53abf097aa3ab6a1e3639813

Vicent Martí 2012-06-07T22:43:03

misc: Fix warnings from PVS Studio trial

diff --git a/include/git2/diff.h b/include/git2/diff.h
index a0e8ad6..46b80d8 100644
--- a/include/git2/diff.h
+++ b/include/git2/diff.h
@@ -96,9 +96,9 @@ typedef enum {
 typedef struct {
 	git_oid oid;
 	char *path;
-	uint16_t mode;
 	git_off_t size;
 	unsigned int flags;
+	uint16_t mode;
 } git_diff_file;
 
 /**
diff --git a/src/attr.c b/src/attr.c
index fb66511..6fbd005 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -415,7 +415,7 @@ int git_attr_cache__push_file(
 	if (parse && (error = parse(repo, content, file)) < 0)
 		goto finish;
 
-	git_strmap_insert(cache->files, file->key, file, error);
+	git_strmap_insert(cache->files, file->key, file, error); //-V595
 	if (error > 0)
 		error = 0;
 
diff --git a/src/buffer.c b/src/buffer.c
index 783a36e..04aaec3 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -144,8 +144,9 @@ int git_buf_puts(git_buf *buf, const char *string)
 int git_buf_vprintf(git_buf *buf, const char *format, va_list ap)
 {
 	int len;
+	const size_t expected_size = buf->size + (strlen(format) * 2);
 
-	ENSURE_SIZE(buf, buf->size + (strlen(format) * 2));
+	ENSURE_SIZE(buf, expected_size);
 
 	while (1) {
 		va_list args;
diff --git a/src/cache.c b/src/cache.c
index 31da3c3..f8d8940 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -69,7 +69,7 @@ void *git_cache_try_store(git_cache *cache, void *_entry)
 	git_cached_obj *entry = _entry;
 	uint32_t hash;
 
-	memcpy(&hash, &entry->oid, sizeof(hash));
+	memcpy(&hash, &entry->oid, sizeof(uint32_t));
 
 	/* increase the refcount on this object, because
 	 * the cache now owns it */
diff --git a/src/date.c b/src/date.c
index 658b09e..f0e637a 100644
--- a/src/date.c
+++ b/src/date.c
@@ -531,7 +531,7 @@ static int parse_date_basic(const char *date, git_time_t *timestamp, int *offset
 	/* mktime uses local timezone */
 	*timestamp = tm_to_time_t(&tm);
 	if (*offset == -1)
-		*offset = ((time_t)*timestamp - mktime(&tm)) / 60;
+		*offset = (int)((time_t)*timestamp - mktime(&tm)) / 60;
 
 	if (*timestamp == (git_time_t)-1)
 		return -1;
diff --git a/src/diff_output.c b/src/diff_output.c
index 5ffa641..1c65e1b 100644
--- a/src/diff_output.c
+++ b/src/diff_output.c
@@ -467,7 +467,7 @@ static char pick_suffix(int mode)
 {
 	if (S_ISDIR(mode))
 		return '/';
-	else if (mode & 0100)
+	else if (mode & 0100) //-V536
 		/* in git, modes are very regular, so we must have 0100755 mode */
 		return '*';
 	else
diff --git a/src/indexer.c b/src/indexer.c
index f0e0a63..5ae66c9 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -292,11 +292,13 @@ int git_indexer_stream_add(git_indexer_stream *idx, const void *data, size_t siz
 {
 	int error;
 	struct git_pack_header hdr;
-	size_t processed = stats->processed;
+	size_t processed; 
 	git_mwindow_file *mwf = &idx->pack->mwf;
 
 	assert(idx && data && stats);
 
+	processed = stats->processed;
+
 	if (git_filebuf_write(&idx->pack_file, data, size) < 0)
 		return -1;
 
diff --git a/src/notes.c b/src/notes.c
index 84ad940..e87ea65 100644
--- a/src/notes.c
+++ b/src/notes.c
@@ -125,7 +125,7 @@ static int note_write(git_oid *out, git_repository *repo,
 		return error;
 	}
 
-	error = git_treebuilder_insert(&entry, tb, target + fanout, &oid, 0100644);
+	error = git_treebuilder_insert(&entry, tb, target + fanout, &oid, 0100644); //-V536
 	if (error < 0) {
 		/* libgit2 doesn't support object removal (gc) yet */
 		/* we leave an orphaned blob object behind - TODO */
@@ -154,7 +154,7 @@ static int note_write(git_oid *out, git_repository *repo,
 		if (error < 0)
 			return error;
 
-		error = git_treebuilder_insert(NULL, tb, subtree, &oid, 0040000);
+		error = git_treebuilder_insert(NULL, tb, subtree, &oid, 0040000); //-V536
 		if (error < 0) {
 			git_treebuilder_free(tb);
 			return error;
diff --git a/src/remote.c b/src/remote.c
index 8d60761..00e108a 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -337,13 +337,16 @@ int git_remote_update_tips(git_remote *remote, int (*cb)(const char *refname, co
 	unsigned int i = 0;
 	git_buf refname = GIT_BUF_INIT;
 	git_oid old;
-	git_vector *refs = &remote->refs;
+	git_vector *refs;
 	git_remote_head *head;
 	git_reference *ref;
-	struct git_refspec *spec = &remote->fetch;
+	struct git_refspec *spec;
 
 	assert(remote);
 
+	refs = &remote->refs;
+	spec = &remote->fetch;
+
 	if (refs->length == 0)
 		return 0;
 
diff --git a/src/revparse.c b/src/revparse.c
index dd8476e..1e6b710 100644
--- a/src/revparse.c
+++ b/src/revparse.c
@@ -506,7 +506,7 @@ static int handle_linear_syntax(git_object **out, git_object *obj, const char *m
    }
 
    /* "~" is the same as "~1" */
-   if (strlen(movement) == 0) {
+   if (*movement == '\0') {
       n = 1;
    } else {
       git__strtol32(&n, movement, NULL, 0);
diff --git a/src/tree-cache.c b/src/tree-cache.c
index ebc2c68..8d186d2 100644
--- a/src/tree-cache.c
+++ b/src/tree-cache.c
@@ -69,7 +69,7 @@ const git_tree_cache *git_tree_cache_get(const git_tree_cache *tree, const char 
 			return NULL;
 		}
 
-		if (end == NULL || end + 1 == '\0')
+		if (end == NULL || *end + 1 == '\0')
 			return tree;
 
 		ptr = end + 1;
diff --git a/src/tree.c b/src/tree.c
index 92b1b1e..d575dc8 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -22,7 +22,7 @@ static int valid_attributes(const int attributes)
 
 static int valid_entry_name(const char *filename)
 {
-	return strlen(filename) > 0 && strchr(filename, '/') == NULL;
+	return *filename != '\0' && strchr(filename, '/') == NULL;
 }
 
 static int entry_sort_cmp(const void *a, const void *b)
diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c
index f0441df..37956af 100644
--- a/src/win32/posix_w32.c
+++ b/src/win32/posix_w32.c
@@ -414,7 +414,7 @@ int p_mkstemp(char *tmp_path)
 		return -1;
 #endif
 
-	return p_creat(tmp_path, 0744);
+	return p_creat(tmp_path, 0744); //-V536
 }
 
 int p_setenv(const char* name, const char* value, int overwrite)
diff --git a/tests-clar/core/filebuf.c b/tests-clar/core/filebuf.c
index eab8a26..4451c01 100644
--- a/tests-clar/core/filebuf.c
+++ b/tests-clar/core/filebuf.c
@@ -8,7 +8,7 @@ void test_core_filebuf__0(void)
 	int fd;
 	char test[] = "test", testlock[] = "test.lock";
 
-	fd = p_creat(testlock, 0744);
+	fd = p_creat(testlock, 0744); //-V536
 
 	cl_must_pass(fd);
 	cl_must_pass(p_close(fd));
@@ -27,7 +27,7 @@ void test_core_filebuf__1(void)
 	int fd;
 	char test[] = "test";
 
-	fd = p_creat(test, 0666);
+	fd = p_creat(test, 0666); //-V536
 	cl_must_pass(fd);
 	cl_must_pass(p_write(fd, "libgit2 rocks\n", 14));
 	cl_must_pass(p_close(fd));
diff --git a/tests-clar/index/tests.c b/tests-clar/index/tests.c
index 3436f8d..a535d68 100644
--- a/tests-clar/index/tests.c
+++ b/tests-clar/index/tests.c
@@ -33,7 +33,7 @@ static void copy_file(const char *src, const char *dst)
 
 	cl_git_pass(git_futils_readbuffer(&source_buf, src));
 
-	dst_fd = git_futils_creat_withpath(dst, 0777, 0666);
+	dst_fd = git_futils_creat_withpath(dst, 0777, 0666); //-V536
 	if (dst_fd < 0)
 		goto cleanup;
 
diff --git a/tests-clar/object/tree/write.c b/tests-clar/object/tree/write.c
index 3911f6f..e6dc549 100644
--- a/tests-clar/object/tree/write.c
+++ b/tests-clar/object/tree/write.c
@@ -63,14 +63,14 @@ void test_object_tree_write__subtree(void)
 
 	//create subtree
 	cl_git_pass(git_treebuilder_create(&builder, NULL));
-	cl_git_pass(git_treebuilder_insert(NULL,builder,"new.txt",&bid,0100644));
+	cl_git_pass(git_treebuilder_insert(NULL,builder,"new.txt",&bid,0100644)); //-V536
 	cl_git_pass(git_treebuilder_write(&subtree_id, g_repo, builder));
 	git_treebuilder_free(builder);
 
 	// create parent tree
 	cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
 	cl_git_pass(git_treebuilder_create(&builder, tree));
-	cl_git_pass(git_treebuilder_insert(NULL,builder,"new",&subtree_id,040000));
+	cl_git_pass(git_treebuilder_insert(NULL,builder,"new",&subtree_id,040000)); //-V536
 	cl_git_pass(git_treebuilder_write(&id_hiearar, g_repo, builder));
 	git_treebuilder_free(builder);
 	git_tree_free(tree);