Commit 44ef8b1b300f0cd3d8572fa1b40d257462f28240

Russell Belfer 2012-04-13T13:00:10

Fix warnings on 64-bit windows builds This fixes all the warnings on win64 except those in deps, which come from the regex code.

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
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
diff --git a/src/netops.c b/src/netops.c
index 2d759fd..e2fec0b 100644
--- a/src/netops.c
+++ b/src/netops.c
@@ -46,7 +46,7 @@ static void net_set_error(const char *str)
 }
 #endif
 
-void gitno_buffer_setup(gitno_buffer *buf, char *data, unsigned int len, int fd)
+void gitno_buffer_setup(gitno_buffer *buf, char *data, unsigned int len, GIT_SOCKET fd)
 {
 	memset(buf, 0x0, sizeof(gitno_buffer));
 	memset(data, 0x0, len);
@@ -60,17 +60,13 @@ int gitno_recv(gitno_buffer *buf)
 {
 	int ret;
 
-	ret = recv(buf->fd, buf->data + buf->offset, buf->len - buf->offset, 0);
-	if (ret == 0) /* Orderly shutdown, so exit */
-		return 0;
-
+	ret = p_recv(buf->fd, buf->data + buf->offset, buf->len - buf->offset, 0);
 	if (ret < 0) {
-		net_set_error("Error receiving data");
+		net_set_error("Error receiving socket data");
 		return -1;
 	}
 
 	buf->offset += ret;
-
 	return ret;
 }
 
@@ -97,12 +93,12 @@ void gitno_consume_n(gitno_buffer *buf, size_t cons)
 	buf->offset -= cons;
 }
 
-int gitno_connect(const char *host, const char *port)
+GIT_SOCKET gitno_connect(const char *host, const char *port)
 {
-	struct addrinfo *info, *p;
+	struct addrinfo *info = NULL, *p;
 	struct addrinfo hints;
 	int ret;
-	GIT_SOCKET s;
+	GIT_SOCKET s = INVALID_SOCKET;
 
 	memset(&hints, 0x0, sizeof(struct addrinfo));
 	hints.ai_family = AF_UNSPEC;
@@ -110,36 +106,30 @@ int gitno_connect(const char *host, const char *port)
 
 	if ((ret = getaddrinfo(host, port, &hints, &info)) < 0) {
 		giterr_set(GITERR_NET, "Failed to resolve address for %s: %s", host, gai_strerror(ret));
-		return -1;
+		return INVALID_SOCKET;
 	}
 
 	for (p = info; p != NULL; p = p->ai_next) {
 		s = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
-#ifdef GIT_WIN32
 		if (s == INVALID_SOCKET) {
-#else
-		if (s < 0) {
-#endif
-			net_set_error("Error creating socket");
-			freeaddrinfo(info);
-			return -1;
+			net_set_error("error creating socket");
+			break;
 		}
 
-		ret = connect(s, p->ai_addr, p->ai_addrlen);
-		/* If we can't connect, try the next one */
-		if (ret < 0) {
-			close(s);
-			continue;
-		}
+		if (connect(s, p->ai_addr, (socklen_t)p->ai_addrlen) == 0)
+			break;
 
-		/* Return the socket */
-		freeaddrinfo(info);
-		return s;
+		/* If we can't connect, try the next one */
+		gitno_close(s);
+		s = INVALID_SOCKET;
 	}
 
 	/* Oops, we couldn't connect to any address */
-	giterr_set(GITERR_OS, "Failed to connect to %s", host);
-	return -1;
+	if (s == INVALID_SOCKET && p == NULL)
+		giterr_set(GITERR_OS, "Failed to connect to %s", host);
+
+	freeaddrinfo(info);
+	return s;
 }
 
 int gitno_send(GIT_SOCKET s, const char *msg, size_t len, int flags)
@@ -150,7 +140,7 @@ int gitno_send(GIT_SOCKET s, const char *msg, size_t len, int flags)
 	while (off < len) {
 		errno = 0;
 
-		ret = send(s, msg + off, len - off, flags);
+		ret = p_send(s, msg + off, len - off, flags);
 		if (ret < 0) {
 			net_set_error("Error sending data");
 			return -1;
@@ -159,7 +149,7 @@ int gitno_send(GIT_SOCKET s, const char *msg, size_t len, int flags)
 		off += ret;
 	}
 
-	return off;
+	return (int)off;
 }
 
 
@@ -187,7 +177,7 @@ int gitno_select_in(gitno_buffer *buf, long int sec, long int usec)
 	FD_SET(buf->fd, &fds);
 
 	/* The select(2) interface is silly */
-	return select(buf->fd + 1, &fds, NULL, NULL, &tv);
+	return select((int)buf->fd + 1, &fds, NULL, NULL, &tv);
 }
 
 int gitno_extract_host_and_port(char **host, char **port, const char *url, const char *default_port)
@@ -198,8 +188,8 @@ int gitno_extract_host_and_port(char **host, char **port, const char *url, const
 	slash = strchr(url, '/');
 
 	if (slash == NULL) {
-			giterr_set(GITERR_NET, "Malformed URL: missing /");
-			return -1;
+		giterr_set(GITERR_NET, "Malformed URL: missing /");
+		return -1;
 	}
 
 	if (colon == NULL) {
diff --git a/src/netops.h b/src/netops.h
index 01ad971..f370019 100644
--- a/src/netops.h
+++ b/src/netops.h
@@ -7,11 +7,7 @@
 #ifndef INCLUDE_netops_h__
 #define INCLUDE_netops_h__
 
-#ifndef GIT_WIN32
-typedef int GIT_SOCKET;
-#else
-typedef SOCKET GIT_SOCKET;
-#endif
+#include "posix.h"
 
 typedef struct gitno_buffer {
 	char *data;
@@ -20,12 +16,12 @@ typedef struct gitno_buffer {
 	GIT_SOCKET fd;
 } gitno_buffer;
 
-void gitno_buffer_setup(gitno_buffer *buf, char *data, unsigned int len, int fd);
+void gitno_buffer_setup(gitno_buffer *buf, char *data, unsigned int len, GIT_SOCKET fd);
 int gitno_recv(gitno_buffer *buf);
 void gitno_consume(gitno_buffer *buf, const char *ptr);
 void gitno_consume_n(gitno_buffer *buf, size_t cons);
 
-int gitno_connect(const char *host, const char *port);
+GIT_SOCKET gitno_connect(const char *host, const char *port);
 int gitno_send(GIT_SOCKET s, const char *msg, size_t len, int flags);
 int gitno_close(GIT_SOCKET s);
 int gitno_send_chunk_size(int s, size_t len);
diff --git a/src/odb_pack.c b/src/odb_pack.c
index 1a1fa55..b91e3ca 100644
--- a/src/odb_pack.c
+++ b/src/odb_pack.c
@@ -212,7 +212,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;
-	size_t i;
+	unsigned int i;
 
 	if (git__suffixcmp(path->ptr, ".idx") != 0)
 		return 0; /* not an index */
@@ -266,7 +266,7 @@ static int packfile_refresh_all(struct pack_backend *backend)
 static int pack_entry_find(struct git_pack_entry *e, struct pack_backend *backend, const git_oid *oid)
 {
 	int error;
-	size_t i;
+	unsigned int i;
 
 	if ((error = packfile_refresh_all(backend)) < 0)
 		return error;
@@ -298,7 +298,7 @@ static int pack_entry_find_prefix(
 	unsigned int len)
 {
 	int error;
-	size_t i;
+	unsigned int i;
 	unsigned found = 0;
 
 	if ((error = packfile_refresh_all(backend)) < 0)
@@ -423,7 +423,7 @@ static int pack_backend__exists(git_odb_backend *backend, const git_oid *oid)
 static void pack_backend__free(git_odb_backend *_backend)
 {
 	struct pack_backend *backend;
-	size_t i;
+	unsigned int i;
 
 	assert(_backend);
 
diff --git a/src/oid.c b/src/oid.c
index 7f0a520..d072f0f 100644
--- a/src/oid.c
+++ b/src/oid.c
@@ -260,6 +260,8 @@ git_oid_shorten *git_oid_shorten_new(size_t min_length)
 {
 	git_oid_shorten *os;
 
+	assert((size_t)((int)min_length) == min_length);
+
 	os = git__calloc(1, sizeof(git_oid_shorten));
 	if (os == NULL)
 		return NULL;
@@ -270,7 +272,7 @@ git_oid_shorten *git_oid_shorten_new(size_t min_length)
 	}
 
 	os->node_count = 1;
-	os->min_length = min_length;
+	os->min_length = (int)min_length;
 
 	return os;
 }
@@ -328,7 +330,8 @@ void git_oid_shorten_free(git_oid_shorten *os)
  */
 int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid)
 {
-	int i, is_leaf;
+	int i;
+	bool is_leaf;
 	node_index idx;
 
 	if (os->full)
@@ -338,7 +341,7 @@ int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid)
 		return os->min_length;
 
 	idx = 0;
-	is_leaf = 0;
+	is_leaf = false;
 
 	for (i = 0; i < GIT_OID_HEXSZ; ++i) {
 		int c = git__fromhex(text_oid[i]);
@@ -368,11 +371,11 @@ int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid)
 		}
 
 		idx = node->children[c];
-		is_leaf = 0;
+		is_leaf = false;
 
 		if (idx < 0) {
 			node->children[c] = idx = -idx;
-			is_leaf = 1;
+			is_leaf = true;
 		}
 	}
 
diff --git a/src/pack.c b/src/pack.c
index a4e5069..af8be25 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -165,6 +165,7 @@ static int pack_index_open(struct git_pack_file *p)
 {
 	char *idx_name;
 	int error;
+	size_t name_len, offset;
 
 	if (p->index_map.data)
 		return 0;
@@ -172,7 +173,11 @@ static int pack_index_open(struct git_pack_file *p)
 	idx_name = git__strdup(p->pack_name);
 	GITERR_CHECK_ALLOC(idx_name);
 
-	strcpy(idx_name + strlen(idx_name) - strlen(".pack"), ".idx");
+	name_len = strlen(idx_name);
+	offset = name_len - strlen(".pack");
+	assert(offset < name_len); /* make sure no underflow */
+
+	strncpy(idx_name + offset, ".idx", name_len - offset);
 
 	error = pack_index_check(idx_name, p);
 	git__free(idx_name);
@@ -474,7 +479,7 @@ git_off_t get_delta_base(
  *
  ***********************************************************/
 
-static struct git_pack_file *packfile_alloc(int extra)
+static struct git_pack_file *packfile_alloc(size_t extra)
 {
 	struct git_pack_file *p = git__calloc(1, sizeof(*p) + extra);
 	if (p != NULL)
diff --git a/src/path.c b/src/path.c
index 3c1a723..a7cf440 100644
--- a/src/path.c
+++ b/src/path.c
@@ -464,21 +464,22 @@ int git_path_find_dir(git_buf *dir, const char *path, const char *base)
 	return error;
 }
 
-int git_path_cmp(const char *name1, int len1, int isdir1,
-		const char *name2, int len2, int isdir2)
+int git_path_cmp(
+	const char *name1, size_t len1, int isdir1,
+	const char *name2, size_t len2, int isdir2)
 {
-	int len = len1 < len2 ? len1 : len2;
+	size_t len = len1 < len2 ? len1 : len2;
 	int cmp;
 
 	cmp = memcmp(name1, name2, len);
 	if (cmp)
 		return cmp;
 	if (len1 < len2)
-		return ((!isdir1 && !isdir2) ? -1 :
-						(isdir1 ? '/' - name2[len1] : name2[len1] - '/'));
+		return (!isdir1 && !isdir2) ? -1 :
+			(isdir1 ? '/' - name2[len1] : name2[len1] - '/');
 	if (len1 > len2)
-		return ((!isdir1 && !isdir2) ? 1 :
-						(isdir2 ? name1[len2] - '/' : '/' - name1[len2]));
+		return (!isdir1 && !isdir2) ? 1 :
+			(isdir2 ? name1[len2] - '/' : '/' - name1[len2]);
 	return 0;
 }
 
diff --git a/src/path.h b/src/path.h
index eb397d1..fd76805 100644
--- a/src/path.h
+++ b/src/path.h
@@ -204,8 +204,8 @@ extern int git_path_direach(
  * Sort function to order two paths.
  */
 extern int git_path_cmp(
-	const char *name1, int len1, int isdir1,
-	const char *name2, int len2, int isdir2);
+	const char *name1, size_t len1, int isdir1,
+	const char *name2, size_t len2, int isdir2);
 
 /**
  * Invoke callback up path directory by directory until the ceiling is
diff --git a/src/pkt.c b/src/pkt.c
index f8af7e2..ee113cd 100644
--- a/src/pkt.c
+++ b/src/pkt.c
@@ -102,6 +102,7 @@ static int comment_pkt(git_pkt **out, const char *line, size_t len)
  */
 static int ref_pkt(git_pkt **out, const char *line, size_t len)
 {
+	int error;
 	git_pkt_ref *pkt;
 
 	pkt = git__malloc(sizeof(git_pkt_ref));
@@ -109,14 +110,13 @@ static int ref_pkt(git_pkt **out, const char *line, size_t len)
 
 	memset(pkt, 0x0, sizeof(git_pkt_ref));
 	pkt->type = GIT_PKT_REF;
-	if (git_oid_fromstr(&pkt->head.oid, line) < 0) {
-		giterr_set(GITERR_NET, "Error parsing pkt-line");
+	if ((error = git_oid_fromstr(&pkt->head.oid, line)) < 0)
 		goto error_out;
-	}
 
 	/* Check for a bit of consistency */
 	if (line[GIT_OID_HEXSZ] != ' ') {
 		giterr_set(GITERR_NET, "Error parsing pkt-line");
+		error = -1;
 		goto error_out;
 	}
 
@@ -138,30 +138,32 @@ static int ref_pkt(git_pkt **out, const char *line, size_t len)
 	}
 
 	*out = (git_pkt *)pkt;
-
 	return 0;
 
 error_out:
 	git__free(pkt);
-	return -1;
+	return error;
 }
 
-static int parse_len(const char *line)
+static int32_t parse_len(const char *line)
 {
 	char num[PKT_LEN_SIZE + 1];
-	int i, len;
+	int i, error;
+	int32_t len;
 	const char *num_end;
 
 	memcpy(num, line, PKT_LEN_SIZE);
 	num[PKT_LEN_SIZE] = '\0';
 
 	for (i = 0; i < PKT_LEN_SIZE; ++i) {
-		if (!isxdigit(num[i]))
-			return GIT_ENOTNUM;
+		if (!isxdigit(num[i])) {
+			giterr_set(GITERR_NET, "Found invalid hex digit in length");
+			return -1;
+		}
 	}
 
-	if (git__strtol32(&len, num, &num_end, 16) < 0)
-		return -1;
+	if ((error = git__strtol32(&len, num, &num_end, 16)) < 0)
+		return error;
 
 	return len;
 }
@@ -179,16 +181,20 @@ static int parse_len(const char *line)
  * in ASCII hexadecimal (including itself)
  */
 
-int git_pkt_parse_line(git_pkt **head, const char *line, const char **out, size_t bufflen)
+int git_pkt_parse_line(
+	git_pkt **head, const char *line, const char **out, size_t bufflen)
 {
-	int ret = 0;
-	size_t len;
+	int ret;
+	int32_t len;
 
 	/* Not even enough for the length */
-	if (bufflen > 0 && bufflen < PKT_LEN_SIZE)
-		return GIT_ESHORTBUFFER;
+	if (bufflen > 0 && bufflen < PKT_LEN_SIZE) {
+		giterr_set(GITERR_NET, "Insufficient buffer data");
+		return -1;
+	}
 
-	if ((ret = parse_len(line)) < 0) {
+	len = parse_len(line);
+	if (len < 0) {
 		/*
 		 * If we fail to parse the length, it might be because the
 		 * server is trying to send us the packfile already.
@@ -198,18 +204,17 @@ int git_pkt_parse_line(git_pkt **head, const char *line, const char **out, size_
 			return pack_pkt(head);
 		}
 
-		giterr_set(GITERR_NET, "Error parsing pkt-line");
-		return -1;
+		return (int)len;
 	}
 
-	len = ret;
-
 	/*
 	 * If we were given a buffer length, then make sure there is
 	 * enough in the buffer to satisfy this line
 	 */
-	if (bufflen > 0 && bufflen < len)
-		return GIT_ESHORTBUFFER;
+	if (bufflen > 0 && bufflen < (size_t)len) {
+		giterr_set(GITERR_NET, "Insufficient buffer data for packet length");
+		return -1;
+	}
 
 	line += PKT_LEN_SIZE;
 	/*
@@ -245,7 +250,7 @@ int git_pkt_parse_line(git_pkt **head, const char *line, const char **out, size_
 
 void git_pkt_free(git_pkt *pkt)
 {
-	if(pkt->type == GIT_PKT_REF) {
+	if (pkt->type == GIT_PKT_REF) {
 		git_pkt_ref *p = (git_pkt_ref *) pkt;
 		git__free(p->head.name);
 	}
@@ -258,7 +263,7 @@ int git_pkt_buffer_flush(git_buf *buf)
 	return git_buf_put(buf, pkt_flush_str, strlen(pkt_flush_str));
 }
 
-int git_pkt_send_flush(int s)
+int git_pkt_send_flush(GIT_SOCKET s)
 {
 
 	return gitno_send(s, pkt_flush_str, strlen(pkt_flush_str), 0);
@@ -268,12 +273,14 @@ static int buffer_want_with_caps(git_remote_head *head, git_transport_caps *caps
 {
 	char capstr[20];
 	char oid[GIT_OID_HEXSZ +1] = {0};
-	int len;
+	unsigned int len;
 
 	if (caps->ofs_delta)
-		strcpy(capstr, GIT_CAP_OFS_DELTA);
+		strncpy(capstr, GIT_CAP_OFS_DELTA, sizeof(capstr));
 
-	len = strlen("XXXXwant ") + GIT_OID_HEXSZ + 1 /* NUL */ + strlen(capstr) + 1 /* LF */;
+	len = (unsigned int)
+		(strlen("XXXXwant ") + GIT_OID_HEXSZ + 1 /* NUL */ +
+		 strlen(capstr) + 1 /* LF */);
 	git_buf_grow(buf, buf->size + len);
 
 	git_oid_fmt(oid, &head->oid);
@@ -283,15 +290,14 @@ static int buffer_want_with_caps(git_remote_head *head, git_transport_caps *caps
 static int send_want_with_caps(git_remote_head *head, git_transport_caps *caps, GIT_SOCKET fd)
 {
 	git_buf buf = GIT_BUF_INIT;
-	int error;
+	int ret;
 
 	if (buffer_want_with_caps(head, caps, &buf) < 0)
 		return -1;
 
-	error = gitno_send(fd, buf.ptr, buf.size, 0);
+	ret = gitno_send(fd, buf.ptr, buf.size, 0);
 	git_buf_free(&buf);
-
-	return error;
+	return ret;
 }
 
 /*
@@ -335,7 +341,7 @@ int git_pkt_buffer_wants(const git_vector *refs, git_transport_caps *caps, git_b
 	return git_pkt_buffer_flush(buf);
 }
 
-int git_pkt_send_wants(const git_vector *refs, git_transport_caps *caps, int fd)
+int git_pkt_send_wants(const git_vector *refs, git_transport_caps *caps, GIT_SOCKET fd)
 {
 	unsigned int i = 0;
 	char buf[sizeof(pkt_want_prefix) + GIT_OID_HEXSZ + 1];
@@ -357,6 +363,7 @@ int git_pkt_send_wants(const git_vector *refs, git_transport_caps *caps, int fd)
 
 		if (send_want_with_caps(refs->contents[i], caps, fd) < 0)
 			return -1;
+
 		/* Increase it here so it's correct whether we run this or not */
 		i++;
 	}
@@ -384,7 +391,7 @@ int git_pkt_buffer_have(git_oid *oid, git_buf *buf)
 	return git_buf_printf(buf, "%s%s\n", pkt_have_prefix, oidhex);
 }
 
-int git_pkt_send_have(git_oid *oid, int fd)
+int git_pkt_send_have(git_oid *oid, GIT_SOCKET fd)
 {
 	char buf[] = "0032have 0000000000000000000000000000000000000000\n";
 
@@ -398,7 +405,7 @@ int git_pkt_buffer_done(git_buf *buf)
 	return git_buf_puts(buf, pkt_done_str);
 }
 
-int git_pkt_send_done(int fd)
+int git_pkt_send_done(GIT_SOCKET fd)
 {
 	return gitno_send(fd, pkt_done_str, strlen(pkt_done_str), 0);
 }
diff --git a/src/pkt.h b/src/pkt.h
index b0bc089..1f8d62e 100644
--- a/src/pkt.h
+++ b/src/pkt.h
@@ -11,6 +11,7 @@
 #include "common.h"
 #include "transport.h"
 #include "buffer.h"
+#include "posix.h"
 #include "git2/net.h"
 
 enum git_pkt_type {
@@ -65,13 +66,13 @@ typedef struct {
 
 int git_pkt_parse_line(git_pkt **head, const char *line, const char **out, size_t len);
 int git_pkt_buffer_flush(git_buf *buf);
-int git_pkt_send_flush(int s);
+int git_pkt_send_flush(GIT_SOCKET s);
 int git_pkt_buffer_done(git_buf *buf);
-int git_pkt_send_done(int s);
+int git_pkt_send_done(GIT_SOCKET s);
 int git_pkt_buffer_wants(const git_vector *refs, git_transport_caps *caps, git_buf *buf);
-int git_pkt_send_wants(const git_vector *refs, git_transport_caps *caps, int fd);
+int git_pkt_send_wants(const git_vector *refs, git_transport_caps *caps, GIT_SOCKET fd);
 int git_pkt_buffer_have(git_oid *oid, git_buf *buf);
-int git_pkt_send_have(git_oid *oid, int fd);
+int git_pkt_send_have(git_oid *oid, GIT_SOCKET fd);
 void git_pkt_free(git_pkt *pkt);
 
 #endif
diff --git a/src/posix.c b/src/posix.c
index 9778809..a3f81d7 100644
--- a/src/posix.c
+++ b/src/posix.c
@@ -58,7 +58,13 @@ int p_read(git_file fd, void *buf, size_t cnt)
 {
 	char *b = buf;
 	while (cnt) {
-		ssize_t r = read(fd, b, cnt);
+		ssize_t r;
+#ifdef GIT_WIN32
+		assert((size_t)((unsigned int)cnt) == cnt);
+		r = read(fd, b, (unsigned int)cnt);
+#else
+		r = read(fd, b, cnt);
+#endif
 		if (r < 0) {
 			if (errno == EINTR || errno == EAGAIN)
 				continue;
@@ -76,7 +82,13 @@ int p_write(git_file fd, const void *buf, size_t cnt)
 {
 	const char *b = buf;
 	while (cnt) {
-		ssize_t r = write(fd, b, cnt);
+		ssize_t r;
+#ifdef GIT_WIN32
+		assert((size_t)((unsigned int)cnt) == cnt);
+		r = write(fd, b, (unsigned int)cnt);
+#else
+		r = write(fd, b, cnt);
+#endif
 		if (r < 0) {
 			if (errno == EINTR || errno == EAGAIN)
 				continue;
diff --git a/src/posix.h b/src/posix.h
index fb17cba..752d515 100644
--- a/src/posix.h
+++ b/src/posix.h
@@ -54,6 +54,14 @@ extern int p_rename(const char *from, const char *to);
 #define p_rmdir(p) rmdir(p)
 #define p_chmod(p,m) chmod(p, m)
 #define p_access(p,m) access(p,m)
+#define p_recv(s,b,l,f) recv(s,b,l,f)
+#define p_send(s,b,l,f) send(s,b,l,f)
+typedef int GIT_SOCKET;
+#define INVALID_SOCKET -1
+
+#else
+
+typedef SOCKET GIT_SOCKET;
 
 #endif
 
diff --git a/src/pqueue.c b/src/pqueue.c
index 3fbf933..cb59c13 100644
--- a/src/pqueue.c
+++ b/src/pqueue.c
@@ -17,14 +17,14 @@ int git_pqueue_init(git_pqueue *q, size_t n, git_pqueue_cmp cmppri)
 	assert(q);
 
 	/* Need to allocate n+1 elements since element 0 isn't used. */
-	if ((q->d = git__malloc((n + 1) * sizeof(void *))) == NULL)
-		return GIT_ENOMEM;
+	q->d = git__malloc((n + 1) * sizeof(void *));
+	GITERR_CHECK_ALLOC(q->d);
 
 	q->size = 1;
 	q->avail = q->step = (n + 1); /* see comment above about n+1 */
 	q->cmppri = cmppri;
 
-	return GIT_SUCCESS;
+	return 0;
 }
 
 
@@ -102,8 +102,8 @@ int git_pqueue_insert(git_pqueue *q, void *d)
 	/* allocate more memory if necessary */
 	if (q->size >= q->avail) {
 		newsize = q->size + q->step;
-		if ((tmp = git__realloc(q->d, sizeof(void *) * newsize)) == NULL)
-			return GIT_ENOMEM;
+		tmp = git__realloc(q->d, sizeof(void *) * newsize);
+		GITERR_CHECK_ALLOC(tmp);
 
 		q->d = tmp;
 		q->avail = newsize;
@@ -114,7 +114,7 @@ int git_pqueue_insert(git_pqueue *q, void *d)
 	q->d[i] = d;
 	bubble_up(q, i);
 
-	return GIT_SUCCESS;
+	return 0;
 }
 
 
diff --git a/src/refs.c b/src/refs.c
index 90cb920..6fffe3e 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -133,13 +133,13 @@ static int reference_read(
 
 static int loose_parse_symbolic(git_reference *ref, git_buf *file_content)
 {
-	const unsigned int header_len = strlen(GIT_SYMREF);
+	const unsigned int header_len = (unsigned int)strlen(GIT_SYMREF);
 	const char *refname_start;
 	char *eol;
 
 	refname_start = (const char *)file_content->ptr;
 
-	if (file_content->size < (header_len + 1))
+	if (file_content->size < header_len + 1)
 		goto corrupt;
 
 	/*
@@ -730,11 +730,11 @@ static int packed_write(git_repository *repo)
 	unsigned int i;
 	git_buf pack_file_path = GIT_BUF_INIT;
 	git_vector packing_list;
-	size_t total_refs;
+	unsigned int total_refs;
 
 	assert(repo && repo->references.packfile);
 
-	total_refs = repo->references.packfile->key_count;
+	total_refs = (unsigned int)repo->references.packfile->key_count;
 
 	if (git_vector_init(&packing_list, total_refs, packed_sort) < 0)
 		return -1;
@@ -821,9 +821,9 @@ static int _reference_available_cb(const char *ref, void *data)
 	d = (struct reference_available_t *)data;
 
 	if (!d->old_ref || strcmp(d->old_ref, ref)) {
-		int reflen = strlen(ref);
-		int newlen = strlen(d->new_ref);
-		int cmplen = reflen < newlen ? reflen : newlen;
+		size_t reflen = strlen(ref);
+		size_t newlen = strlen(d->new_ref);
+		size_t cmplen = reflen < newlen ? reflen : newlen;
 		const char *lead = reflen < newlen ? d->new_ref : ref;
 
 		if (!strncmp(d->new_ref, ref, cmplen) && lead[cmplen] == '/') {
diff --git a/src/repository.c b/src/repository.c
index 572b516..d191c78 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -183,21 +183,20 @@ static int find_ceiling_dir_offset(
 	char buf[GIT_PATH_MAX + 1];
 	char buf2[GIT_PATH_MAX + 1];
 	const char *ceil, *sep;
-	int len, max_len = -1;
-	int min_len;
+	size_t len, max_len = 0, min_len;
 
 	assert(path);
 
-	min_len = git_path_root(path) + 1;
+	min_len = (size_t)(git_path_root(path) + 1);
 
 	if (ceiling_directories == NULL || min_len == 0)
-		return min_len;
+		return (int)min_len;
 
 	for (sep = ceil = ceiling_directories; *sep; ceil = sep + 1) {
 		for (sep = ceil; *sep && *sep != GIT_PATH_LIST_SEPARATOR; sep++);
 		len = sep - ceil;
 
-		if (len == 0 || len >= (int)sizeof(buf) || git_path_root(ceil) == -1)
+		if (len == 0 || len >= sizeof(buf) || git_path_root(ceil) == -1)
 			continue;
 
 		strncpy(buf, ceil, len);
@@ -218,7 +217,7 @@ static int find_ceiling_dir_offset(
 		}
 	}
 
-	return max_len <= min_len ? min_len : max_len;
+	return (int)(max_len <= min_len ? min_len : max_len);
 }
 
 /*
diff --git a/src/revwalk.c b/src/revwalk.c
index 2d815b9..a88fc84 100644
--- a/src/revwalk.c
+++ b/src/revwalk.c
@@ -77,11 +77,10 @@ static int commit_time_cmp(void *a, void *b)
 static commit_list *commit_list_insert(commit_object *item, commit_list **list_p)
 {
 	commit_list *new_list = git__malloc(sizeof(commit_list));
-	if (new_list == NULL)
-		return NULL;
-
-	new_list->item = item;
-	new_list->next = *list_p;
+	if (new_list != NULL) {
+		new_list->item = item;
+		new_list->next = *list_p;
+	}
 	*list_p = new_list;
 	return new_list;
 }
@@ -143,8 +142,7 @@ static int alloc_chunk(git_revwalk *walk)
 	void *chunk;
 
 	chunk = git__calloc(COMMITS_PER_CHUNK, CHUNK_STEP);
-	if (chunk == NULL)
-		return GIT_ENOMEM;
+	GITERR_CHECK_ALLOC(chunk);
 
 	walk->chunk_size = 0;
 	return git_vector_insert(&walk->memory_alloc, chunk);
@@ -155,7 +153,8 @@ static commit_object *alloc_commit(git_revwalk *walk)
 	unsigned char *chunk;
 
 	if (walk->chunk_size == COMMITS_PER_CHUNK)
-		alloc_chunk(walk);
+		if (alloc_chunk(walk) < 0)
+			return NULL;
 
 	chunk = git_vector_get(&walk->memory_alloc, walk->memory_alloc.length - 1);
 	chunk += (walk->chunk_size * CHUNK_STEP);
@@ -186,7 +185,7 @@ static commit_object *commit_lookup(git_revwalk *walk, const git_oid *oid)
 
 	git_oid_cpy(&commit->oid, oid);
 
-	if (git_hashtable_insert(walk->commits, &commit->oid, commit) < GIT_SUCCESS) {
+	if (git_hashtable_insert(walk->commits, &commit->oid, commit) < 0) {
 		git__free(commit);
 		return NULL;
 	}
@@ -196,7 +195,7 @@ static commit_object *commit_lookup(git_revwalk *walk, const git_oid *oid)
 
 static int commit_quick_parse(git_revwalk *walk, commit_object *commit, git_rawobj *raw)
 {
-	const int parent_len = strlen("parent ") + GIT_OID_HEXSZ + 1;
+	const size_t parent_len = strlen("parent ") + GIT_OID_HEXSZ + 1;
 
 	unsigned char *buffer = raw->data;
 	unsigned char *buffer_end = buffer + raw->len;
@@ -262,7 +261,7 @@ static int commit_parse(git_revwalk *walk, commit_object *commit)
 		return 0;
 
 	if ((error = git_odb_read(&obj, walk->odb, &commit->oid)) < 0)
-		return -1;
+		return error;
 
 	if (obj->raw.type != GIT_OBJ_COMMIT) {
 		git_odb_object_free(obj);
@@ -432,6 +431,8 @@ static void mark_uninteresting(commit_object *commit)
 
 static int process_commit(git_revwalk *walk, commit_object *commit, int hide)
 {
+	int error;
+
 	if (hide)
 		mark_uninteresting(commit);
 
@@ -440,8 +441,8 @@ static int process_commit(git_revwalk *walk, commit_object *commit, int hide)
 
 	commit->seen = 1;
 
-	if (commit_parse(walk, commit) < 0)
-		return -1;
+	if ((error = commit_parse(walk, commit)) < 0)
+		return error;
 
 	return walk->enqueue(walk, commit);
 }
@@ -449,13 +450,12 @@ static int process_commit(git_revwalk *walk, commit_object *commit, int hide)
 static int process_commit_parents(git_revwalk *walk, commit_object *commit)
 {
 	unsigned short i;
+	int error = 0;
 
-	for (i = 0; i < commit->out_degree; ++i) {
-		if (process_commit(walk, commit->parents[i], commit->uninteresting) < 0)
-			return -1;
-	}
+	for (i = 0; i < commit->out_degree && !error; ++i)
+		error = process_commit(walk, commit->parents[i], commit->uninteresting);
 
-	return 0;
+	return error;
 }
 
 static int push_commit(git_revwalk *walk, const git_oid *oid, int uninteresting)
@@ -464,7 +464,7 @@ static int push_commit(git_revwalk *walk, const git_oid *oid, int uninteresting)
 
 	commit = commit_lookup(walk, oid);
 	if (commit == NULL)
-		return -1;
+		return -1; /* error already reported by failed lookup */
 
 	commit->uninteresting = uninteresting;
 	if (walk->one == NULL && !uninteresting) {
@@ -549,7 +549,8 @@ static int push_glob(git_revwalk *walk, const char *glob, int hide)
 	data.glob = git_buf_cstr(&buf);
 	data.hide = hide;
 
-	if (git_reference_foreach(walk->repo, GIT_REF_LISTALL, push_glob_cb, &data) < 0)
+	if (git_reference_foreach(
+			walk->repo, GIT_REF_LISTALL, push_glob_cb, &data) < 0)
 		goto on_error;
 
 	regfree(&preg);
@@ -605,7 +606,7 @@ static int revwalk_enqueue_timesort(git_revwalk *walk, commit_object *commit)
 
 static int revwalk_enqueue_unsorted(git_revwalk *walk, commit_object *commit)
 {
-	return commit_list_insert(commit, &walk->iterator_rand) ? GIT_SUCCESS : GIT_ENOMEM;
+	return commit_list_insert(commit, &walk->iterator_rand) ? 0 : -1;
 }
 
 static int revwalk_next_timesort(commit_object **object_out, git_revwalk *walk)
@@ -615,7 +616,7 @@ static int revwalk_next_timesort(commit_object **object_out, git_revwalk *walk)
 
 	while ((next = git_pqueue_pop(&walk->iterator_time)) != NULL) {
 		if ((error = process_commit_parents(walk, next)) < 0)
-			return -1;
+			return error;
 
 		if (!next->uninteresting) {
 			*object_out = next;
@@ -633,7 +634,7 @@ static int revwalk_next_unsorted(commit_object **object_out, git_revwalk *walk)
 
 	while ((next = commit_list_pop(&walk->iterator_rand)) != NULL) {
 		if ((error = process_commit_parents(walk, next)) < 0)
-			return -1;
+			return error;
 
 		if (!next->uninteresting) {
 			*object_out = next;
@@ -715,19 +716,19 @@ static int prepare_walk(git_revwalk *walk)
 		}
 
 		if (error != GIT_EREVWALKOVER)
-			return -1;
+			return error;
 
 		walk->get_next = &revwalk_next_toposort;
 	}
 
 	if (walk->sorting & GIT_SORT_REVERSE) {
 
-		while ((error = walk->get_next(&next, walk)) == GIT_SUCCESS)
+		while ((error = walk->get_next(&next, walk)) == 0)
 			if (commit_list_insert(next, &walk->iterator_reverse) == NULL)
 				return -1;
 
 		if (error != GIT_EREVWALKOVER)
-			return -1;
+			return error;
 
 		walk->get_next = &revwalk_next_reverse;
 	}
@@ -752,16 +753,13 @@ int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo)
 	walk->commits = git_hashtable_alloc(64,
 			object_table_hash,
 			(git_hash_keyeq_ptr)git_oid_cmp);
+	GITERR_CHECK_ALLOC(walk->commits);
 
-	if (walk->commits == NULL) {
-		git__free(walk);
+	if (git_pqueue_init(&walk->iterator_time, 8, commit_time_cmp) < 0 ||
+		git_vector_init(&walk->memory_alloc, 8, NULL) < 0 ||
+		git_vector_init(&walk->twos, 4, NULL) < 0 ||
+		alloc_chunk(walk) < 0)
 		return -1;
-	}
-
-	git_pqueue_init(&walk->iterator_time, 8, commit_time_cmp);
-	git_vector_init(&walk->memory_alloc, 8, NULL);
-	git_vector_init(&walk->twos, 4, NULL);
-	alloc_chunk(walk);
 
 	walk->get_next = &revwalk_next_unsorted;
 	walk->enqueue = &revwalk_enqueue_unsorted;
@@ -840,7 +838,7 @@ int git_revwalk_next(git_oid *oid, git_revwalk *walk)
 
 	if (!walk->walking) {
 		if ((error = prepare_walk(walk)) < 0)
-			return -1;
+			return error;
 	}
 
 	error = walk->get_next(&next, walk);
@@ -850,11 +848,10 @@ int git_revwalk_next(git_oid *oid, git_revwalk *walk)
 		return GIT_EREVWALKOVER;
 	}
 
-	if (error < 0)
-		return -1;
+	if (!error)
+		git_oid_cpy(oid, &next->oid);
 
-	git_oid_cpy(oid, &next->oid);
-	return 0;
+	return error;
 }
 
 void git_revwalk_reset(git_revwalk *walk)
diff --git a/src/sha1.c b/src/sha1.c
index af3c2d2..8aaedeb 100644
--- a/src/sha1.c
+++ b/src/sha1.c
@@ -232,7 +232,7 @@ void git__blk_SHA1_Init(blk_SHA_CTX *ctx)
 	ctx->H[4] = 0xc3d2e1f0;
 }
 
-void git__blk_SHA1_Update(blk_SHA_CTX *ctx, const void *data, unsigned long len)
+void git__blk_SHA1_Update(blk_SHA_CTX *ctx, const void *data, size_t len)
 {
 	unsigned int lenW = ctx->size & 63;
 
@@ -242,7 +242,7 @@ void git__blk_SHA1_Update(blk_SHA_CTX *ctx, const void *data, unsigned long len)
 	if (lenW) {
 		unsigned int left = 64 - lenW;
 		if (len < left)
-			left = len;
+			left = (unsigned int)len;
 		memcpy(lenW + (char *)ctx->W, data, left);
 		lenW = (lenW + left) & 63;
 		len -= left;
diff --git a/src/sha1.h b/src/sha1.h
index 117e931..93a244d 100644
--- a/src/sha1.h
+++ b/src/sha1.h
@@ -12,7 +12,7 @@ typedef struct {
 } blk_SHA_CTX;
 
 void git__blk_SHA1_Init(blk_SHA_CTX *ctx);
-void git__blk_SHA1_Update(blk_SHA_CTX *ctx, const void *dataIn, unsigned long len);
+void git__blk_SHA1_Update(blk_SHA_CTX *ctx, const void *dataIn, size_t len);
 void git__blk_SHA1_Final(unsigned char hashout[20], blk_SHA_CTX *ctx);
 
 #define SHA_CTX		blk_SHA_CTX
diff --git a/src/signature.c b/src/signature.c
index 87386bc..4d6d11c 100644
--- a/src/signature.c
+++ b/src/signature.c
@@ -47,7 +47,7 @@ static int signature_error(const char *msg)
 static int process_trimming(const char *input, char **storage, const char *input_end, int fail_when_empty)
 {
 	const char *left, *right;
-	int trimmed_input_length;
+	size_t trimmed_input_length;
 
 	assert(storage);
 
diff --git a/src/tag.c b/src/tag.c
index cfd2c70..5524879 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -67,7 +67,8 @@ int git_tag__parse_buffer(git_tag *tag, const char *buffer, size_t length)
 		NULL, "commit\n", "tree\n", "blob\n", "tag\n"
 	};
 
-	unsigned int i, text_len;
+	unsigned int i;
+	size_t text_len;
 	char *search;
 	int error;
 
diff --git a/src/transports/git.c b/src/transports/git.c
index fd3ff5b..825f072 100644
--- a/src/transports/git.c
+++ b/src/transports/git.c
@@ -46,7 +46,7 @@ static int gen_proto(git_buf *request, const char *cmd, const char *url)
 	char *delim, *repo;
 	char default_command[] = "git-upload-pack";
 	char host[] = "host=";
-	int len;
+	size_t len;
 
 	delim = strchr(url, '/');
 	if (delim == NULL) {
@@ -66,7 +66,8 @@ static int gen_proto(git_buf *request, const char *cmd, const char *url)
 	len = 4 + strlen(cmd) + 1 + strlen(repo) + 1 + strlen(host) + (delim - url) + 1;
 
 	git_buf_grow(request, len);
-	git_buf_printf(request, "%04x%s %s%c%s", len, cmd, repo, 0, host);
+	git_buf_printf(request, "%04x%s %s%c%s",
+		(unsigned int)(len & 0x0FFFF), cmd, repo, 0, host);
 	git_buf_put(request, url, delim - url);
 	git_buf_putc(request, '\0');
 
@@ -119,7 +120,7 @@ static int do_connect(transport_git *t, const char *url)
 	git__free(port);
 
 	if (error < GIT_SUCCESS && s > 0)
-		close(s);
+		gitno_close(s);
 	if (!connected) {
 		giterr_set(GITERR_NET, "Failed to connect to the host");
 		return -1;
diff --git a/src/transports/http.c b/src/transports/http.c
index d8af99d..0938fef 100644
--- a/src/transports/http.c
+++ b/src/transports/http.c
@@ -32,7 +32,7 @@ typedef struct {
 	git_protocol proto;
 	git_vector refs;
 	git_vector common;
-	int socket;
+	GIT_SOCKET socket;
 	git_buf buf;
 	git_remote_head **heads;
 	int error;
@@ -96,7 +96,7 @@ static int do_connect(transport_http *t, const char *host, const char *port)
 	t->socket = s;
 	t->parent.connected = 1;
 
-	return GIT_SUCCESS;
+	return 0;
 }
 
 /*
diff --git a/src/tree.c b/src/tree.c
index 5957f7a..56a7229 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -624,7 +624,7 @@ static int tree_frompath(
 	git_tree **parent_out,
 	git_tree *root,
 	git_buf *treeentry_path,
-	int offset)
+	size_t offset)
 {
 	char *slash_pos = NULL;
 	const git_tree_entry* entry;
diff --git a/src/tsort.c b/src/tsort.c
index 6fbec5b..f54c21e 100644
--- a/src/tsort.c
+++ b/src/tsort.c
@@ -30,8 +30,10 @@ static int binsearch(void **dst, const void *x, size_t size, cmp_ptr_t cmp)
 	int l, c, r;
 	void *lx, *cx;
 
+	assert(size > 0);
+
 	l = 0;
-	r = size - 1;
+	r = (int)size - 1;
 	c = r >> 1;
 	lx = dst[l];
 
@@ -84,7 +86,7 @@ static void bisort(void **dst, size_t start, size_t size, cmp_ptr_t cmp)
 		/* Else we need to find the right place, shift everything over, and squeeze in */
 		x = dst[i];
 		location = binsearch(dst, x, i, cmp);
-		for (j = i - 1; j >= location; j--) {
+		for (j = (int)i - 1; j >= location; j--) {
 			dst[j + 1] = dst[j];
 		}
 		dst[location] = x;
@@ -104,7 +106,7 @@ struct tsort_store {
 	void **storage;
 };
 
-static void reverse_elements(void **dst, int start, int end)
+static void reverse_elements(void **dst, ssize_t start, ssize_t end)
 {
 	while (start < end) {
 		void *tmp = dst[start];
@@ -116,7 +118,7 @@ static void reverse_elements(void **dst, int start, int end)
 	}
 }
 
-static int count_run(void **dst, ssize_t start, ssize_t size, struct tsort_store *store)
+static ssize_t count_run(void **dst, ssize_t start, ssize_t size, struct tsort_store *store)
 {
 	ssize_t curr = start + 2;
 
@@ -148,7 +150,7 @@ static int count_run(void **dst, ssize_t start, ssize_t size, struct tsort_store
 	}
 }
 
-static int compute_minrun(size_t n)
+static size_t compute_minrun(size_t n)
 {
 	int r = 0;
 	while (n >= 64) {
@@ -158,19 +160,19 @@ static int compute_minrun(size_t n)
 	return n + r;
 }
 
-static int check_invariant(struct tsort_run *stack, int stack_curr)
+static int check_invariant(struct tsort_run *stack, ssize_t stack_curr)
 {
 	if (stack_curr < 2)
 		return 1;
 
 	else if (stack_curr == 2) {
-		const int A = stack[stack_curr - 2].length;
-		const int B = stack[stack_curr - 1].length;
+		const ssize_t A = stack[stack_curr - 2].length;
+		const ssize_t B = stack[stack_curr - 1].length;
 		return (A > B);
 	} else {
-		const int A = stack[stack_curr - 3].length;
-		const int B = stack[stack_curr - 2].length;
-		const int C = stack[stack_curr - 1].length;
+		const ssize_t A = stack[stack_curr - 3].length;
+		const ssize_t B = stack[stack_curr - 2].length;
+		const ssize_t C = stack[stack_curr - 1].length;
 		return !((A <= B + C) || (B <= C));
 	}
 }
@@ -195,7 +197,7 @@ static int resize(struct tsort_store *store, size_t new_size)
 	return 0;
 }
 
-static void merge(void **dst, const struct tsort_run *stack, int stack_curr, struct tsort_store *store)
+static void merge(void **dst, const struct tsort_run *stack, ssize_t stack_curr, struct tsort_store *store)
 {
 	const ssize_t A = stack[stack_curr - 2].length;
 	const ssize_t B = stack[stack_curr - 1].length;
@@ -343,7 +345,7 @@ void git__tsort(void **dst, size_t size, cmp_ptr_t cmp)
 	}
 
 	/* compute the minimum run length */
-	minrun = compute_minrun(size);
+	minrun = (ssize_t)compute_minrun(size);
 
 	/* temporary storage for merges */
 	store->alloc = 0;
diff --git a/src/util.c b/src/util.c
index 81ad106..2cf7b15 100644
--- a/src/util.c
+++ b/src/util.c
@@ -391,10 +391,11 @@ int git__bsearch(
 	int (*compare)(const void *, const void *),
 	size_t *position)
 {
-	int lim, cmp = -1;
+	unsigned int lim;
+	int cmp = -1;
 	void **part, **base = array;
 
-	for (lim = array_len; lim != 0; lim >>= 1) {
+	for (lim = (unsigned int)array_len; lim != 0; lim >>= 1) {
 		part = base + (lim >> 1);
 		cmp = (*compare)(key, *part);
 		if (cmp == 0) {
diff --git a/src/vector.c b/src/vector.c
index d73c2b4..304f324 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -10,7 +10,7 @@
 #include "vector.h"
 
 static const double resize_factor = 1.75;
-static const size_t minimum_size = 8;
+static const unsigned int minimum_size = 8;
 
 static int resize_vector(git_vector *v)
 {
diff --git a/src/win32/posix.h b/src/win32/posix.h
index 60adc96..d13d3e3 100644
--- a/src/win32/posix.h
+++ b/src/win32/posix.h
@@ -49,5 +49,7 @@ extern int p_open(const char *path, int flags);
 extern int p_creat(const char *path, mode_t mode);
 extern int p_getcwd(char *buffer_out, size_t size);
 extern int p_rename(const char *from, const char *to);
+extern int p_recv(GIT_SOCKET socket, void *buffer, size_t length, int flags);
+extern int p_send(GIT_SOCKET socket, const void *buffer, size_t length, int flags);
 
 #endif
diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c
index c6b36a8..8af6641 100644
--- a/src/win32/posix_w32.c
+++ b/src/win32/posix_w32.c
@@ -4,7 +4,7 @@
  * This file is part of libgit2, distributed under the GNU GPL v2 with
  * a Linking Exception. For full terms see the included COPYING file.
  */
-#include "posix.h"
+#include "../posix.h"
 #include "path.h"
 #include "utf-conv.h"
 #include <errno.h>
@@ -179,11 +179,11 @@ int p_readlink(const char *link, char *target, size_t target_len)
 	target_w = (wchar_t*)git__malloc(target_len * sizeof(wchar_t));
 	GITERR_CHECK_ALLOC(target_w);
 
-	dwRet = pGetFinalPath(hFile, target_w, target_len, 0x0);
+	dwRet = pGetFinalPath(hFile, target_w, (DWORD)target_len, 0x0);
 	if (dwRet == 0 ||
 		dwRet >= target_len ||
 		!WideCharToMultiByte(CP_UTF8, 0, target_w, -1, target,
-			target_len * sizeof(char), NULL, NULL))
+			(int)(target_len * sizeof(char)), NULL, NULL))
 		error = -1;
 
 	git__free(target_w);
@@ -241,13 +241,19 @@ int p_creat(const char *path, mode_t mode)
 
 int p_getcwd(char *buffer_out, size_t size)
 {
-	wchar_t* buf = (wchar_t*)git__malloc(sizeof(wchar_t) * (int)size);
 	int ret;
+	wchar_t* buf;
+
+	if ((size_t)((int)size) != size)
+		return -1;
+
+	buf = (wchar_t*)git__malloc(sizeof(wchar_t) * (int)size);
+	GITERR_CHECK_ALLOC(buf);
 
 	_wgetcwd(buf, (int)size);
 
 	ret = WideCharToMultiByte(
-		CP_UTF8, 0, buf, -1, buffer_out, size, NULL, NULL);
+		CP_UTF8, 0, buf, -1, buffer_out, (int)size, NULL, NULL);
 
 	git__free(buf);
 	return !ret ? -1 : 0;
@@ -421,3 +427,19 @@ int p_rename(const char *from, const char *to)
 
 	return ret;
 }
+
+int p_recv(GIT_SOCKET socket, void *buffer, size_t length, int flags)
+{
+	if ((size_t)((int)length) != length)
+		return -1; /* giterr_set will be done by caller */
+
+	return recv(socket, buffer, (int)length, flags);
+}
+
+int p_send(GIT_SOCKET socket, const void *buffer, size_t length, int flags)
+{
+	if ((size_t)((int)length) != length)
+		return -1; /* giterr_set will be done by caller */
+
+	return send(socket, buffer, (int)length, flags);
+}
diff --git a/src/win32/utf-conv.c b/src/win32/utf-conv.c
index f00f5be..fbcb69d 100644
--- a/src/win32/utf-conv.c
+++ b/src/win32/utf-conv.c
@@ -31,27 +31,23 @@ void gitwin_set_utf8(void)
 wchar_t* gitwin_to_utf16(const char* str)
 {
 	wchar_t* ret;
-	int cb;
+	size_t cb;
 
 	if (!str)
 		return NULL;
 
 	cb = strlen(str) * sizeof(wchar_t);
-	if (cb == 0) {
-		ret = (wchar_t*)git__malloc(sizeof(wchar_t));
-		if (ret)
-			ret[0] = 0;
-		return ret;
-	}
+	if (cb == 0)
+		return (wchar_t *)git__calloc(1, sizeof(wchar_t));
 
 	/* Add space for null terminator */
 	cb += sizeof(wchar_t);
 
-	ret = (wchar_t*)git__malloc(cb);
+	ret = (wchar_t *)git__malloc(cb);
 	if (!ret)
 		return NULL;
 
-	if (MultiByteToWideChar(_active_codepage, 0, str, -1, ret, cb) == 0) {
+	if (MultiByteToWideChar(_active_codepage, 0, str, -1, ret, (int)cb) == 0) {
 		giterr_set(GITERR_OS, "Could not convert string to UTF-16");
 		git__free(ret);
 		ret = NULL;
@@ -62,7 +58,7 @@ wchar_t* gitwin_to_utf16(const char* str)
 
 int gitwin_append_utf16(wchar_t *buffer, const char *str, size_t len)
 {
-	int result = MultiByteToWideChar(_active_codepage, 0, str, -1, buffer, len);
+	int result = MultiByteToWideChar(_active_codepage, 0, str, -1, buffer, (int)len);
 	if (result == 0)
 		giterr_set(GITERR_OS, "Could not convert string to UTF-16");
 	return result;
@@ -71,19 +67,14 @@ int gitwin_append_utf16(wchar_t *buffer, const char *str, size_t len)
 char* gitwin_from_utf16(const wchar_t* str)
 {
 	char* ret;
-	int cb;
+	size_t cb;
 
-	if (!str) {
+	if (!str)
 		return NULL;
-	}
 
 	cb = wcslen(str) * sizeof(char);
-	if (cb == 0) {
-		ret = (char*)git__malloc(sizeof(char));
-		if (ret)
-			ret[0] = 0;
-		return ret;
-	}
+	if (cb == 0)
+		return (char *)git__calloc(1, sizeof(char));
 
 	/* Add space for null terminator */
 	cb += sizeof(char);
@@ -92,7 +83,7 @@ char* gitwin_from_utf16(const wchar_t* str)
 	if (!ret)
 		return NULL;
 
-	if (WideCharToMultiByte(_active_codepage, 0, str, -1, ret, cb, NULL, NULL) == 0) {
+	if (WideCharToMultiByte(_active_codepage, 0, str, -1, ret, (int)cb, NULL, NULL) == 0) {
 		giterr_set(GITERR_OS, "Could not convert string to UTF-8");
 		git__free(ret);
 		ret = NULL;
diff --git a/src/xdiff/xemit.c b/src/xdiff/xemit.c
index 8b7d417..e3e63d9 100644
--- a/src/xdiff/xemit.c
+++ b/src/xdiff/xemit.c
@@ -40,7 +40,7 @@ static long xdl_get_rec(xdfile_t *xdf, long ri, char const **rec) {
 
 
 static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t *ecb) {
-	long size, psize = strlen(pre);
+	long size, psize = (long)strlen(pre);
 	char const *rec;
 
 	size = xdl_get_rec(xdf, ri, &rec);
diff --git a/src/xdiff/xmerge.c b/src/xdiff/xmerge.c
index 9e13b25..84e4246 100644
--- a/src/xdiff/xmerge.c
+++ b/src/xdiff/xmerge.c
@@ -149,9 +149,9 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
 			      int size, int i, int style,
 			      xdmerge_t *m, char *dest, int marker_size)
 {
-	int marker1_size = (name1 ? strlen(name1) + 1 : 0);
-	int marker2_size = (name2 ? strlen(name2) + 1 : 0);
-	int marker3_size = (name3 ? strlen(name3) + 1 : 0);
+	int marker1_size = (name1 ? (int)strlen(name1) + 1 : 0);
+	int marker2_size = (name2 ? (int)strlen(name2) + 1 : 0);
+	int marker3_size = (name3 ? (int)strlen(name3) + 1 : 0);
 
 	if (marker_size <= 0)
 		marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
diff --git a/src/xdiff/xutils.c b/src/xdiff/xutils.c
index 9dea04b..bb7bdee 100644
--- a/src/xdiff/xutils.c
+++ b/src/xdiff/xutils.c
@@ -62,14 +62,14 @@ int xdl_emit_diffrec(char const *rec, long size, char const *pre, long psize,
 
 void *xdl_mmfile_first(mmfile_t *mmf, long *size)
 {
-	*size = mmf->size;
+	*size = (long)mmf->size;
 	return mmf->ptr;
 }
 
 
 long xdl_mmfile_size(mmfile_t *mmf)
 {
-	return mmf->size;
+	return (long)mmf->size;
 }
 
 
@@ -321,7 +321,7 @@ int xdl_num_out(char *out, long val) {
 		*str++ = '0';
 	*str = '\0';
 
-	return str - out;
+	return (int)(str - out);
 }
 
 
diff --git a/tests-clar/diff/diff_helpers.c b/tests-clar/diff/diff_helpers.c
index 055bd4b..c9a633c 100644
--- a/tests-clar/diff/diff_helpers.c
+++ b/tests-clar/diff/diff_helpers.c
@@ -5,7 +5,7 @@ git_tree *resolve_commit_oid_to_tree(
 	git_repository *repo,
 	const char *partial_oid)
 {
-	size_t len = strlen(partial_oid);
+	unsigned int len = (unsigned int)strlen(partial_oid);
 	git_oid oid;
 	git_object *obj = NULL;
 	git_tree *tree = NULL;
diff --git a/tests-clar/repo/open.c b/tests-clar/repo/open.c
index 2450de0..1813887 100644
--- a/tests-clar/repo/open.c
+++ b/tests-clar/repo/open.c
@@ -1,5 +1,6 @@
 #include "clar_libgit2.h"
 #include "fileops.h"
+#include <ctype.h>
 
 void test_repo_open__cleanup(void)
 {
@@ -234,7 +235,6 @@ void test_repo_open__win32_path(void)
 #ifdef GIT_WIN32
 	git_repository *repo = cl_git_sandbox_init("empty_standard_repo"), *repo2;
 	git_buf winpath = GIT_BUF_INIT;
-	char *src, *tgt;
 	static const char *repo_path = "empty_standard_repo/.git/";
 	static const char *repo_wd   = "empty_standard_repo/";
 
diff --git a/tests-clar/status/status_data.h b/tests-clar/status/status_data.h
index e60b67c..7f078bf 100644
--- a/tests-clar/status/status_data.h
+++ b/tests-clar/status/status_data.h
@@ -1,11 +1,11 @@
 
 struct status_entry_counts {
-	int wrong_status_flags_count;
-	int wrong_sorted_path;
-	int entry_count;
+	size_t wrong_status_flags_count;
+	size_t wrong_sorted_path;
+	size_t entry_count;
 	const unsigned int* expected_statuses;
 	const char** expected_paths;
-	int expected_entry_count;
+	size_t expected_entry_count;
 };
 
 /* entries for a plain copy of tests/resources/status */
diff --git a/tests/t07-hashtable.c b/tests/t07-hashtable.c
index 6beaeac..4d45c7f 100644
--- a/tests/t07-hashtable.c
+++ b/tests/t07-hashtable.c
@@ -112,7 +112,7 @@ BEGIN_TEST(table2, "make sure the table resizes automatically")
 
 	const int objects_n = 64;
 	int i;
-	unsigned int old_size;
+	size_t old_size;
 	table_item *objects;
 	git_hashtable *table = NULL;
 
diff --git a/tests/test_helpers.c b/tests/test_helpers.c
index fc03519..a1689e3 100644
--- a/tests/test_helpers.c
+++ b/tests/test_helpers.c
@@ -87,7 +87,7 @@ void locate_loose_object(const char *repository_folder, git_object *object, char
 	static const char *objects_folder = "objects/";
 
 	char *ptr, *full_path, *top_folder;
-	int path_length, objects_length;
+	size_t path_length, objects_length;
 
 	assert(repository_folder && object);