Commit 10c06114cbb1c384b7de3cca6d6601ee750f5178

Arkadiy Shapkin 2013-03-17T04:46:46

Several warnings detected by static code analyzer fixed Implicit type conversion argument of function to size_t type Suspicious sequence of types castings: size_t -> int -> size_t Consider reviewing the expression of the 'A = B == C' kind. The expression is calculated as following: 'A = (B == C)' Unsigned type is never < 0

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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
diff --git a/deps/regex/regex_internal.c b/deps/regex/regex_internal.c
index 193854c..ad57c20 100644
--- a/deps/regex/regex_internal.c
+++ b/deps/regex/regex_internal.c
@@ -32,7 +32,7 @@ static re_dfastate_t *create_cd_newstate (const re_dfa_t *dfa,
 
 #ifdef GAWK
 #undef MAX	/* safety */
-static int
+static size_t
 MAX(size_t a, size_t b)
 {
 	return (a > b ? a : b);
diff --git a/src/branch.c b/src/branch.c
index 6b289b1..45ecca7 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -176,7 +176,7 @@ int git_branch_move(
 		return not_a_local_branch(git_reference_name(branch));
 
 	if ((error = git_buf_joinpath(&new_reference_name, GIT_REFS_HEADS_DIR, new_branch_name)) < 0 ||
-		(error = git_buf_printf(&old_config_section, "branch.%s", git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR)) < 0) ||
+		(error = git_buf_printf(&old_config_section, "branch.%s", git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR))) < 0 ||
 		(error = git_buf_printf(&new_config_section, "branch.%s", new_branch_name)) < 0)
 		goto done;
 
diff --git a/src/commit.c b/src/commit.c
index 7a356c5..e2d3d34 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -20,7 +20,7 @@
 
 static void clear_parents(git_commit *commit)
 {
-	unsigned int i;
+	size_t i;
 
 	for (i = 0; i < commit->parent_ids.length; ++i) {
 		git_oid *parent = git_vector_get(&commit->parent_ids, i);
diff --git a/src/config.c b/src/config.c
index d6aa307..99b519d 100644
--- a/src/config.c
+++ b/src/config.c
@@ -36,7 +36,7 @@ static void file_internal_free(file_internal *internal)
 
 static void config_free(git_config *cfg)
 {
-	unsigned int i;
+	size_t i;
 	file_internal *internal;
 
 	for(i = 0; i < cfg->files.length; ++i){
@@ -284,7 +284,7 @@ int git_config_add_backend(
 int git_config_refresh(git_config *cfg)
 {
 	int error = 0;
-	unsigned int i;
+	size_t i;
 
 	for (i = 0; i < cfg->files.length && !error; ++i) {
 		file_internal *internal = git_vector_get(&cfg->files, i);
@@ -312,7 +312,7 @@ int git_config_foreach_match(
 	void *payload)
 {
 	int ret = 0;
-	unsigned int i;
+	size_t i;
 	file_internal *internal;
 	git_config_backend *file;
 
diff --git a/src/diff_output.c b/src/diff_output.c
index c0aff68..b938cc0 100644
--- a/src/diff_output.c
+++ b/src/diff_output.c
@@ -390,7 +390,7 @@ static int get_workdir_content(
 		map->data = git__malloc(alloc_len);
 		GITERR_CHECK_ALLOC(map->data);
 
-		read_len = p_readlink(path.ptr, map->data, (int)alloc_len);
+		read_len = p_readlink(path.ptr, map->data, alloc_len);
 		if (read_len < 0) {
 			giterr_set(GITERR_OS, "Failed to read symlink '%s'", file->path);
 			error = -1;
diff --git a/src/fileops.c b/src/fileops.c
index c1824e8..e33377e 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -529,7 +529,7 @@ int git_futils_cleanupdir_r(const char *path)
 	git_buf fullpath = GIT_BUF_INIT;
 	futils__rmdir_data data;
 
-	if ((error = git_buf_put(&fullpath, path, strlen(path)) < 0))
+	if ((error = git_buf_put(&fullpath, path, strlen(path))) < 0)
 		goto clean_up;
 
 	data.base    = "";
diff --git a/src/filter.c b/src/filter.c
index f0bfb79..9f749dc 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -48,7 +48,8 @@ void git_filters_free(git_vector *filters)
 
 int git_filters_apply(git_buf *dest, git_buf *source, git_vector *filters)
 {
-	unsigned int i, src;
+	size_t i;
+	unsigned int src;
 	git_buf *dbuffer[2];
 
 	dbuffer[0] = source;
diff --git a/src/index.c b/src/index.c
index 1ca3b16..6290ec4 100644
--- a/src/index.c
+++ b/src/index.c
@@ -317,7 +317,7 @@ void git_index_free(git_index *index)
 
 void git_index_clear(git_index *index)
 {
-	unsigned int i;
+	size_t i;
 
 	assert(index);
 
@@ -786,7 +786,7 @@ int git_index_remove(git_index *index, const char *path, int stage)
 	if (entry != NULL)
 		git_tree_cache_invalidate_path(index->tree, entry->path);
 
-	error = git_vector_remove(&index->entries, (unsigned int)position);
+	error = git_vector_remove(&index->entries, position);
 
 	if (!error)
 		index_entry_free(entry);
@@ -1129,7 +1129,7 @@ int git_index_reuc_remove(git_index *index, size_t position)
 	git_vector_sort(&index->reuc);
 
 	reuc = git_vector_get(&index->reuc, position);
-	error = git_vector_remove(&index->reuc, (unsigned int)position);
+	error = git_vector_remove(&index->reuc, position);
 
 	if (!error)
 		index_entry_reuc_free(reuc);
diff --git a/src/indexer.c b/src/indexer.c
index c7e142b..2cfbd3a 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -415,6 +415,8 @@ int git_indexer_stream_add(git_indexer_stream *idx, const void *data, size_t siz
 	}
 
 	if (!idx->parsed_header) {
+		unsigned int total_objects;
+
 		if ((unsigned)idx->pack->mwf.size < sizeof(hdr))
 			return 0;
 
@@ -427,20 +429,24 @@ int git_indexer_stream_add(git_indexer_stream *idx, const void *data, size_t siz
 
 		/* for now, limit to 2^32 objects */
 		assert(idx->nr_objects == (size_t)((unsigned int)idx->nr_objects));
+		if (idx->nr_objects == (size_t)((unsigned int)idx->nr_objects))
+			total_objects = (unsigned int)idx->nr_objects;
+		else
+			total_objects = UINT_MAX;
 
 		idx->pack->idx_cache = git_oidmap_alloc();
 		GITERR_CHECK_ALLOC(idx->pack->idx_cache);
 
 		idx->pack->has_cache = 1;
-		if (git_vector_init(&idx->objects, (unsigned int)idx->nr_objects, objects_cmp) < 0)
+		if (git_vector_init(&idx->objects, total_objects, objects_cmp) < 0)
 			return -1;
 
-		if (git_vector_init(&idx->deltas, (unsigned int)(idx->nr_objects / 2), NULL) < 0)
+		if (git_vector_init(&idx->deltas, total_objects / 2, NULL) < 0)
 			return -1;
 
 		stats->received_objects = 0;
 		processed = stats->indexed_objects = 0;
-		stats->total_objects = (unsigned int)idx->nr_objects;
+		stats->total_objects = total_objects;
 		do_progress_callback(idx, stats);
 	}
 
diff --git a/src/mwindow.c b/src/mwindow.c
index cb2ef78..b35503d 100644
--- a/src/mwindow.c
+++ b/src/mwindow.c
@@ -33,7 +33,7 @@ static git_mwindow_ctl mem_ctl;
 void git_mwindow_free_all(git_mwindow_file *mwf)
 {
 	git_mwindow_ctl *ctl = &mem_ctl;
-	unsigned int i;
+	size_t i;
 
 	if (git_mutex_lock(&git__mwindow_mutex)) {
 		giterr_set(GITERR_THREAD, "unable to lock mwindow mutex");
@@ -115,7 +115,7 @@ static void git_mwindow_scan_lru(
 static int git_mwindow_close_lru(git_mwindow_file *mwf)
 {
 	git_mwindow_ctl *ctl = &mem_ctl;
-	unsigned int i;
+	size_t i;
 	git_mwindow *lru_w = NULL, *lru_l = NULL, **list = &mwf->windows;
 
 	/* FIXME: Does this give us any advantage? */
@@ -288,7 +288,7 @@ void git_mwindow_file_deregister(git_mwindow_file *mwf)
 {
 	git_mwindow_ctl *ctl = &mem_ctl;
 	git_mwindow_file *cur;
-	unsigned int i;
+	size_t i;
 
 	if (git_mutex_lock(&git__mwindow_mutex))
 		return;
diff --git a/src/odb.c b/src/odb.c
index 24381e7..5a0d887 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -499,7 +499,7 @@ int git_odb_open(git_odb **out, const char *objects_dir)
 
 static void odb_free(git_odb *db)
 {
-	unsigned int i;
+	size_t i;
 
 	for (i = 0; i < db->backends.length; ++i) {
 		backend_internal *internal = git_vector_get(&db->backends, i);
@@ -527,7 +527,7 @@ void git_odb_free(git_odb *db)
 int git_odb_exists(git_odb *db, const git_oid *id)
 {
 	git_odb_object *object;
-	unsigned int i;
+	size_t i;
 	bool found = false;
 	bool refreshed = false;
 
@@ -577,7 +577,7 @@ int git_odb__read_header_or_object(
 	git_odb_object **out, size_t *len_p, git_otype *type_p,
 	git_odb *db, const git_oid *id)
 {
-	unsigned int i;
+	size_t i;
 	int error = GIT_ENOTFOUND;
 	git_odb_object *object;
 
@@ -619,7 +619,7 @@ int git_odb__read_header_or_object(
 
 int git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id)
 {
-	unsigned int i;
+	size_t i;
 	int error;
 	bool refreshed = false;
 	git_rawobj raw;
@@ -664,7 +664,7 @@ attempt_lookup:
 int git_odb_read_prefix(
 	git_odb_object **out, git_odb *db, const git_oid *short_id, size_t len)
 {
-	unsigned int i;
+	size_t i;
 	int error = GIT_ENOTFOUND;
 	git_oid found_full_oid = {{0}};
 	git_rawobj raw;
@@ -743,7 +743,7 @@ int git_odb_foreach(git_odb *db, git_odb_foreach_cb cb, void *payload)
 int git_odb_write(
 	git_oid *oid, git_odb *db, const void *data, size_t len, git_otype type)
 {
-	unsigned int i;
+	size_t i;
 	int error = GIT_ERROR;
 	git_odb_stream *stream;
 
@@ -785,7 +785,7 @@ int git_odb_write(
 int git_odb_open_wstream(
 	git_odb_stream **stream, git_odb *db, size_t size, git_otype type)
 {
-	unsigned int i;
+	size_t i;
 	int error = GIT_ERROR;
 
 	assert(stream && db);
@@ -812,7 +812,7 @@ int git_odb_open_wstream(
 
 int git_odb_open_rstream(git_odb_stream **stream, git_odb *db, const git_oid *oid)
 {
-	unsigned int i;
+	size_t i;
 	int error = GIT_ERROR;
 
 	assert(stream && db);
@@ -833,7 +833,7 @@ int git_odb_open_rstream(git_odb_stream **stream, git_odb *db, const git_oid *oi
 
 int git_odb_write_pack(struct git_odb_writepack **out, git_odb *db, git_transfer_progress_callback progress_cb, void *progress_payload)
 {
-	unsigned int i;
+	size_t i;
 	int error = GIT_ERROR;
 
 	assert(out && db);
@@ -864,7 +864,7 @@ void *git_odb_backend_malloc(git_odb_backend *backend, size_t len)
 
 int git_odb_refresh(struct git_odb *db)
 {
-	unsigned int i;
+	size_t i;
 	assert(db);
 
 	for (i = 0; i < db->backends.length; ++i) {
diff --git a/src/odb_pack.c b/src/odb_pack.c
index 7e6828e..7240a4a 100644
--- a/src/odb_pack.c
+++ b/src/odb_pack.c
@@ -195,7 +195,7 @@ static int packfile_load__cb(void *_data, git_buf *path)
 	struct pack_backend *backend = (struct pack_backend *)_data;
 	struct git_pack_file *pack;
 	int error;
-	unsigned int i;
+	size_t i;
 
 	if (git__suffixcmp(path->ptr, ".idx") != 0)
 		return 0; /* not an index */
@@ -222,7 +222,7 @@ static int pack_entry_find_inner(
 	const git_oid *oid,
 	struct git_pack_file *last_found)
 {
-	unsigned int i;
+	size_t i;
 
 	if (last_found &&
 		git_pack_entry_find(e, last_found, oid, GIT_OID_HEXSZ) == 0)
@@ -266,7 +266,7 @@ static unsigned pack_entry_find_prefix_inner(
 		struct git_pack_file *last_found)
 {
 	int error;
-	unsigned int i;
+	size_t i;
 	unsigned found = 0;
 
 	if (last_found) {
@@ -510,7 +510,7 @@ static int pack_backend__writepack(struct git_odb_writepack **out,
 static void pack_backend__free(git_odb_backend *_backend)
 {
 	struct pack_backend *backend;
-	unsigned int i;
+	size_t i;
 
 	assert(_backend);
 
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index 5f5d42f..f00bd72 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -769,7 +769,7 @@ static int packed_remove_loose(
 	refdb_fs_backend *backend,
 	git_vector *packing_list)
 {
-	unsigned int i;
+	size_t i;
 	git_buf full_path = GIT_BUF_INIT;
 	int failed = 0;
 
@@ -811,7 +811,7 @@ static int packed_remove_loose(
 static int packed_write(refdb_fs_backend *backend)
 {
 	git_filebuf pack_file = GIT_FILEBUF_INIT;
-	unsigned int i;
+	size_t i;
 	git_buf pack_file_path = GIT_BUF_INIT;
 	git_vector packing_list;
 	unsigned int total_refs;
diff --git a/src/reflog.c b/src/reflog.c
index 432680b..8c133fe 100644
--- a/src/reflog.c
+++ b/src/reflog.c
@@ -163,7 +163,7 @@ fail:
 
 void git_reflog_free(git_reflog *reflog)
 {
-	unsigned int i;
+	size_t i;
 	git_reflog_entry *entry;
 
 	if (reflog == NULL)
diff --git a/src/refs.c b/src/refs.c
index 41c6fd8..dde2f51 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -440,6 +440,7 @@ int git_reference_rename(
 	git_oid *oid;
 	const char *symbolic;
 	int error = 0;
+	int reference_has_log;
 	
 	*out = NULL;
 
@@ -465,11 +466,13 @@ int git_reference_rename(
 		return -1;
 
 	/* Check if we have to update HEAD. */
-	if ((should_head_be_updated = git_branch_is_head(ref)) < 0)
+	if ((error = git_branch_is_head(ref)) < 0)
 		goto on_error;
 
+	should_head_be_updated = (error > 0);
+
 	/* Now delete the old ref and save the new one. */
-	if (git_refdb_delete(ref->db, ref) < 0)
+	if ((error = git_refdb_delete(ref->db, ref)) < 0)
 		goto on_error;
 	
 	/* Save the new reference. */
@@ -477,14 +480,18 @@ int git_reference_rename(
 		goto rollback;
 	
 	/* Update HEAD it was poiting to the reference being renamed. */
-	if (should_head_be_updated && git_repository_set_head(ref->db->repo, new_name) < 0) {
+	if (should_head_be_updated && (error = git_repository_set_head(ref->db->repo, new_name)) < 0) {
 		giterr_set(GITERR_REFERENCE, "Failed to update HEAD after renaming reference");
 		goto on_error;
 	}
 
 	/* Rename the reflog file, if it exists. */
-	if (git_reference_has_log(ref) &&
-		(error = git_reflog_rename(ref, new_name)) < 0)
+	reference_has_log = git_reference_has_log(ref);
+	if (reference_has_log < 0) {
+		error = reference_has_log;
+		goto on_error;
+	}
+	if (reference_has_log && (error = git_reflog_rename(ref, new_name)) < 0)
 		goto on_error;
 
 	*out = result;
diff --git a/src/remote.c b/src/remote.c
index 21ca6ec..a6f62d6 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -1254,7 +1254,7 @@ static int rename_fetch_refspecs(
 		goto cleanup;
 
 	/* Is it an in-memory remote? */
-	if (remote->name == '\0') {
+	if (!remote->name) {
 		error = (callback(git_buf_cstr(&serialized), payload) < 0) ? GIT_EUSER : 0;
 		goto cleanup;
 	}
diff --git a/src/revparse.c b/src/revparse.c
index 8a45889..1518a7c 100644
--- a/src/revparse.c
+++ b/src/revparse.c
@@ -511,8 +511,8 @@ static int walk_and_search(git_object **out, git_revwalk *walk, regex_t *regex)
 	
 	while (!(error = git_revwalk_next(&oid, walk))) {
 
-		if ((error = git_object_lookup(&obj, git_revwalk_repository(walk), &oid, GIT_OBJ_COMMIT) < 0) &&
-			(error != GIT_ENOTFOUND))
+		error = git_object_lookup(&obj, git_revwalk_repository(walk), &oid, GIT_OBJ_COMMIT);
+		if ((error < 0) && (error != GIT_ENOTFOUND))
 			return -1;
 
 		if (!regexec(regex, git_commit_message((git_commit*)obj), 0, NULL, 0)) {
@@ -635,7 +635,7 @@ static int extract_how_many(int *n, const char *spec, size_t *pos)
 		} while (spec[(*pos)] == kind && kind == '~');
 
 		if (git__isdigit(spec[*pos])) {
-			if ((git__strtol32(&parsed, spec + *pos, &end_ptr, 10) < 0) < 0)
+			if (git__strtol32(&parsed, spec + *pos, &end_ptr, 10) < 0)
 				return GIT_EINVALIDSPEC;
 
 			accumulated += (parsed - 1);
diff --git a/src/submodule.c b/src/submodule.c
index c020613..957766d 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -1497,7 +1497,7 @@ static int submodule_wd_status(unsigned int *status, git_submodule *sm)
 			if (untracked > 0)
 				*status |= GIT_SUBMODULE_STATUS_WD_UNTRACKED;
 
-			if ((git_diff_num_deltas(diff) - untracked) > 0)
+			if (git_diff_num_deltas(diff) != untracked)
 				*status |= GIT_SUBMODULE_STATUS_WD_WD_MODIFIED;
 
 			git_diff_list_free(diff);
diff --git a/src/transports/local.c b/src/transports/local.c
index 5ed7b45..ce89bb2 100644
--- a/src/transports/local.c
+++ b/src/transports/local.c
@@ -124,7 +124,7 @@ static int store_refs(transport_local *t)
 	assert(t);
 
 	if (git_reference_list(&ref_names, t->repo, GIT_REF_LISTALL) < 0 ||
-		git_vector_init(&t->refs, (unsigned int)ref_names.count, NULL) < 0)
+		git_vector_init(&t->refs, ref_names.count, NULL) < 0)
 		goto on_error;
 
 	/* Sort the references first */
diff --git a/src/transports/smart.c b/src/transports/smart.c
index e820488..bfcce0c 100644
--- a/src/transports/smart.c
+++ b/src/transports/smart.c
@@ -24,7 +24,7 @@ static int git_smart__recv_cb(gitno_buffer *buf)
 	buf->offset += bytes_read;
 
 	if (t->packetsize_cb)
-		t->packetsize_cb((int)bytes_read, t->packetsize_payload);
+		t->packetsize_cb(bytes_read, t->packetsize_payload);
 
 	return (int)(buf->offset - old_len);
 }
diff --git a/src/transports/smart_protocol.c b/src/transports/smart_protocol.c
index c653ad5..8acedeb 100644
--- a/src/transports/smart_protocol.c
+++ b/src/transports/smart_protocol.c
@@ -454,7 +454,7 @@ int git_smart__download_pack(
 
 		/* We might have something in the buffer already from negotiate_fetch */
 		if (t->buffer.offset > 0)
-			t->packetsize_cb((int)t->buffer.offset, t->packetsize_payload);
+			t->packetsize_cb(t->buffer.offset, t->packetsize_payload);
 	}
 
 	if ((error = git_repository_odb__weakptr(&odb, repo)) < 0 ||