Commit 5663d4f69124481fcfab2952b5207c7563568388

Carlos Martín Nieto 2016-02-18T12:31:56

Merge pull request #3613 from ethomson/fixups Remove most of the silly warnings

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
diff --git a/src/index.c b/src/index.c
index c5a1b07..d0a0da2 100644
--- a/src/index.c
+++ b/src/index.c
@@ -603,14 +603,14 @@ const git_oid *git_index_checksum(git_index *index)
  */
 static int compare_checksum(git_index *index)
 {
-	int fd, error;
+	int fd;
 	ssize_t bytes_read;
 	git_oid checksum = {{ 0 }};
 
 	if ((fd = p_open(index->index_file_path, O_RDONLY)) < 0)
 		return fd;
 
-	if ((error = p_lseek(fd, -20, SEEK_END)) < 0) {
+	if (p_lseek(fd, -20, SEEK_END) < 0) {
 		p_close(fd);
 		giterr_set(GITERR_OS, "failed to seek to end of file");
 		return -1;
@@ -826,8 +826,8 @@ const git_index_entry *git_index_get_bypath(
 void git_index_entry__init_from_stat(
 	git_index_entry *entry, struct stat *st, bool trust_mode)
 {
-	entry->ctime.seconds = (git_time_t)st->st_ctime;
-	entry->mtime.seconds = (git_time_t)st->st_mtime;
+	entry->ctime.seconds = (int32_t)st->st_ctime;
+	entry->mtime.seconds = (int32_t)st->st_mtime;
 #if defined(GIT_USE_NSEC)
 	entry->mtime.nanoseconds = st->st_mtim.tv_nsec;
 	entry->ctime.nanoseconds = st->st_ctim.tv_nsec;
@@ -838,7 +838,7 @@ void git_index_entry__init_from_stat(
 		git_index__create_mode(0666) : git_index__create_mode(st->st_mode);
 	entry->uid  = st->st_uid;
 	entry->gid  = st->st_gid;
-	entry->file_size = st->st_size;
+	entry->file_size = (uint32_t)st->st_size;
 }
 
 static void index_entry_adjust_namemask(
@@ -1529,7 +1529,7 @@ int git_index__fill(git_index *index, const git_vector *source_entries)
 		return 0;
 
 	git_vector_size_hint(&index->entries, source_entries->length);
-	git_idxmap_resize(index->entries_map, source_entries->length * 1.3);
+	git_idxmap_resize(index->entries_map, (khint_t)(source_entries->length * 1.3));
 
 	git_vector_foreach(source_entries, i, source_entry) {
 		git_index_entry *entry = NULL;
diff --git a/src/submodule.c b/src/submodule.c
index 3e55c56..38db415 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -778,9 +778,9 @@ int git_submodule_add_to_index(git_submodule *sm, int write_index)
 	if ((error = git_commit_lookup(&head, sm_repo, &sm->wd_oid)) < 0)
 		goto cleanup;
 
-	entry.ctime.seconds = git_commit_time(head);
+	entry.ctime.seconds = (int32_t)git_commit_time(head);
 	entry.ctime.nanoseconds = 0;
-	entry.mtime.seconds = git_commit_time(head);
+	entry.mtime.seconds = (int32_t)git_commit_time(head);
 	entry.mtime.nanoseconds = 0;
 
 	git_commit_free(head);
diff --git a/src/transports/winhttp.c b/src/transports/winhttp.c
index 3750f6e..ded0416 100644
--- a/src/transports/winhttp.c
+++ b/src/transports/winhttp.c
@@ -283,7 +283,7 @@ static int winhttp_stream_connect(winhttp_stream *s)
 	unsigned long disable_redirects = WINHTTP_DISABLE_REDIRECTS;
 	int default_timeout = TIMEOUT_INFINITE;
 	int default_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
-	int i;
+	size_t i;
 
 	/* Prepare URL */
 	git_buf_printf(&buf, "%s%s", t->connection_data.path, s->service_url);
diff --git a/src/tree.c b/src/tree.c
index aab4b58..cfceb3f 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -17,6 +17,9 @@
 #define DEFAULT_TREE_SIZE 16
 #define MAX_FILEMODE_BYTES 6
 
+#define TREE_ENTRY_CHECK_NAMELEN(n) \
+	if (n > UINT16_MAX) { giterr_set(GITERR_INVALID, "tree entry path too long"); }
+
 GIT__USE_STRMAP
 
 static bool valid_filemode(const int filemode)
@@ -89,10 +92,7 @@ static git_tree_entry *alloc_entry_base(git_pool *pool, const char *filename, si
 	git_tree_entry *entry = NULL;
 	size_t tree_len;
 
-	if (filename_len > UINT16_MAX) {
-		giterr_set(GITERR_INVALID, "tree entry is over UINT16_MAX in length");
-		return NULL;
-	}
+	TREE_ENTRY_CHECK_NAMELEN(filename_len);
 
 	if (GIT_ADD_SIZET_OVERFLOW(&tree_len, sizeof(git_tree_entry), filename_len) ||
 	    GIT_ADD_SIZET_OVERFLOW(&tree_len, tree_len, 1))
@@ -106,7 +106,7 @@ static git_tree_entry *alloc_entry_base(git_pool *pool, const char *filename, si
 	memset(entry, 0x0, sizeof(git_tree_entry));
 	memcpy(entry->filename, filename, filename_len);
 	entry->filename[filename_len] = 0;
-	entry->filename_len = filename_len;
+	entry->filename_len = (uint16_t)filename_len;
 
 	return entry;
 }
@@ -143,8 +143,8 @@ static int homing_search_cmp(const void *key, const void *array_member)
 	const struct tree_key_search *ksearch = key;
 	const git_tree_entry *entry = array_member;
 
-	const size_t len1 = ksearch->filename_len;
-	const size_t len2 = entry->filename_len;
+	const uint16_t len1 = ksearch->filename_len;
+	const uint16_t len2 = entry->filename_len;
 
 	return memcmp(
 		ksearch->filename,
@@ -180,8 +180,10 @@ static int tree_key_search(
 	const git_tree_entry *entry;
 	size_t homing, i;
 
+	TREE_ENTRY_CHECK_NAMELEN(filename_len);
+
 	ksearch.filename = filename;
-	ksearch.filename_len = filename_len;
+	ksearch.filename_len = (uint16_t)filename_len;
 
 	/* Initial homing search; find an entry on the tree with
 	 * the same prefix as the filename we're looking for */
@@ -334,6 +336,7 @@ const git_tree_entry *git_tree_entry_byname(
 	const git_tree *tree, const char *filename)
 {
 	assert(tree && filename);
+
 	return entry_fromname(tree, filename, strlen(filename));
 }
 
@@ -364,13 +367,16 @@ int git_tree__prefix_position(const git_tree *tree, const char *path)
 {
 	const git_vector *entries = &tree->entries;
 	struct tree_key_search ksearch;
-	size_t at_pos;
+	size_t at_pos, path_len;
 
 	if (!path)
 		return 0;
 
+	path_len = strlen(path);
+	TREE_ENTRY_CHECK_NAMELEN(path_len);
+
 	ksearch.filename = path;
-	ksearch.filename_len = strlen(path);
+	ksearch.filename_len = (uint16_t)path_len;
 
 	/* be safe when we cast away constness - i.e. don't trigger a sort */
 	assert(git_vector_is_sorted(&tree->entries));
diff --git a/src/unix/posix.h b/src/unix/posix.h
index 6633689..83edf2b 100644
--- a/src/unix/posix.h
+++ b/src/unix/posix.h
@@ -52,8 +52,10 @@ extern char *p_realpath(const char *, char *);
 #define p_localtime_r(c, r) localtime_r(c, r)
 #define p_gmtime_r(c, r) gmtime_r(c, r)
 
+#define p_timeval timeval
+
 #ifdef HAVE_FUTIMENS
-GIT_INLINE(int) p_futimes(int f, const struct timeval t[2])
+GIT_INLINE(int) p_futimes(int f, const struct p_timeval t[2])
 {
 	struct timespec s[2];
 	s[0].tv_sec = t[0].tv_sec;
diff --git a/src/win32/posix.h b/src/win32/posix.h
index ac98fd8..5fab267 100644
--- a/src/win32/posix.h
+++ b/src/win32/posix.h
@@ -9,6 +9,7 @@
 
 #include "common.h"
 #include "../posix.h"
+#include "win32-compat.h"
 #include "path_w32.h"
 #include "utf-conv.h"
 #include "dir.h"
@@ -16,12 +17,13 @@
 typedef SOCKET GIT_SOCKET;
 
 #define p_lseek(f,n,w) _lseeki64(f, n, w)
-#define p_fstat(f,b) _fstat64(f, b)
+
+extern int p_fstat(int fd, struct stat *buf);
 extern int p_lstat(const char *file_name, struct stat *buf);
-extern int p_stat(const char* path, struct stat* buf);
+extern int p_stat(const char* path, struct stat *buf);
 
-extern int p_utimes(const char *filename, const struct timeval times[2]);
-extern int p_futimes(int fd, const struct timeval times[2]);
+extern int p_utimes(const char *filename, const struct p_timeval times[2]);
+extern int p_futimes(int fd, const struct p_timeval times[2]);
 
 extern int p_readlink(const char *path, char *buf, size_t bufsiz);
 extern int p_symlink(const char *old, const char *new);
diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c
index 414cb47..fea634b 100644
--- a/src/win32/posix_w32.c
+++ b/src/win32/posix_w32.c
@@ -210,7 +210,7 @@ int p_lstat_posixly(const char *filename, struct stat *buf)
 	return do_lstat(filename, buf, true);
 }
 
-int p_utimes(const char *filename, const struct timeval times[2])
+int p_utimes(const char *filename, const struct p_timeval times[2])
 {
 	int fd, error;
 
@@ -223,7 +223,7 @@ int p_utimes(const char *filename, const struct timeval times[2])
 	return error;
 }
 
-int p_futimes(int fd, const struct timeval times[2])
+int p_futimes(int fd, const struct p_timeval times[2])
 {
 	HANDLE handle;
 	FILETIME atime = {0}, mtime = {0};
@@ -398,6 +398,22 @@ static int follow_and_lstat_link(git_win32_path path, struct stat* buf)
 	return lstat_w(target_w, buf, false);
 }
 
+int p_fstat(int fd, struct stat *buf)
+{
+	BY_HANDLE_FILE_INFORMATION fhInfo;
+
+	HANDLE fh = (HANDLE)_get_osfhandle(fd);
+
+	if (fh == INVALID_HANDLE_VALUE ||
+		!GetFileInformationByHandle(fh, &fhInfo)) {
+		errno = EBADF;
+		return -1;
+	}
+
+	git_win32__file_information_to_stat(buf, &fhInfo);
+	return 0;
+}
+
 int p_stat(const char* path, struct stat* buf)
 {
 	git_win32_path path_w;
diff --git a/src/win32/w32_util.h b/src/win32/w32_util.h
index 727ed1c..2e475e5 100644
--- a/src/win32/w32_util.h
+++ b/src/win32/w32_util.h
@@ -96,7 +96,7 @@ GIT_INLINE(void) git_win32__filetime_to_timespec(
 }
 
 GIT_INLINE(void) git_win32__timeval_to_filetime(
-	FILETIME *ft, const struct timeval tv)
+	FILETIME *ft, const struct p_timeval tv)
 {
 	long long ticks = (tv.tv_sec * 10000000LL) +
 		(tv.tv_usec * 10LL) + 116444736000000000LL;
@@ -105,19 +105,25 @@ GIT_INLINE(void) git_win32__timeval_to_filetime(
 	ft->dwLowDateTime = (ticks & 0xffffffffLL);
 }
 
-GIT_INLINE(int) git_win32__file_attribute_to_stat(
+GIT_INLINE(void) git_win32__stat_init(
 	struct stat *st,
-	const WIN32_FILE_ATTRIBUTE_DATA *attrdata,
-	const wchar_t *path)
+	DWORD dwFileAttributes,
+	DWORD nFileSizeHigh,
+	DWORD nFileSizeLow,
+	FILETIME ftCreationTime,
+	FILETIME ftLastAccessTime,
+	FILETIME ftLastWriteTime)
 {
 	mode_t mode = S_IREAD;
 
-	if (attrdata->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
+	memset(st, 0, sizeof(struct stat));
+
+	if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
 		mode |= S_IFDIR;
 	else
 		mode |= S_IFREG;
 
-	if ((attrdata->dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0)
+	if ((dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0)
 		mode |= S_IWRITE;
 
 	st->st_ino = 0;
@@ -125,12 +131,39 @@ GIT_INLINE(int) git_win32__file_attribute_to_stat(
 	st->st_uid = 0;
 	st->st_nlink = 1;
 	st->st_mode = mode;
-	st->st_size = ((git_off_t)attrdata->nFileSizeHigh << 32) + attrdata->nFileSizeLow;
+	st->st_size = ((git_off_t)nFileSizeHigh << 32) + nFileSizeLow;
 	st->st_dev = _getdrive() - 1;
 	st->st_rdev = st->st_dev;
-	git_win32__filetime_to_timespec(&(attrdata->ftLastAccessTime), &(st->st_atim));
-	git_win32__filetime_to_timespec(&(attrdata->ftLastWriteTime), &(st->st_mtim));
-	git_win32__filetime_to_timespec(&(attrdata->ftCreationTime), &(st->st_ctim));
+	git_win32__filetime_to_timespec(&ftLastAccessTime, &(st->st_atim));
+	git_win32__filetime_to_timespec(&ftLastWriteTime, &(st->st_mtim));
+	git_win32__filetime_to_timespec(&ftCreationTime, &(st->st_ctim));
+}
+
+GIT_INLINE(void) git_win32__file_information_to_stat(
+	struct stat *st,
+	const BY_HANDLE_FILE_INFORMATION *fileinfo)
+{
+	git_win32__stat_init(st,
+		fileinfo->dwFileAttributes,
+		fileinfo->nFileSizeHigh,
+		fileinfo->nFileSizeLow,
+		fileinfo->ftCreationTime,
+		fileinfo->ftLastAccessTime,
+		fileinfo->ftLastWriteTime);
+}
+
+GIT_INLINE(int) git_win32__file_attribute_to_stat(
+	struct stat *st,
+	const WIN32_FILE_ATTRIBUTE_DATA *attrdata,
+	const wchar_t *path)
+{
+	git_win32__stat_init(st,
+		attrdata->dwFileAttributes,
+		attrdata->nFileSizeHigh,
+		attrdata->nFileSizeLow,
+		attrdata->ftCreationTime,
+		attrdata->ftLastAccessTime,
+		attrdata->ftLastWriteTime);
 
 	if (attrdata->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT && path) {
 		git_win32_path target;
@@ -139,7 +172,7 @@ GIT_INLINE(int) git_win32__file_attribute_to_stat(
 			st->st_mode = (st->st_mode & ~S_IFMT) | S_IFLNK;
 
 			/* st_size gets the UTF-8 length of the target name, in bytes,
-				* not counting the NULL terminator */
+			 * not counting the NULL terminator */
 			if ((st->st_size = git__utf16_to_8(NULL, 0, target)) < 0) {
 				giterr_set(GITERR_OS, "Could not convert reparse point name for '%s'", path);
 				return -1;
diff --git a/src/win32/win32-compat.h b/src/win32/win32-compat.h
index d3a5b68..dff1f45 100644
--- a/src/win32/win32-compat.h
+++ b/src/win32/win32-compat.h
@@ -13,6 +13,13 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 
+typedef long suseconds_t;
+
+struct p_timeval {
+	time_t tv_sec;
+	suseconds_t tv_usec;
+};
+
 struct p_timespec {
 	time_t tv_sec;
 	long tv_nsec;
diff --git a/tests/checkout/checkout_helpers.c b/tests/checkout/checkout_helpers.c
index fb2f415..d7d24f3 100644
--- a/tests/checkout/checkout_helpers.c
+++ b/tests/checkout/checkout_helpers.c
@@ -133,7 +133,7 @@ int checkout_count_callback(
 void tick_index(git_index *index)
 {
 	struct timespec ts;
-	struct timeval times[2];
+	struct p_timeval times[2];
 
 	cl_assert(index->on_disk);
 	cl_assert(git_index_path(index));
diff --git a/tests/core/posix.c b/tests/core/posix.c
index 5a9e248..34a67bf 100644
--- a/tests/core/posix.c
+++ b/tests/core/posix.c
@@ -100,7 +100,7 @@ void test_core_posix__inet_pton(void)
 
 void test_core_posix__utimes(void)
 {
-	struct timeval times[2];
+	struct p_timeval times[2];
 	struct stat st;
 	time_t curtime;
 	int fd;
diff --git a/tests/diff/workdir.c b/tests/diff/workdir.c
index 4c78233..892c7b7 100644
--- a/tests/diff/workdir.c
+++ b/tests/diff/workdir.c
@@ -1755,7 +1755,7 @@ void test_diff_workdir__with_stale_index(void)
 static int touch_file(void *payload, git_buf *path)
 {
 	struct stat st;
-	struct timeval times[2];
+	struct p_timeval times[2];
 
 	GIT_UNUSED(payload);
 	if (git_path_isdir(path->ptr))
@@ -2006,7 +2006,7 @@ void test_diff_workdir__only_writes_index_when_necessary(void)
 	git_oid initial, first, second;
 	git_buf path = GIT_BUF_INIT;
 	struct stat st;
-	struct timeval times[2];
+	struct p_timeval times[2];
 
 	opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_UPDATE_INDEX;
 
diff --git a/tests/index/racy.c b/tests/index/racy.c
index e2275ea..ace84d5 100644
--- a/tests/index/racy.c
+++ b/tests/index/racy.c
@@ -54,7 +54,7 @@ void test_index_racy__write_index_just_after_file(void)
 	git_index *index;
 	git_diff *diff;
 	git_buf path = GIT_BUF_INIT;
-	struct timeval times[2];
+	struct p_timeval times[2];
 
 	/* Make sure we do have a timestamp */
 	cl_git_pass(git_repository_index(&index, g_repo));
diff --git a/tests/merge/workdir/dirty.c b/tests/merge/workdir/dirty.c
index f168963..99e33e0 100644
--- a/tests/merge/workdir/dirty.c
+++ b/tests/merge/workdir/dirty.c
@@ -133,7 +133,7 @@ static void hack_index(char *files[])
 	struct stat statbuf;
 	git_buf path = GIT_BUF_INIT;
 	git_index_entry *entry;
-	struct timeval times[2];
+	struct p_timeval times[2];
 	time_t now;
 	size_t i;
 
@@ -162,8 +162,8 @@ static void hack_index(char *files[])
 		cl_git_pass(p_utimes(path.ptr, times));
 		cl_git_pass(p_stat(path.ptr, &statbuf));
 
-		entry->ctime.seconds = (git_time_t)statbuf.st_ctime;
-		entry->mtime.seconds = (git_time_t)statbuf.st_mtime;
+		entry->ctime.seconds = (int32_t)statbuf.st_ctime;
+		entry->mtime.seconds = (int32_t)statbuf.st_mtime;
 #if defined(GIT_USE_NSEC)
 		entry->ctime.nanoseconds = statbuf.st_ctim.tv_nsec;
 		entry->mtime.nanoseconds = statbuf.st_mtim.tv_nsec;
@@ -175,7 +175,7 @@ static void hack_index(char *files[])
 		entry->ino = statbuf.st_ino;
 		entry->uid  = statbuf.st_uid;
 		entry->gid  = statbuf.st_gid;
-		entry->file_size = statbuf.st_size;
+		entry->file_size = (uint32_t)statbuf.st_size;
 	}
 
 	git_buf_free(&path);
diff --git a/tests/reset/hard.c b/tests/reset/hard.c
index 1499733..0c0af91 100644
--- a/tests/reset/hard.c
+++ b/tests/reset/hard.c
@@ -238,7 +238,7 @@ void test_reset_hard__reflog_is_correct(void)
 
 void test_reset_hard__switch_file_to_dir(void)
 {
-	git_index_entry entry = { 0 };
+	git_index_entry entry = {{ 0 }};
 	git_index *idx;
 	git_object *commit;
 	git_tree *tree;
diff --git a/tests/win32/longpath.c b/tests/win32/longpath.c
index 07eecd3..5a36875 100644
--- a/tests/win32/longpath.c
+++ b/tests/win32/longpath.c
@@ -36,7 +36,7 @@ void assert_name_too_long(void)
 {
 	const git_error *err;
 	size_t expected_len, actual_len;
-	const char *expected_msg;
+	char *expected_msg;
 
 	err = giterr_last();
 	actual_len = strlen(err->message);