Commit c544850127abb96b472485768dee3ede68e1622e

Edward Thomson 2019-06-25T12:55:10

Merge pull request #5078 from libgit2/ethomson/warnings Remove 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
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6e633f1..292fadd 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -239,10 +239,22 @@ ELSE ()
 	ENABLE_WARNINGS(shift-count-overflow)
 	ENABLE_WARNINGS(unused-const-variable)
 	ENABLE_WARNINGS(unused-function)
-	ENABLE_WARNINGS(format)
-	ENABLE_WARNINGS(format-security)
 	ENABLE_WARNINGS(int-conversion)
-	DISABLE_WARNINGS(documentation-deprecated-sync)
+
+	# MinGW uses gcc, which expects POSIX formatting for printf, but
+	# uses the Windows C library, which uses its own format specifiers.
+	# Disable format specifier warnings.
+	IF(MINGW)
+		DISABLE_WARNINGS(format)
+		DISABLE_WARNINGS(format-security)
+	ELSE()
+		ENABLE_WARNINGS(format)
+		ENABLE_WARNINGS(format-security)
+	ENDIF()
+
+	IF("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
+		DISABLE_WARNINGS(documentation-deprecated-sync)
+	ENDIF()
 
 	IF (PROFILE)
 		SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}")
diff --git a/cmake/Modules/EnableWarnings.cmake b/cmake/Modules/EnableWarnings.cmake
index 72e1523..b61ed7e 100644
--- a/cmake/Modules/EnableWarnings.cmake
+++ b/cmake/Modules/EnableWarnings.cmake
@@ -7,5 +7,9 @@ MACRO(DISABLE_WARNINGS flag)
 ENDMACRO()
 
 IF(ENABLE_WERROR)
-	ADD_C_FLAG_IF_SUPPORTED(-Werror)
+	IF(MSVC)
+		ADD_COMPILE_OPTIONS(-WX)
+	ELSE()
+		ADD_C_FLAG_IF_SUPPORTED(-Werror)
+	ENDIF()
 ENDIF()
diff --git a/deps/http-parser/http_parser.c b/deps/http-parser/http_parser.c
index cc53a52..1bcd330 100644
--- a/deps/http-parser/http_parser.c
+++ b/deps/http-parser/http_parser.c
@@ -1992,6 +1992,9 @@ http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
   const char *p;
   size_t buflen = u->field_data[UF_HOST].off + u->field_data[UF_HOST].len;
 
+  if (buflen > UINT16_MAX)
+    return 1;
+
   u->field_data[UF_HOST].len = 0;
 
   s = found_at ? s_http_userinfo_start : s_http_host_start;
@@ -2006,21 +2009,21 @@ http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
     switch(new_s) {
       case s_http_host:
         if (s != s_http_host) {
-          u->field_data[UF_HOST].off = p - buf;
+          u->field_data[UF_HOST].off = (uint16_t)(p - buf);
         }
         u->field_data[UF_HOST].len++;
         break;
 
       case s_http_host_v6:
         if (s != s_http_host_v6) {
-          u->field_data[UF_HOST].off = p - buf;
+          u->field_data[UF_HOST].off = (uint16_t)(p - buf);
         }
         u->field_data[UF_HOST].len++;
         break;
 
       case s_http_host_port:
         if (s != s_http_host_port) {
-          u->field_data[UF_PORT].off = p - buf;
+          u->field_data[UF_PORT].off = (uint16_t)(p - buf);
           u->field_data[UF_PORT].len = 0;
           u->field_set |= (1 << UF_PORT);
         }
@@ -2029,7 +2032,7 @@ http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
 
       case s_http_userinfo:
         if (s != s_http_userinfo) {
-          u->field_data[UF_USERINFO].off = p - buf ;
+          u->field_data[UF_USERINFO].off = (uint16_t)(p - buf);
           u->field_data[UF_USERINFO].len = 0;
           u->field_set |= (1 << UF_USERINFO);
         }
@@ -2066,6 +2069,9 @@ http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
   enum http_parser_url_fields uf, old_uf;
   int found_at = 0;
 
+  if (buflen > UINT16_MAX)
+    return 1;
+
   u->port = u->field_set = 0;
   s = is_connect ? s_req_server_start : s_req_spaces_before_url;
   uf = old_uf = UF_MAX;
@@ -2121,7 +2127,7 @@ http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
       continue;
     }
 
-    u->field_data[uf].off = p - buf;
+    u->field_data[uf].off = (uint16_t)(p - buf);
     u->field_data[uf].len = 1;
 
     u->field_set |= (1 << uf);
diff --git a/deps/zlib/adler32.c b/deps/zlib/adler32.c
index d0be438..444a5a8 100644
--- a/deps/zlib/adler32.c
+++ b/deps/zlib/adler32.c
@@ -177,10 +177,3 @@ uLong ZEXPORT adler32_combine(adler1, adler2, len2)
     return adler32_combine_(adler1, adler2, len2);
 }
 
-uLong ZEXPORT adler32_combine64(adler1, adler2, len2)
-    uLong adler1;
-    uLong adler2;
-    z_off64_t len2;
-{
-    return adler32_combine_(adler1, adler2, len2);
-}
diff --git a/deps/zlib/crc32.c b/deps/zlib/crc32.c
index 9580440..1ee0284 100644
--- a/deps/zlib/crc32.c
+++ b/deps/zlib/crc32.c
@@ -433,10 +433,3 @@ uLong ZEXPORT crc32_combine(crc1, crc2, len2)
     return crc32_combine_(crc1, crc2, len2);
 }
 
-uLong ZEXPORT crc32_combine64(crc1, crc2, len2)
-    uLong crc1;
-    uLong crc2;
-    z_off64_t len2;
-{
-    return crc32_combine_(crc1, crc2, len2);
-}
diff --git a/include/git2/tree.h b/include/git2/tree.h
index aca5d75..4455d2e 100644
--- a/include/git2/tree.h
+++ b/include/git2/tree.h
@@ -267,7 +267,7 @@ GIT_EXTERN(void) git_treebuilder_clear(git_treebuilder *bld);
  * @param bld a previously loaded treebuilder.
  * @return the number of entries in the treebuilder
  */
-GIT_EXTERN(unsigned int) git_treebuilder_entrycount(git_treebuilder *bld);
+GIT_EXTERN(size_t) git_treebuilder_entrycount(git_treebuilder *bld);
 
 /**
  * Free a tree builder
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 7ab1832..750085b 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -314,12 +314,20 @@ ELSE()
 	ENDIF()
 	FILE(GLOB SRC_OS unix/*.c unix/*.h)
 ENDIF()
+
 FILE(GLOB SRC_GIT2 *.c *.h
 	allocators/*.c allocators/*.h
 	streams/*.c streams/*.h
 	transports/*.c transports/*.h
 	xdiff/*.c xdiff/*.h)
 
+# the xdiff dependency is not (yet) warning-free, disable warnings as
+# errors for the xdiff sources until we've sorted them out
+IF(MSVC)
+	SET_SOURCE_FILES_PROPERTIES(xdiff/xdiffi.c PROPERTIES COMPILE_FLAGS -WX-)
+	SET_SOURCE_FILES_PROPERTIES(xdiff/xutils.c PROPERTIES COMPILE_FLAGS -WX-)
+ENDIF()
+
 # Determine architecture of the machine
 IF (CMAKE_SIZEOF_VOID_P EQUAL 8)
 	SET(GIT_ARCH_64 1)
diff --git a/src/attrcache.c b/src/attrcache.c
index 00e4596..b88bc09 100644
--- a/src/attrcache.c
+++ b/src/attrcache.c
@@ -54,7 +54,7 @@ int git_attr_cache__alloc_file_entry(
 			cachesize++;
 	}
 
-	ce = git_pool_mallocz(pool, (uint32_t)cachesize);
+	ce = git_pool_mallocz(pool, cachesize);
 	GIT_ERROR_CHECK_ALLOC(ce);
 
 	if (baselen) {
diff --git a/src/commit_list.c b/src/commit_list.c
index 5496dca..44673d9 100644
--- a/src/commit_list.c
+++ b/src/commit_list.c
@@ -73,7 +73,7 @@ static git_commit_list_node **alloc_parents(
 		return (git_commit_list_node **)((char *)commit + sizeof(git_commit_list_node));
 
 	return (git_commit_list_node **)git_pool_malloc(
-		&walk->commit_pool, (uint32_t)(n_parents * sizeof(git_commit_list_node *)));
+		&walk->commit_pool, (n_parents * sizeof(git_commit_list_node *)));
 }
 
 
diff --git a/src/config_parse.c b/src/config_parse.c
index 64d9a69..46dce40 100644
--- a/src/config_parse.c
+++ b/src/config_parse.c
@@ -88,6 +88,11 @@ static int parse_subsection_header(git_config_parser *reader, const char *line, 
 	last_quote = strrchr(line, '"');
 	quoted_len = last_quote - first_quote;
 
+	if ((last_quote - line) > INT_MAX) {
+		set_parse_error(reader, 0, "invalid section header, line too long");
+		goto end_error;
+	}
+
 	if (quoted_len == 0) {
 		set_parse_error(reader, 0, "missing closing quotation mark in section header");
 		goto end_error;
@@ -146,7 +151,7 @@ end_parse:
 	}
 
 	*section_name = git_buf_detach(&buf);
-	return &line[rpos + 2] - line_start; /* rpos is at the closing quote */
+	return (int)(&line[rpos + 2] - line_start); /* rpos is at the closing quote */
 
 end_error:
 	git_buf_dispose(&buf);
diff --git a/src/fileops.c b/src/fileops.c
index a4d5cc6..0b732aa 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -636,9 +636,7 @@ retry_lstat:
 			size_t alloc_size;
 
 			GIT_ERROR_CHECK_ALLOC_ADD(&alloc_size, make_path.size, 1);
-			if (!git__is_uint32(alloc_size))
-				return -1;
-			cache_path = git_pool_malloc(opts->pool, (uint32_t)alloc_size);
+			cache_path = git_pool_malloc(opts->pool, alloc_size);
 			GIT_ERROR_CHECK_ALLOC(cache_path);
 
 			memcpy(cache_path, make_path.ptr, make_path.size + 1);
diff --git a/src/idxmap.c b/src/idxmap.c
index e75887c..4dcf963 100644
--- a/src/idxmap.c
+++ b/src/idxmap.c
@@ -70,7 +70,8 @@ void git_idxmap_icase_clear(git_idxmap_icase *map)
 
 int git_idxmap_resize(git_idxmap *map, size_t size)
 {
-	if (kh_resize(idx, map, size) < 0) {
+	if (!git__is_uint32(size) ||
+	    kh_resize(idx, map, (khiter_t)size) < 0) {
 		git_error_set_oom();
 		return -1;
 	}
@@ -79,7 +80,8 @@ int git_idxmap_resize(git_idxmap *map, size_t size)
 
 int git_idxmap_icase_resize(git_idxmap_icase *map, size_t size)
 {
-	if (kh_resize(idxicase, map, size) < 0) {
+	if (!git__is_uint32(size) ||
+	    kh_resize(idxicase, map, (khiter_t)size) < 0) {
 		git_error_set_oom();
 		return -1;
 	}
diff --git a/src/index.c b/src/index.c
index 482728d..0c90173 100644
--- a/src/index.c
+++ b/src/index.c
@@ -1474,6 +1474,11 @@ int git_index_add_from_buffer(
 		return -1;
 	}
 
+	if (len > UINT32_MAX) {
+		git_error_set(GIT_ERROR_INDEX, "buffer is too large");
+		return -1;
+	}
+
 	if (index_entry_dup(&entry, index, source_entry) < 0)
 		return -1;
 
@@ -1484,7 +1489,7 @@ int git_index_add_from_buffer(
 	}
 
 	git_oid_cpy(&entry->id, &id);
-	entry->file_size = len;
+	entry->file_size = (uint32_t)len;
 
 	if ((error = index_insert(index, &entry, 1, true, true, true)) < 0)
 		return error;
diff --git a/src/merge.c b/src/merge.c
index ec34ccb..c1f1cc4 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -867,7 +867,7 @@ static int merge_conflict_invoke_driver(
 
 	git_oid_cpy(&result->id, &oid);
 	result->mode = mode;
-	result->file_size = buf.size;
+	result->file_size = (uint32_t)buf.size;
 
 	result->path = git_pool_strdup(&diff_list->pool, path);
 	GIT_ERROR_CHECK_ALLOC(result->path);
diff --git a/src/netops.c b/src/netops.c
index 708f694..f19bae9 100644
--- a/src/netops.c
+++ b/src/netops.c
@@ -37,14 +37,17 @@ void gitno_buffer_setup_callback(
 static int recv_stream(gitno_buffer *buf)
 {
 	git_stream *io = (git_stream *) buf->cb_data;
-	int ret;
+	size_t readlen = buf->len - buf->offset;
+	ssize_t ret;
 
-	ret = git_stream_read(io, buf->data + buf->offset, buf->len - buf->offset);
+	readlen = min(readlen, INT_MAX);
+
+	ret = git_stream_read(io, buf->data + buf->offset, (int)readlen);
 	if (ret < 0)
 		return -1;
 
 	buf->offset += ret;
-	return ret;
+	return (int)ret;
 }
 
 void gitno_buffer_setup_fromstream(git_stream *st, gitno_buffer *buf, char *data, size_t len)
diff --git a/src/odb_loose.c b/src/odb_loose.c
index 5bdf884..4b249b6 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -408,7 +408,8 @@ done:
 static int read_header_loose(git_rawobj *out, git_buf *loc)
 {
 	unsigned char obj[1024];
-	int fd, obj_len, error;
+	ssize_t obj_len;
+	int fd, error;
 
 	assert(out && loc);
 
@@ -417,10 +418,14 @@ static int read_header_loose(git_rawobj *out, git_buf *loc)
 
 	out->data = NULL;
 
-	if ((error = fd = git_futils_open_ro(loc->ptr)) < 0 ||
-		(error = obj_len = p_read(fd, obj, sizeof(obj))) < 0)
+	if ((error = fd = git_futils_open_ro(loc->ptr)) < 0)
 		goto done;
 
+	if ((obj_len = p_read(fd, obj, sizeof(obj))) < 0) {
+		error = (int)obj_len;
+		goto done;
+	}
+
 	if (!is_zlib_compressed_data(obj, (size_t)obj_len))
 		error = read_header_loose_packlike(out, obj, (size_t)obj_len);
 	else
@@ -871,6 +876,8 @@ static int loose_backend__readstream_read(
 	size_t start_remain = stream->start_len - stream->start_read;
 	int total = 0, error;
 
+	buffer_len = min(buffer_len, INT_MAX);
+
 	/*
 	 * if we read more than just the header in the initial read, play
 	 * that back for the caller.
@@ -882,20 +889,20 @@ static int loose_backend__readstream_read(
 		buffer += chunk;
 		stream->start_read += chunk;
 
-		total += chunk;
+		total += (int)chunk;
 		buffer_len -= chunk;
 	}
 
 	if (buffer_len) {
-		size_t chunk = min(buffer_len, INT_MAX);
+		size_t chunk = buffer_len;
 
 		if ((error = git_zstream_get_output(buffer, &chunk, &stream->zstream)) < 0)
 			return error;
 
-		total += chunk;
+		total += (int)chunk;
 	}
 
-	return total;
+	return (int)total;
 }
 
 static void loose_backend__readstream_free(git_odb_stream *_stream)
diff --git a/src/patch_parse.c b/src/patch_parse.c
index d731115..b44d4f0 100644
--- a/src/patch_parse.c
+++ b/src/patch_parse.c
@@ -33,7 +33,7 @@ typedef struct {
 	char *old_prefix, *new_prefix;
 } git_patch_parsed;
 
-static int header_path_len(git_patch_parse_ctx *ctx)
+static size_t header_path_len(git_patch_parse_ctx *ctx)
 {
 	bool inquote = 0;
 	bool quoted = git_parse_ctx_contains_s(&ctx->parse_ctx, "\"");
diff --git a/src/path.c b/src/path.c
index af18edc..b1a92ae 100644
--- a/src/path.c
+++ b/src/path.c
@@ -160,7 +160,13 @@ int git_path_dirname_r(git_buf *buffer, const char *path)
 	while (endp > path && *endp == '/')
 		endp--;
 
-	if ((len = win32_prefix_length(path, endp - path + 1)) > 0) {
+	if (endp - path + 1 > INT_MAX) {
+		git_error_set(GIT_ERROR_INVALID, "path too long");
+		len = -1;
+		goto Exit;
+	}
+
+	if ((len = win32_prefix_length(path, (int)(endp - path + 1))) > 0) {
 		is_prefix = 1;
 		goto Exit;
 	}
@@ -180,7 +186,13 @@ int git_path_dirname_r(git_buf *buffer, const char *path)
 		endp--;
 	} while (endp > path && *endp == '/');
 
-	if ((len = win32_prefix_length(path, endp - path + 1)) > 0) {
+	if (endp - path + 1 > INT_MAX) {
+		git_error_set(GIT_ERROR_INVALID, "path too long");
+		len = -1;
+		goto Exit;
+	}
+
+	if ((len = win32_prefix_length(path, (int)(endp - path + 1))) > 0) {
 		is_prefix = 1;
 		goto Exit;
 	}
diff --git a/src/pool.c b/src/pool.c
index c0efe9c..b3bc8d4 100644
--- a/src/pool.c
+++ b/src/pool.c
@@ -14,30 +14,30 @@
 
 struct git_pool_page {
 	git_pool_page *next;
-	uint32_t size;
-	uint32_t avail;
+	size_t size;
+	size_t avail;
 	GIT_ALIGN(char data[GIT_FLEX_ARRAY], 8);
 };
 
-static void *pool_alloc_page(git_pool *pool, uint32_t size);
+static void *pool_alloc_page(git_pool *pool, size_t size);
 
-uint32_t git_pool__system_page_size(void)
+size_t git_pool__system_page_size(void)
 {
-	static uint32_t size = 0;
+	static size_t size = 0;
 
 	if (!size) {
 		size_t page_size;
 		if (git__page_size(&page_size) < 0)
 			page_size = 4096;
 		/* allow space for malloc overhead */
-		size = page_size - (2 * sizeof(void *)) - sizeof(git_pool_page);
+		size = (page_size - (2 * sizeof(void *)) - sizeof(git_pool_page));
 	}
 
 	return size;
 }
 
 #ifndef GIT_DEBUG_POOL
-void git_pool_init(git_pool *pool, uint32_t item_size)
+void git_pool_init(git_pool *pool, size_t item_size)
 {
 	assert(pool);
 	assert(item_size >= 1);
@@ -59,10 +59,10 @@ void git_pool_clear(git_pool *pool)
 	pool->pages = NULL;
 }
 
-static void *pool_alloc_page(git_pool *pool, uint32_t size)
+static void *pool_alloc_page(git_pool *pool, size_t size)
 {
 	git_pool_page *page;
-	const uint32_t new_page_size = (size <= pool->page_size) ? pool->page_size : size;
+	const size_t new_page_size = (size <= pool->page_size) ? pool->page_size : size;
 	size_t alloc_size;
 
 	if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, new_page_size, sizeof(git_pool_page)) ||
@@ -78,7 +78,7 @@ static void *pool_alloc_page(git_pool *pool, uint32_t size)
 	return page->data;
 }
 
-static void *pool_alloc(git_pool *pool, uint32_t size)
+static void *pool_alloc(git_pool *pool, size_t size)
 {
 	git_pool_page *page = pool->pages;
 	void *ptr = NULL;
@@ -125,7 +125,7 @@ static int git_pool__ptr_cmp(const void * a, const void * b)
 	}
 }
 
-void git_pool_init(git_pool *pool, uint32_t item_size)
+void git_pool_init(git_pool *pool, size_t item_size)
 {
 	assert(pool);
 	assert(item_size >= 1);
@@ -141,7 +141,7 @@ void git_pool_clear(git_pool *pool)
 	git_vector_free_deep(&pool->allocations);
 }
 
-static void *pool_alloc(git_pool *pool, uint32_t size) {
+static void *pool_alloc(git_pool *pool, size_t size) {
 	void *ptr = NULL;
 	if((ptr = git__malloc(size)) == NULL) {
 		return NULL;
@@ -169,26 +169,26 @@ void git_pool_swap(git_pool *a, git_pool *b)
 	memcpy(b, &temp, sizeof(temp));
 }
 
-static uint32_t alloc_size(git_pool *pool, uint32_t count)
+static size_t alloc_size(git_pool *pool, size_t count)
 {
-	const uint32_t align = sizeof(void *) - 1;
+	const size_t align = sizeof(void *) - 1;
 
 	if (pool->item_size > 1) {
-		const uint32_t item_size = (pool->item_size + align) & ~align;
+		const size_t item_size = (pool->item_size + align) & ~align;
 		return item_size * count;
 	}
 
 	return (count + align) & ~align;
 }
 
-void *git_pool_malloc(git_pool *pool, uint32_t items)
+void *git_pool_malloc(git_pool *pool, size_t items)
 {
 	return pool_alloc(pool, alloc_size(pool, items));
 }
 
-void *git_pool_mallocz(git_pool *pool, uint32_t items)
+void *git_pool_mallocz(git_pool *pool, size_t items)
 {
-	const uint32_t size = alloc_size(pool, items);
+	const size_t size = alloc_size(pool, items);
 	void *ptr = pool_alloc(pool, size);
 	if (ptr)
 		memset(ptr, 0x0, size);
@@ -201,10 +201,10 @@ char *git_pool_strndup(git_pool *pool, const char *str, size_t n)
 
 	assert(pool && str && pool->item_size == sizeof(char));
 
-	if ((uint32_t)(n + 1) < n)
+	if (n == SIZE_MAX)
 		return NULL;
 
-	if ((ptr = git_pool_malloc(pool, (uint32_t)(n + 1))) != NULL) {
+	if ((ptr = git_pool_malloc(pool, (n + 1))) != NULL) {
 		memcpy(ptr, str, n);
 		ptr[n] = '\0';
 	}
@@ -226,14 +226,18 @@ char *git_pool_strdup_safe(git_pool *pool, const char *str)
 char *git_pool_strcat(git_pool *pool, const char *a, const char *b)
 {
 	void *ptr;
-	size_t len_a, len_b;
+	size_t len_a, len_b, total;
 
 	assert(pool && pool->item_size == sizeof(char));
 
 	len_a = a ? strlen(a) : 0;
 	len_b = b ? strlen(b) : 0;
 
-	if ((ptr = git_pool_malloc(pool, (uint32_t)(len_a + len_b + 1))) != NULL) {
+	if (GIT_ADD_SIZET_OVERFLOW(&total, len_a, len_b) ||
+		GIT_ADD_SIZET_OVERFLOW(&total, total, 1))
+		return NULL;
+
+	if ((ptr = git_pool_malloc(pool, total)) != NULL) {
 		if (len_a)
 			memcpy(ptr, a, len_a);
 		if (len_b)
diff --git a/src/pool.h b/src/pool.h
index 92ddf99..23f6899 100644
--- a/src/pool.h
+++ b/src/pool.h
@@ -32,8 +32,8 @@ typedef struct git_pool_page git_pool_page;
  */
 typedef struct {
 	git_pool_page *pages; /* allocated pages */
-	uint32_t item_size;  /* size of single alloc unit in bytes */
-	uint32_t page_size;  /* size of page in bytes */
+	size_t item_size;  /* size of single alloc unit in bytes */
+	size_t page_size;  /* size of page in bytes */
 } git_pool;
 
 #define GIT_POOL_INIT { NULL, 0, 0 }
@@ -57,8 +57,8 @@ typedef struct {
  */
 typedef struct {
 	git_vector allocations;
-	uint32_t item_size;
-	uint32_t page_size;
+	size_t item_size;
+	size_t page_size;
 } git_pool;
 
 #define GIT_POOL_INIT { GIT_VECTOR_INIT, 0, 0 }
@@ -81,7 +81,7 @@ typedef struct {
  * Of course, you can use this in other ways, but those are the
  * two most common patterns.
  */
-extern void git_pool_init(git_pool *pool, uint32_t item_size);
+extern void git_pool_init(git_pool *pool, size_t item_size);
 
 /**
  * Free all items in pool
@@ -96,8 +96,8 @@ extern void git_pool_swap(git_pool *a, git_pool *b);
 /**
  * Allocate space for one or more items from a pool.
  */
-extern void *git_pool_malloc(git_pool *pool, uint32_t items);
-extern void *git_pool_mallocz(git_pool *pool, uint32_t items);
+extern void *git_pool_malloc(git_pool *pool, size_t items);
+extern void *git_pool_mallocz(git_pool *pool, size_t items);
 
 /**
  * Allocate space and duplicate string data into it.
diff --git a/src/rebase.c b/src/rebase.c
index 6de7180..fbba4bc 100644
--- a/src/rebase.c
+++ b/src/rebase.c
@@ -298,7 +298,8 @@ int git_rebase_open(
 	git_rebase *rebase;
 	git_buf path = GIT_BUF_INIT, orig_head_name = GIT_BUF_INIT,
 		orig_head_id = GIT_BUF_INIT, onto_id = GIT_BUF_INIT;
-	int state_path_len, error;
+	size_t state_path_len;
+	int error;
 
 	assert(repo);
 
diff --git a/src/repository.c b/src/repository.c
index d3c7bab..6d7954a 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -878,7 +878,8 @@ int git_repository_open_from_worktree(git_repository **repo_out, git_worktree *w
 {
 	git_buf path = GIT_BUF_INIT;
 	git_repository *repo = NULL;
-	int len, err;
+	size_t len;
+	int err;
 
 	assert(repo_out && wt);
 
diff --git a/src/sortedcache.c b/src/sortedcache.c
index 15ba6fd..8f7ea23 100644
--- a/src/sortedcache.c
+++ b/src/sortedcache.c
@@ -282,7 +282,7 @@ int git_sortedcache_upsert(void **out, git_sortedcache *sc, const char *key)
 	itemlen = sc->item_path_offset + keylen + 1;
 	itemlen = (itemlen + 7) & ~7;
 
-	if ((item = git_pool_mallocz(&sc->pool, (uint32_t)itemlen)) == NULL) {
+	if ((item = git_pool_mallocz(&sc->pool, itemlen)) == NULL) {
 		/* don't use GIT_ERROR_CHECK_ALLOC b/c of lock */
 		error = -1;
 		goto done;
diff --git a/src/trailer.c b/src/trailer.c
index dc8d1ab..f837c97 100644
--- a/src/trailer.c
+++ b/src/trailer.c
@@ -42,15 +42,19 @@ static const char *next_line(const char *str)
 }
 
 /*
- * Return the position of the start of the last line. If len is 0, return -1.
+ * Return the position of the start of the last line. If len is 0, return 0.
  */
-static int last_line(const char *buf, size_t len)
+static bool last_line(size_t *out, const char *buf, size_t len)
 {
-	int i;
+	size_t i;
+
+	*out = 0;
+
 	if (len == 0)
-		return -1;
+		return false;
 	if (len == 1)
-		return 0;
+		return true;
+
 	/*
 	 * Skip the last character (in addition to the null terminator),
 	 * because if the last character is a newline, it is considered as part
@@ -58,31 +62,37 @@ static int last_line(const char *buf, size_t len)
 	 */
 	i = len - 2;
 
-	for (; i >= 0; i--) {
-		if (buf[i] == '\n')
-			return i + 1;
+	for (; i > 0; i--) {
+		if (buf[i] == '\n') {
+			*out = i + 1;
+			return true;
+		}
 	}
-	return 0;
+	return true;
 }
 
 /*
  * If the given line is of the form
- * "<token><optional whitespace><separator>..." or "<separator>...", return the
- * location of the separator. Otherwise, return -1.  The optional whitespace
- * is allowed there primarily to allow things like "Bug #43" where <token> is
- * "Bug" and <separator> is "#".
+ * "<token><optional whitespace><separator>..." or "<separator>...", sets out
+ * to the location of the separator and returns true.  Otherwise, returns
+ * false.  The optional whitespace is allowed there primarily to allow things
+ * like "Bug #43" where <token> is "Bug" and <separator> is "#".
  *
- * The separator-starts-line case (in which this function returns 0) is
- * distinguished from the non-well-formed-line case (in which this function
- * returns -1) because some callers of this function need such a distinction.
+ * The separator-starts-line case (in which this function returns true and
+ * sets out to 0) is distinguished from the non-well-formed-line case (in
+ * which this function returns false) because some callers of this function
+ * need such a distinction.
  */
-static int find_separator(const char *line, const char *separators)
+static bool find_separator(size_t *out, const char *line, const char *separators)
 {
 	int whitespace_found = 0;
 	const char *c;
 	for (c = line; *c; c++) {
-		if (strchr(separators, *c))
-			return c - line;
+		if (strchr(separators, *c)) {
+			*out = c - line;
+			return true;
+		}
+
 		if (!whitespace_found && (isalnum(*c) || *c == '-'))
 			continue;
 		if (c != line && (*c == ' ' || *c == '\t')) {
@@ -91,7 +101,7 @@ static int find_separator(const char *line, const char *separators)
 		}
 		break;
 	}
-	return -1;
+	return false;
 }
 
 /*
@@ -104,10 +114,9 @@ static int find_separator(const char *line, const char *separators)
  * Returns the number of bytes from the tail to ignore, to be fed as
  * the second parameter to append_signoff().
  */
-static int ignore_non_trailer(const char *buf, size_t len)
+static size_t ignore_non_trailer(const char *buf, size_t len)
 {
-	int boc = 0;
-	size_t bol = 0;
+	size_t boc = 0, bol = 0;
 	int in_old_conflicts_block = 0;
 	size_t cutoff = len;
 
@@ -144,7 +153,7 @@ static int ignore_non_trailer(const char *buf, size_t len)
  * Return the position of the start of the patch or the length of str if there
  * is no patch in the message.
  */
-static int find_patch_start(const char *str)
+static size_t find_patch_start(const char *str)
 {
 	const char *s;
 
@@ -160,10 +169,11 @@ static int find_patch_start(const char *str)
  * Return the position of the first trailer line or len if there are no
  * trailers.
  */
-static int find_trailer_start(const char *buf, size_t len)
+static size_t find_trailer_start(const char *buf, size_t len)
 {
 	const char *s;
-	int end_of_title, l, only_spaces = 1;
+	size_t end_of_title, l;
+	int only_spaces = 1;
 	int recognized_prefix = 0, trailer_lines = 0, non_trailer_lines = 0;
 	/*
 	 * Number of possible continuation lines encountered. This will be
@@ -189,12 +199,11 @@ static int find_trailer_start(const char *buf, size_t len)
 	 * trailers, or (ii) contains at least one Git-generated trailer and
 	 * consists of at least 25% trailers.
 	 */
-	for (l = last_line(buf, len);
-	     l >= end_of_title;
-	     l = last_line(buf, l)) {
+	l = len;
+	while (last_line(&l, buf, l) && l >= end_of_title) {
 		const char *bol = buf + l;
 		const char *const *p;
-		int separator_pos;
+		size_t separator_pos = 0;
 
 		if (bol[0] == COMMENT_LINE_CHAR) {
 			non_trailer_lines += possible_continuation_lines;
@@ -223,7 +232,7 @@ static int find_trailer_start(const char *buf, size_t len)
 			}
 		}
 
-		separator_pos = find_separator(bol, TRAILER_SEPARATORS);
+		find_separator(&separator_pos, bol, TRAILER_SEPARATORS);
 		if (separator_pos >= 1 && !isspace(bol[0])) {
 			trailer_lines++;
 			possible_continuation_lines = 0;
@@ -244,7 +253,7 @@ continue_outer_loop:
 }
 
 /* Return the position of the end of the trailers. */
-static int find_trailer_end(const char *buf, size_t len)
+static size_t find_trailer_end(const char *buf, size_t len)
 {
 	return len - ignore_non_trailer(buf, len);
 }
diff --git a/src/transports/local.c b/src/transports/local.c
index 591dd3b..cfc6c2a 100644
--- a/src/transports/local.c
+++ b/src/transports/local.c
@@ -501,7 +501,7 @@ static int local_counting(int stage, unsigned int current, unsigned int total, v
 	if (git_buf_oom(&progress_info))
 		return -1;
 
-	error = t->progress_cb(git_buf_cstr(&progress_info), git_buf_len(&progress_info), t->message_cb_payload);
+	error = t->progress_cb(git_buf_cstr(&progress_info), (int)git_buf_len(&progress_info), t->message_cb_payload);
 	git_buf_dispose(&progress_info);
 
 	return error;
@@ -588,7 +588,7 @@ static int local_download_pack(
 		goto cleanup;
 
 	if (t->progress_cb &&
-	    (error = t->progress_cb(git_buf_cstr(&progress_info), git_buf_len(&progress_info), t->message_cb_payload)) < 0)
+	    (error = t->progress_cb(git_buf_cstr(&progress_info), (int)git_buf_len(&progress_info), t->message_cb_payload)) < 0)
 		goto cleanup;
 
 	/* Walk the objects, building a packfile */
@@ -602,7 +602,7 @@ static int local_download_pack(
 		goto cleanup;
 
 	if (t->progress_cb &&
-	    (error = t->progress_cb(git_buf_cstr(&progress_info), git_buf_len(&progress_info), t->message_cb_payload)) < 0)
+	    (error = t->progress_cb(git_buf_cstr(&progress_info), (int)git_buf_len(&progress_info), t->message_cb_payload)) < 0)
 		goto cleanup;
 
 	if ((error = git_odb_write_pack(&writepack, odb, progress_cb, progress_payload)) != 0)
diff --git a/src/transports/smart.c b/src/transports/smart.c
index f3d55b2..8e8af00 100644
--- a/src/transports/smart.c
+++ b/src/transports/smart.c
@@ -73,7 +73,7 @@ static int git_smart__set_callbacks(
 	return 0;
 }
 
-static int http_header_name_length(const char *http_header)
+static size_t http_header_name_length(const char *http_header)
 {
 	const char *colon = strchr(http_header, ':');
 	if (!colon)
@@ -84,7 +84,7 @@ static int http_header_name_length(const char *http_header)
 static bool is_malformed_http_header(const char *http_header)
 {
 	const char *c;
-	int name_len;
+	size_t name_len;
 
 	/* Disallow \r and \n */
 	c = strchr(http_header, '\r');
@@ -114,7 +114,7 @@ static char *forbidden_custom_headers[] = {
 static bool is_forbidden_custom_header(const char *custom_header)
 {
 	unsigned long i;
-	int name_len = http_header_name_length(custom_header);
+	size_t name_len = http_header_name_length(custom_header);
 
 	/* Disallow headers that we set */
 	for (i = 0; i < ARRAY_SIZE(forbidden_custom_headers); i++)
diff --git a/src/transports/smart_protocol.c b/src/transports/smart_protocol.c
index c251eb8..cfbe7e4 100644
--- a/src/transports/smart_protocol.c
+++ b/src/transports/smart_protocol.c
@@ -604,7 +604,14 @@ int git_smart__download_pack(
 			} else if (pkt->type == GIT_PKT_PROGRESS) {
 				if (t->progress_cb) {
 					git_pkt_progress *p = (git_pkt_progress *) pkt;
-					error = t->progress_cb(p->data, p->len, t->message_cb_payload);
+
+					if (p->len > INT_MAX) {
+						git_error_set(GIT_ERROR_NET, "oversized progress message");
+						error = GIT_ERROR;
+						goto done;
+					}
+
+					error = t->progress_cb(p->data, (int)p->len, t->message_cb_payload);
 				}
 			} else if (pkt->type == GIT_PKT_DATA) {
 				git_pkt_data *p = (git_pkt_data *) pkt;
@@ -839,7 +846,14 @@ static int parse_report(transport_smart *transport, git_push *push)
 			case GIT_PKT_PROGRESS:
 				if (transport->progress_cb) {
 					git_pkt_progress *p = (git_pkt_progress *) pkt;
-					error = transport->progress_cb(p->data, p->len, transport->message_cb_payload);
+
+					if (p->len > INT_MAX) {
+						git_error_set(GIT_ERROR_NET, "oversized progress message");
+						error = GIT_ERROR;
+						goto done;
+					}
+
+					error = transport->progress_cb(p->data, (int)p->len, transport->message_cb_payload);
 				}
 				break;
 			default:
diff --git a/src/transports/winhttp.c b/src/transports/winhttp.c
index 6392a7d..7fc6b70 100644
--- a/src/transports/winhttp.c
+++ b/src/transports/winhttp.c
@@ -48,6 +48,10 @@
 # define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 0x00000800
 #endif
 
+#ifndef DWORD_MAX
+# define DWORD_MAX 0xffffffff
+#endif
+
 static const char *prefix_https = "https://";
 static const char *upload_pack_service = "upload-pack";
 static const char *upload_pack_ls_service_url = "/info/refs?service=git-upload-pack";
@@ -339,6 +343,8 @@ static int apply_credentials(
 {
 	int error = 0;
 
+	GIT_UNUSED(url);
+
 	/* If we have creds, just apply them */
 	if (creds && creds->credtype == GIT_CREDTYPE_USERPASS_PLAINTEXT)
 		error = apply_userpass_credentials(request, target, mechanisms, creds);
@@ -686,6 +692,10 @@ static void CALLBACK winhttp_status(
 {
 	DWORD status;
 
+	GIT_UNUSED(connection);
+	GIT_UNUSED(ctx);
+	GIT_UNUSED(info_len);
+
 	if (code != WINHTTP_CALLBACK_STATUS_SECURE_FAILURE)
 		return;
 
@@ -816,6 +826,11 @@ static int do_send_request(winhttp_stream *s, size_t len, int ignore_length)
 	int attempts;
 	bool success;
 
+	if (len > DWORD_MAX) {
+		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+		return -1;
+	}
+
 	for (attempts = 0; attempts < 5; attempts++) {
 		if (ignore_length) {
 			success = WinHttpSendRequest(s->request,
@@ -826,7 +841,7 @@ static int do_send_request(winhttp_stream *s, size_t len, int ignore_length)
 			success = WinHttpSendRequest(s->request,
 				WINHTTP_NO_ADDITIONAL_HEADERS, 0,
 				WINHTTP_NO_REQUEST_DATA, 0,
-				len, 0);
+				(DWORD)len, 0);
 		}
 
 		if (success || GetLastError() != (DWORD)SEC_E_BUFFER_TOO_SMALL)
diff --git a/src/tree-cache.c b/src/tree-cache.c
index d3ba864..04d86fd 100644
--- a/src/tree-cache.c
+++ b/src/tree-cache.c
@@ -120,12 +120,14 @@ static int read_tree_internal(git_tree_cache **out,
 
 	/* Parse children: */
 	if (tree->children_count > 0) {
-		unsigned int i;
+		size_t i, bufsize;
 
-		tree->children = git_pool_malloc(pool, tree->children_count * sizeof(git_tree_cache *));
+		GIT_ERROR_CHECK_ALLOC_MULTIPLY(&bufsize, tree->children_count, sizeof(git_tree_cache*));
+
+		tree->children = git_pool_malloc(pool, bufsize);
 		GIT_ERROR_CHECK_ALLOC(tree->children);
 
-		memset(tree->children, 0x0, tree->children_count * sizeof(git_tree_cache *));
+		memset(tree->children, 0x0, bufsize);
 
 		for (i = 0; i < tree->children_count; ++i) {
 			if (read_tree_internal(&tree->children[i], &buffer, buffer_end, pool) < 0)
@@ -160,7 +162,7 @@ int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer
 static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_pool *pool)
 {
 	git_repository *repo;
-	size_t i, j, nentries, ntrees;
+	size_t i, j, nentries, ntrees, alloc_size;
 	int error;
 
 	repo = git_tree_owner(tree);
@@ -182,8 +184,10 @@ static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_
 			ntrees++;
 	}
 
+	GIT_ERROR_CHECK_ALLOC_MULTIPLY(&alloc_size, ntrees, sizeof(git_tree_cache *));
+
 	cache->children_count = ntrees;
-	cache->children = git_pool_mallocz(pool, ntrees * sizeof(git_tree_cache *));
+	cache->children = git_pool_mallocz(pool, alloc_size);
 	GIT_ERROR_CHECK_ALLOC(cache->children);
 
 	j = 0;
@@ -232,11 +236,14 @@ int git_tree_cache_read_tree(git_tree_cache **out, const git_tree *tree, git_poo
 
 int git_tree_cache_new(git_tree_cache **out, const char *name, git_pool *pool)
 {
-	size_t name_len;
+	size_t name_len, alloc_size;
 	git_tree_cache *tree;
 
 	name_len = strlen(name);
-	tree = git_pool_malloc(pool, sizeof(git_tree_cache) + name_len + 1);
+
+	GIT_ERROR_CHECK_ALLOC_ADD3(&alloc_size, sizeof(git_tree_cache), name_len, 1);
+
+	tree = git_pool_malloc(pool, alloc_size);
 	GIT_ERROR_CHECK_ALLOC(tree);
 
 	memset(tree, 0x0, sizeof(git_tree_cache));
diff --git a/src/tree.c b/src/tree.c
index 5a48bfc..3799cab 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -340,7 +340,7 @@ size_t git_tree_entrycount(const git_tree *tree)
 	return tree->entries.size;
 }
 
-unsigned int git_treebuilder_entrycount(git_treebuilder *bld)
+size_t git_treebuilder_entrycount(git_treebuilder *bld)
 {
 	assert(bld);
 
diff --git a/src/util.c b/src/util.c
index 508dce5..316a289 100644
--- a/src/util.c
+++ b/src/util.c
@@ -192,7 +192,7 @@ int git__strntol32(int32_t *result, const char *nptr, size_t nptr_len, const cha
 
 	tmp_int = tmp_long & 0xFFFFFFFF;
 	if (tmp_int != tmp_long) {
-		int len = tmp_endptr - nptr;
+		int len = (int)(tmp_endptr - nptr);
 		git_error_set(GIT_ERROR_INVALID, "failed to convert: '%.*s' is too large", len, nptr);
 		return -1;
 	}
@@ -802,23 +802,23 @@ static const int8_t utf8proc_utf8class[256] = {
 	4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0
 };
 
-int git__utf8_charlen(const uint8_t *str, int str_len)
+int git__utf8_charlen(const uint8_t *str, size_t str_len)
 {
-	int length, i;
+	size_t length, i;
 
 	length = utf8proc_utf8class[str[0]];
 	if (!length)
 		return -1;
 
-	if (str_len >= 0 && length > str_len)
-		return -str_len;
+	if (str_len > 0 && length > str_len)
+		return -1;
 
 	for (i = 1; i < length; i++) {
 		if ((str[i] & 0xC0) != 0x80)
-			return -i;
+			return -1;
 	}
 
-	return length;
+	return (int)length;
 }
 
 int git__utf8_iterate(const uint8_t *str, int str_len, int32_t *dst)
diff --git a/src/wildmatch.c b/src/wildmatch.c
index 865f3f0..a894e48 100644
--- a/src/wildmatch.c
+++ b/src/wildmatch.c
@@ -250,7 +250,7 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 					for (s = p += 2; (p_ch = *p) && p_ch != ']'; p++) {} /*SHARED ITERATOR*/
 					if (!p_ch)
 						return WM_ABORT_ALL;
-					i = p - s - 1;
+					i = (int)(p - s - 1);
 					if (i < 0 || p[-1] != ':') {
 						/* Didn't find ":]", so treat like a normal set. */
 						p = s - 2;
diff --git a/src/win32/path_w32.c b/src/win32/path_w32.c
index b955b02..eda85ab 100644
--- a/src/win32/path_w32.c
+++ b/src/win32/path_w32.c
@@ -140,14 +140,24 @@ int git_win32_path_canonicalize(git_win32_path path)
 
 	*to = L'\0';
 
-	return (to - path);
+	if ((to - path) > INT_MAX) {
+		SetLastError(ERROR_FILENAME_EXCED_RANGE);
+		return -1;
+	}
+
+	return (int)(to - path);
 }
 
 int git_win32_path__cwd(wchar_t *out, size_t len)
 {
 	int cwd_len;
 
-	if ((cwd_len = path__cwd(out, len)) < 0)
+	if (len > INT_MAX) {
+		errno = ENAMETOOLONG;
+		return -1;
+	}
+
+	if ((cwd_len = path__cwd(out, (int)len)) < 0)
 		return -1;
 
 	/* UNC paths */
diff --git a/src/win32/thread.c b/src/win32/thread.c
index 2d96005..c3a6c0e 100644
--- a/src/win32/thread.c
+++ b/src/win32/thread.c
@@ -42,15 +42,15 @@ int git_threads_init(void)
 	HMODULE hModule = GetModuleHandleW(L"kernel32");
 
 	if (hModule) {
-		win32_srwlock_initialize = (win32_srwlock_fn)
+		win32_srwlock_initialize = (win32_srwlock_fn)(void *)
 			GetProcAddress(hModule, "InitializeSRWLock");
-		win32_srwlock_acquire_shared = (win32_srwlock_fn)
+		win32_srwlock_acquire_shared = (win32_srwlock_fn)(void *)
 			GetProcAddress(hModule, "AcquireSRWLockShared");
-		win32_srwlock_release_shared = (win32_srwlock_fn)
+		win32_srwlock_release_shared = (win32_srwlock_fn)(void *)
 			GetProcAddress(hModule, "ReleaseSRWLockShared");
-		win32_srwlock_acquire_exclusive = (win32_srwlock_fn)
+		win32_srwlock_acquire_exclusive = (win32_srwlock_fn)(void *)
 			GetProcAddress(hModule, "AcquireSRWLockExclusive");
-		win32_srwlock_release_exclusive = (win32_srwlock_fn)
+		win32_srwlock_release_exclusive = (win32_srwlock_fn)(void *)
 			GetProcAddress(hModule, "ReleaseSRWLockExclusive");
 	}
 
diff --git a/src/win32/w32_buffer.c b/src/win32/w32_buffer.c
index 22ea9a1..b78a7e6 100644
--- a/src/win32/w32_buffer.c
+++ b/src/win32/w32_buffer.c
@@ -25,13 +25,17 @@ int git_buf_put_w(git_buf *buf, const wchar_t *string_w, size_t len_w)
 	int utf8_len, utf8_write_len;
 	size_t new_size;
 
-	if (!len_w)
+	if (!len_w) {
 		return 0;
+	} else if (len_w > INT_MAX) {
+		git_error_set_oom();
+		return -1;
+	}
 
 	assert(string_w);
 
 	/* Measure the string necessary for conversion */
-	if ((utf8_len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, string_w, len_w, NULL, 0, NULL, NULL)) == 0)
+	if ((utf8_len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, string_w, (int)len_w, NULL, 0, NULL, NULL)) == 0)
 		return 0;
 
 	assert(utf8_len > 0);
@@ -43,7 +47,7 @@ int git_buf_put_w(git_buf *buf, const wchar_t *string_w, size_t len_w)
 		return -1;
 
 	if ((utf8_write_len = WideCharToMultiByte(
-			CP_UTF8, WC_ERR_INVALID_CHARS, string_w, len_w, &buf->ptr[buf->size], utf8_len, NULL, NULL)) == 0)
+			CP_UTF8, WC_ERR_INVALID_CHARS, string_w, (int)len_w, &buf->ptr[buf->size], utf8_len, NULL, NULL)) == 0)
 		return handle_wc_error();
 
 	assert(utf8_write_len == utf8_len);
diff --git a/src/win32/w32_stack.c b/src/win32/w32_stack.c
index c9d6b1a..6eb7196 100644
--- a/src/win32/w32_stack.c
+++ b/src/win32/w32_stack.c
@@ -13,11 +13,6 @@
 #include "win32/posix.h"
 #include "hash.h"
 
-/**
- * This is supposedly defined in WinBase.h (from Windows.h) but there were linker issues.
- */
-USHORT WINAPI RtlCaptureStackBackTrace(ULONG, ULONG, PVOID*, PULONG);
-
 static bool   g_win32_stack_initialized = false;
 static HANDLE g_win32_stack_process = INVALID_HANDLE_VALUE;
 static git_win32__stack__aux_cb_alloc  g_aux_cb_alloc  = NULL;
diff --git a/src/win32/w32_util.c b/src/win32/w32_util.c
index 10e17fc..fe4b75b 100644
--- a/src/win32/w32_util.c
+++ b/src/win32/w32_util.c
@@ -93,3 +93,34 @@ int git_win32__hidden(bool *out, const char *path)
 	*out = (attrs & FILE_ATTRIBUTE_HIDDEN) ? true : false;
 	return 0;
 }
+
+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;
+
+		if (git_win32_path_readlink_w(target, path) >= 0) {
+			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 */
+			if ((st->st_size = git__utf16_to_8(NULL, 0, target)) < 0) {
+				git_error_set(GIT_ERROR_OS, "could not convert reparse point name for '%ls'", path);
+				return -1;
+			}
+		}
+	}
+
+	return 0;
+}
diff --git a/src/win32/w32_util.h b/src/win32/w32_util.h
index 3a79425..ac19115 100644
--- a/src/win32/w32_util.h
+++ b/src/win32/w32_util.h
@@ -59,6 +59,11 @@ extern int git_win32__set_hidden(const char *path, bool hidden);
  */
 extern int git_win32__hidden(bool *hidden, const char *path);
 
+extern int git_win32__file_attribute_to_stat(
+	struct stat *st,
+	const WIN32_FILE_ATTRIBUTE_DATA *attrdata,
+	const wchar_t *path);
+
 /**
  * Converts a FILETIME structure to a struct timespec.
  *
@@ -136,35 +141,4 @@ GIT_INLINE(void) git_win32__file_information_to_stat(
 		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;
-
-		if (git_win32_path_readlink_w(target, path) >= 0) {
-			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 */
-			if ((st->st_size = git__utf16_to_8(NULL, 0, target)) < 0) {
-				git_error_set(GIT_ERROR_OS, "could not convert reparse point name for '%ls'", path);
-				return -1;
-			}
-		}
-	}
-
-	return 0;
-}
-
 #endif
diff --git a/src/worktree.c b/src/worktree.c
index 7213b8a..ef4ebfd 100644
--- a/src/worktree.c
+++ b/src/worktree.c
@@ -34,7 +34,7 @@ int git_worktree_list(git_strarray *wts, git_repository *repo)
 	git_vector worktrees = GIT_VECTOR_INIT;
 	git_buf path = GIT_BUF_INIT;
 	char *worktree;
-	unsigned i, len;
+	size_t i, len;
 	int error;
 
 	assert(wts && repo);
diff --git a/tests/clar.c b/tests/clar.c
index 27d35e1..459ece0 100644
--- a/tests/clar.c
+++ b/tests/clar.c
@@ -67,7 +67,7 @@
 #		define PRIxZ "Ix"
 #	endif
 
-#	ifdef _MSC_VER
+#	if defined(_MSC_VER) || defined(__MINGW32__)
 	typedef struct stat STAT_T;
 #	else
 	typedef struct _stat STAT_T;
diff --git a/tests/core/link.c b/tests/core/link.c
index 8cd6b01..1e5ed45 100644
--- a/tests/core/link.c
+++ b/tests/core/link.c
@@ -54,7 +54,7 @@ static void do_symlink(const char *old, const char *new, int is_dir)
 	create_symlink_func pCreateSymbolicLink;
 
 	cl_assert(module = GetModuleHandle("kernel32"));
-	cl_assert(pCreateSymbolicLink = (create_symlink_func)GetProcAddress(module, "CreateSymbolicLinkA"));
+	cl_assert(pCreateSymbolicLink = (create_symlink_func)(void *)GetProcAddress(module, "CreateSymbolicLinkA"));
 
 	cl_win32_pass(pCreateSymbolicLink(new, old, is_dir));
 #endif
@@ -70,7 +70,7 @@ static void do_hardlink(const char *old, const char *new)
 	create_hardlink_func pCreateHardLink;
 
 	cl_assert(module = GetModuleHandle("kernel32"));
-	cl_assert(pCreateHardLink = (create_hardlink_func)GetProcAddress(module, "CreateHardLinkA"));
+	cl_assert(pCreateHardLink = (create_hardlink_func)(void *)GetProcAddress(module, "CreateHardLinkA"));
 
 	cl_win32_pass(pCreateHardLink(new, old, 0));
 #endif
diff --git a/tests/object/tree/read.c b/tests/object/tree/read.c
index 6fe224b..de2e64c 100644
--- a/tests/object/tree/read.c
+++ b/tests/object/tree/read.c
@@ -75,8 +75,9 @@ void test_object_tree_read__two(void)
 }
 
 #define BIGFILE "bigfile"
+
 #ifdef GIT_ARCH_64
-# define BIGFILE_SIZE (size_t)4 * 1024 * 1024 * 1024 /* 4 GiB */
+#define BIGFILE_SIZE (off_t)4294967296
 #else
 # define BIGFILE_SIZE SIZE_MAX
 #endif
diff --git a/tests/online/fetchhead.c b/tests/online/fetchhead.c
index ae72dde..7e9ca7e 100644
--- a/tests/online/fetchhead.c
+++ b/tests/online/fetchhead.c
@@ -35,10 +35,10 @@ static void fetchhead_test_clone(void)
 	cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
 }
 
-static int count_references(void)
+static size_t count_references(void)
 {
 	git_strarray array;
-	int refs;
+	size_t refs;
 
 	cl_git_pass(git_reference_list(&array, g_repo));
 	refs = array.count;
@@ -118,7 +118,7 @@ void test_online_fetchhead__no_merges(void)
 void test_online_fetchhead__explicit_dst_refspec_creates_branch(void)
 {
 	git_reference *ref;
-	int refs;
+	size_t refs;
 
 	fetchhead_test_clone();
 	refs = count_references();
@@ -133,7 +133,7 @@ void test_online_fetchhead__explicit_dst_refspec_creates_branch(void)
 void test_online_fetchhead__empty_dst_refspec_creates_no_branch(void)
 {
 	git_reference *ref;
-	int refs;
+	size_t refs;
 
 	fetchhead_test_clone();
 	refs = count_references();
@@ -146,7 +146,7 @@ void test_online_fetchhead__empty_dst_refspec_creates_no_branch(void)
 
 void test_online_fetchhead__colon_only_dst_refspec_creates_no_branch(void)
 {
-	int refs;
+	size_t refs;
 
 	fetchhead_test_clone();
 	refs = count_references();
diff --git a/tests/worktree/worktree.c b/tests/worktree/worktree.c
index d41aa2c..5e99dbf 100644
--- a/tests/worktree/worktree.c
+++ b/tests/worktree/worktree.c
@@ -42,7 +42,7 @@ void test_worktree_worktree__list_with_invalid_worktree_dirs(void)
 	};
 	git_buf path = GIT_BUF_INIT;
 	git_strarray wts;
-	unsigned i, j, len;
+	size_t i, j, len;
 
 	cl_git_pass(git_buf_printf(&path, "%s/worktrees/invalid",
 		    fixture.repo->commondir));