Merge pull request #4834 from pks-t/pks/v0.27.5 Security release v0.27.5
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
diff --git a/CHANGELOG.md b/CHANGELOG.md
index be62aa6..7a1ffc6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,50 @@
+v0.27.5
+-------
+
+This is a security release fixing the following list of issues:
+
+- Submodule URLs and paths with a leading "-" are now ignored.
+ This is due to the recently discovered CVE-2018-17456, which
+ can lead to arbitrary code execution in upstream git. While
+ libgit2 itself is not vulnerable, it can be used to inject
+ options in an implementation which performs a recursive clone
+ by executing an external command.
+
+- When running repack while doing repo writes,
+ `packfile_load__cb()` could see some temporary files in the
+ directory that were bigger than the usual, and makes `memcmp`
+ overflow on the `p->pack_name` string. This issue was reported
+ and fixed by bisho.
+
+- The configuration file parser used unbounded recursion to parse
+ multiline variables, which could lead to a stack overflow. The
+ issue was reported by the oss-fuzz project, issue 10048 and
+ fixed by Nelson Elhage.
+
+- The fix to the unbounded recursion introduced a memory leak in
+ the config parser. While this leak was never in a public
+ release, the oss-fuzz project reported this as issue 10127. The
+ fix was implemented by Nelson Elhage and Patrick Steinhardt.
+
+- When parsing "ok" packets received via the smart protocol, our
+ parsing code did not correctly verify the bounds of the
+ packets, which could result in a heap-buffer overflow. The
+ issue was reported by the oss-fuzz project, issue 9749 and
+ fixed by Patrick Steinhardt.
+
+- The parsing code for the smart protocol has been tightened in
+ general, fixing heap-buffer overflows when parsing the packet
+ type as well as for "ACK" and "unpack" packets. The issue was
+ discovered and fixed by Patrick Steinhardt.
+
+- Fixed potential integer overflows on platforms with 16 bit
+ integers when parsing packets for the smart protocol. The issue
+ was discovered and fixed by Patrick Steinhardt.
+
+- Fixed potential NULL pointer dereference when parsing
+ configuration files which have "include.path" or
+ "includeIf..path" statements without a value.
+
v0.27.4
-------
diff --git a/include/git2/version.h b/include/git2/version.h
index 7c42426..1c20724 100644
--- a/include/git2/version.h
+++ b/include/git2/version.h
@@ -7,10 +7,10 @@
#ifndef INCLUDE_git_version_h__
#define INCLUDE_git_version_h__
-#define LIBGIT2_VERSION "0.27.4"
+#define LIBGIT2_VERSION "0.27.5"
#define LIBGIT2_VER_MAJOR 0
#define LIBGIT2_VER_MINOR 27
-#define LIBGIT2_VER_REVISION 4
+#define LIBGIT2_VER_REVISION 5
#define LIBGIT2_VER_PATCH 0
#define LIBGIT2_SOVERSION 27
diff --git a/src/config_file.c b/src/config_file.c
index aa9e83b..0f4eb0e 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -928,6 +928,9 @@ static int parse_include(git_config_parser *reader,
char *dir;
int result;
+ if (!file)
+ return 0;
+
if ((result = git_path_dirname_r(&path, reader->file->path)) < 0)
return result;
@@ -1029,7 +1032,7 @@ static int parse_conditional_include(git_config_parser *reader,
size_t i;
int error = 0, matches;
- if (!parse_data->repo)
+ if (!parse_data->repo || !file)
return 0;
condition = git__substrdup(section + strlen("includeIf."),
diff --git a/src/config_parse.c b/src/config_parse.c
index 149550d..d4c1c2b 100644
--- a/src/config_parse.c
+++ b/src/config_parse.c
@@ -315,49 +315,51 @@ done:
static int parse_multiline_variable(git_config_parser *reader, git_buf *value, int in_quotes)
{
- char *line = NULL, *proc_line = NULL;
int quote_count;
- bool multiline;
+ bool multiline = true;
- /* Check that the next line exists */
- git_parse_advance_line(&reader->ctx);
- line = git__strndup(reader->ctx.line, reader->ctx.line_len);
- if (line == NULL)
- return -1;
+ while (multiline) {
+ char *line = NULL, *proc_line = NULL;
+ int error;
- /* We've reached the end of the file, there is no continuation.
- * (this is not an error).
- */
- if (line[0] == '\0') {
- git__free(line);
- return 0;
- }
+ /* Check that the next line exists */
+ git_parse_advance_line(&reader->ctx);
+ line = git__strndup(reader->ctx.line, reader->ctx.line_len);
+ GITERR_CHECK_ALLOC(line);
+
+ /*
+ * We've reached the end of the file, there is no continuation.
+ * (this is not an error).
+ */
+ if (line[0] == '\0') {
+ error = 0;
+ goto out;
+ }
- quote_count = strip_comments(line, !!in_quotes);
+ /* If it was just a comment, pretend it didn't exist */
+ quote_count = strip_comments(line, !!in_quotes);
+ if (line[0] == '\0')
+ goto next;
- /* If it was just a comment, pretend it didn't exist */
- if (line[0] == '\0') {
+ if ((error = unescape_line(&proc_line, &multiline,
+ line, in_quotes)) < 0)
+ goto out;
+
+ /* Add this line to the multiline var */
+ if ((error = git_buf_puts(value, proc_line)) < 0)
+ goto out;
+
+next:
git__free(line);
- return parse_multiline_variable(reader, value, quote_count);
- /* TODO: unbounded recursion. This **could** be exploitable */
- }
+ git__free(proc_line);
+ in_quotes = quote_count;
+ continue;
- if (unescape_line(&proc_line, &multiline, line, in_quotes) < 0) {
+out:
git__free(line);
- return -1;
+ git__free(proc_line);
+ return error;
}
- /* add this line to the multiline var */
-
- git_buf_puts(value, proc_line);
- git__free(line);
- git__free(proc_line);
-
- /*
- * If we need to continue reading the next line, let's just
- * keep putting stuff in the buffer
- */
- if (multiline)
- return parse_multiline_variable(reader, value, quote_count);
return 0;
}
diff --git a/src/odb_pack.c b/src/odb_pack.c
index 20aff53..7c977d5 100644
--- a/src/odb_pack.c
+++ b/src/odb_pack.c
@@ -210,7 +210,7 @@ static int packfile_load__cb(void *data, git_buf *path)
for (i = 0; i < backend->packs.length; ++i) {
struct git_pack_file *p = git_vector_get(&backend->packs, i);
- if (memcmp(p->pack_name, path_str, cmp_len) == 0)
+ if (strncmp(p->pack_name, path_str, cmp_len) == 0)
return 0;
}
diff --git a/src/submodule.c b/src/submodule.c
index 4bf74dc..06b7de5 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -1813,6 +1813,14 @@ static int get_value(const char **out, git_config *cfg, git_buf *buf, const char
return error;
}
+static bool looks_like_command_line_option(const char *s)
+{
+ if (s && s[0] == '-')
+ return true;
+
+ return false;
+}
+
static int submodule_read_config(git_submodule *sm, git_config *cfg)
{
git_buf key = GIT_BUF_INIT;
@@ -1826,24 +1834,31 @@ static int submodule_read_config(git_submodule *sm, git_config *cfg)
if ((error = get_value(&value, cfg, &key, sm->name, "path")) == 0) {
in_config = 1;
+ /* We would warn here if we had that API */
+ if (!looks_like_command_line_option(value)) {
/*
* TODO: if case insensitive filesystem, then the following strcmp
* should be strcasecmp
*/
- if (strcmp(sm->name, value) != 0) {
- if (sm->path != sm->name)
- git__free(sm->path);
- sm->path = git__strdup(value);
- GITERR_CHECK_ALLOC(sm->path);
+ if (strcmp(sm->name, value) != 0) {
+ if (sm->path != sm->name)
+ git__free(sm->path);
+ sm->path = git__strdup(value);
+ GITERR_CHECK_ALLOC(sm->path);
+ }
+
}
} else if (error != GIT_ENOTFOUND) {
goto cleanup;
}
if ((error = get_value(&value, cfg, &key, sm->name, "url")) == 0) {
- in_config = 1;
- sm->url = git__strdup(value);
- GITERR_CHECK_ALLOC(sm->url);
+ /* We would warn here if we had that API */
+ if (!looks_like_command_line_option(value)) {
+ in_config = 1;
+ sm->url = git__strdup(value);
+ GITERR_CHECK_ALLOC(sm->url);
+ }
} else if (error != GIT_ENOTFOUND) {
goto cleanup;
}
diff --git a/src/transports/smart.h b/src/transports/smart.h
index e33a254..e602081 100644
--- a/src/transports/smart.h
+++ b/src/transports/smart.h
@@ -33,14 +33,14 @@
extern bool git_smart__ofs_delta_enabled;
-enum git_pkt_type {
+typedef enum {
GIT_PKT_CMD,
GIT_PKT_FLUSH,
GIT_PKT_REF,
GIT_PKT_HAVE,
GIT_PKT_ACK,
GIT_PKT_NAK,
- GIT_PKT_PACK,
+ GIT_PKT_PACK__UNUSED,
GIT_PKT_COMMENT,
GIT_PKT_ERR,
GIT_PKT_DATA,
@@ -48,7 +48,7 @@ enum git_pkt_type {
GIT_PKT_OK,
GIT_PKT_NG,
GIT_PKT_UNPACK,
-};
+} git_pkt_type;
/* Used for multi_ack and mutli_ack_detailed */
enum git_ack_status {
@@ -60,11 +60,11 @@ enum git_ack_status {
/* This would be a flush pkt */
typedef struct {
- enum git_pkt_type type;
+ git_pkt_type type;
} git_pkt;
struct git_pkt_cmd {
- enum git_pkt_type type;
+ git_pkt_type type;
char *cmd;
char *path;
char *host;
@@ -72,50 +72,50 @@ struct git_pkt_cmd {
/* This is a pkt-line with some info in it */
typedef struct {
- enum git_pkt_type type;
+ git_pkt_type type;
git_remote_head head;
char *capabilities;
} git_pkt_ref;
/* Useful later */
typedef struct {
- enum git_pkt_type type;
+ git_pkt_type type;
git_oid oid;
enum git_ack_status status;
} git_pkt_ack;
typedef struct {
- enum git_pkt_type type;
+ git_pkt_type type;
char comment[GIT_FLEX_ARRAY];
} git_pkt_comment;
typedef struct {
- enum git_pkt_type type;
- int len;
+ git_pkt_type type;
+ size_t len;
char data[GIT_FLEX_ARRAY];
} git_pkt_data;
typedef git_pkt_data git_pkt_progress;
typedef struct {
- enum git_pkt_type type;
- int len;
+ git_pkt_type type;
+ size_t len;
char error[GIT_FLEX_ARRAY];
} git_pkt_err;
typedef struct {
- enum git_pkt_type type;
+ git_pkt_type type;
char *ref;
} git_pkt_ok;
typedef struct {
- enum git_pkt_type type;
+ git_pkt_type type;
char *ref;
char *msg;
} git_pkt_ng;
typedef struct {
- enum git_pkt_type type;
+ git_pkt_type type;
int unpack_ok;
} git_pkt_unpack;
@@ -189,7 +189,7 @@ int git_smart__get_push_stream(transport_smart *t, git_smart_subtransport_stream
int git_smart__update_heads(transport_smart *t, git_vector *symrefs);
/* smart_pkt.c */
-int git_pkt_parse_line(git_pkt **head, const char *line, const char **out, size_t len);
+int git_pkt_parse_line(git_pkt **head, const char **endptr, const char *line, size_t linelen);
int git_pkt_buffer_flush(git_buf *buf);
int git_pkt_send_flush(GIT_SOCKET s);
int git_pkt_buffer_done(git_buf *buf);
diff --git a/src/transports/smart_pkt.c b/src/transports/smart_pkt.c
index d10d6c6..e726d07 100644
--- a/src/transports/smart_pkt.c
+++ b/src/transports/smart_pkt.c
@@ -43,34 +43,43 @@ static int flush_pkt(git_pkt **out)
static int ack_pkt(git_pkt **out, const char *line, size_t len)
{
git_pkt_ack *pkt;
- GIT_UNUSED(line);
- GIT_UNUSED(len);
pkt = git__calloc(1, sizeof(git_pkt_ack));
GITERR_CHECK_ALLOC(pkt);
-
pkt->type = GIT_PKT_ACK;
- line += 3;
- len -= 3;
- if (len >= GIT_OID_HEXSZ) {
- git_oid_fromstr(&pkt->oid, line + 1);
- line += GIT_OID_HEXSZ + 1;
- len -= GIT_OID_HEXSZ + 1;
- }
+ if (git__prefixncmp(line, len, "ACK "))
+ goto out_err;
+ line += 4;
+ len -= 4;
+
+ if (len < GIT_OID_HEXSZ || git_oid_fromstr(&pkt->oid, line) < 0)
+ goto out_err;
+ line += GIT_OID_HEXSZ;
+ len -= GIT_OID_HEXSZ;
- if (len >= 7) {
- if (!git__prefixcmp(line + 1, "continue"))
+ if (len && line[0] == ' ') {
+ line++;
+ len--;
+
+ if (!git__prefixncmp(line, len, "continue"))
pkt->status = GIT_ACK_CONTINUE;
- if (!git__prefixcmp(line + 1, "common"))
+ else if (!git__prefixncmp(line, len, "common"))
pkt->status = GIT_ACK_COMMON;
- if (!git__prefixcmp(line + 1, "ready"))
+ else if (!git__prefixncmp(line, len, "ready"))
pkt->status = GIT_ACK_READY;
+ else
+ goto out_err;
}
*out = (git_pkt *) pkt;
return 0;
+
+out_err:
+ giterr_set(GITERR_NET, "error parsing ACK pkt-line");
+ git__free(pkt);
+ return -1;
}
static int nak_pkt(git_pkt **out)
@@ -86,19 +95,6 @@ static int nak_pkt(git_pkt **out)
return 0;
}
-static int pack_pkt(git_pkt **out)
-{
- git_pkt *pkt;
-
- pkt = git__malloc(sizeof(git_pkt));
- GITERR_CHECK_ALLOC(pkt);
-
- pkt->type = GIT_PKT_PACK;
- *out = pkt;
-
- return 0;
-}
-
static int comment_pkt(git_pkt **out, const char *line, size_t len)
{
git_pkt_comment *pkt;
@@ -120,10 +116,12 @@ static int comment_pkt(git_pkt **out, const char *line, size_t len)
static int err_pkt(git_pkt **out, const char *line, size_t len)
{
- git_pkt_err *pkt;
+ git_pkt_err *pkt = NULL;
size_t alloclen;
/* Remove "ERR " from the line */
+ if (git__prefixncmp(line, len, "ERR "))
+ goto out_err;
line += 4;
len -= 4;
@@ -131,15 +129,20 @@ static int err_pkt(git_pkt **out, const char *line, size_t len)
GITERR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1);
pkt = git__malloc(alloclen);
GITERR_CHECK_ALLOC(pkt);
-
pkt->type = GIT_PKT_ERR;
- pkt->len = (int)len;
+ pkt->len = len;
+
memcpy(pkt->error, line, len);
pkt->error[len] = '\0';
*out = (git_pkt *) pkt;
return 0;
+
+out_err:
+ giterr_set(GITERR_NET, "error parsing ERR pkt-line");
+ git__free(pkt);
+ return -1;
}
static int data_pkt(git_pkt **out, const char *line, size_t len)
@@ -155,7 +158,7 @@ static int data_pkt(git_pkt **out, const char *line, size_t len)
GITERR_CHECK_ALLOC(pkt);
pkt->type = GIT_PKT_DATA;
- pkt->len = (int) len;
+ pkt->len = len;
memcpy(pkt->data, line, len);
*out = (git_pkt *) pkt;
@@ -176,7 +179,7 @@ static int sideband_progress_pkt(git_pkt **out, const char *line, size_t len)
GITERR_CHECK_ALLOC(pkt);
pkt->type = GIT_PKT_PROGRESS;
- pkt->len = (int) len;
+ pkt->len = len;
memcpy(pkt->data, line, len);
*out = (git_pkt *) pkt;
@@ -212,28 +215,25 @@ static int sideband_error_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;
size_t alloclen;
- pkt = git__malloc(sizeof(git_pkt_ref));
+ pkt = git__calloc(1, sizeof(git_pkt_ref));
GITERR_CHECK_ALLOC(pkt);
-
- memset(pkt, 0x0, sizeof(git_pkt_ref));
pkt->type = GIT_PKT_REF;
- 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;
- }
- /* Jump from the name */
- line += GIT_OID_HEXSZ + 1;
- len -= (GIT_OID_HEXSZ + 1);
+ if (len < GIT_OID_HEXSZ || git_oid_fromstr(&pkt->head.oid, line) < 0)
+ goto out_err;
+ line += GIT_OID_HEXSZ;
+ len -= GIT_OID_HEXSZ;
+
+ if (git__prefixncmp(line, len, " "))
+ goto out_err;
+ line++;
+ len--;
+
+ if (!len)
+ goto out_err;
if (line[len - 1] == '\n')
--len;
@@ -245,36 +245,36 @@ static int ref_pkt(git_pkt **out, const char *line, size_t len)
memcpy(pkt->head.name, line, len);
pkt->head.name[len] = '\0';
- if (strlen(pkt->head.name) < len) {
+ if (strlen(pkt->head.name) < len)
pkt->capabilities = strchr(pkt->head.name, '\0') + 1;
- }
*out = (git_pkt *)pkt;
return 0;
-error_out:
+out_err:
+ giterr_set(GITERR_NET, "error parsing REF pkt-line");
+ if (pkt)
+ git__free(pkt->head.name);
git__free(pkt);
- return error;
+ return -1;
}
static int ok_pkt(git_pkt **out, const char *line, size_t len)
{
git_pkt_ok *pkt;
- const char *ptr;
size_t alloc_len;
pkt = git__malloc(sizeof(*pkt));
GITERR_CHECK_ALLOC(pkt);
-
pkt->type = GIT_PKT_OK;
- line += 3; /* skip "ok " */
- if (!(ptr = strchr(line, '\n'))) {
- giterr_set(GITERR_NET, "invalid packet line");
- git__free(pkt);
- return -1;
- }
- len = ptr - line;
+ if (git__prefixncmp(line, len, "ok "))
+ goto out_err;
+ line += 3;
+ len -= 3;
+
+ if (line[len - 1] == '\n')
+ --len;
GITERR_CHECK_ALLOC_ADD(&alloc_len, len, 1);
pkt->ref = git__malloc(alloc_len);
@@ -285,12 +285,17 @@ static int ok_pkt(git_pkt **out, const char *line, size_t len)
*out = (git_pkt *)pkt;
return 0;
+
+out_err:
+ giterr_set(GITERR_NET, "error parsing OK pkt-line");
+ git__free(pkt);
+ return -1;
}
static int ng_pkt(git_pkt **out, const char *line, size_t len)
{
git_pkt_ng *pkt;
- const char *ptr;
+ const char *ptr, *eol;
size_t alloclen;
pkt = git__malloc(sizeof(*pkt));
@@ -299,11 +304,13 @@ static int ng_pkt(git_pkt **out, const char *line, size_t len)
pkt->ref = NULL;
pkt->type = GIT_PKT_NG;
- if (len < 3)
+ eol = line + len;
+
+ if (git__prefixncmp(line, len, "ng "))
goto out_err;
- line += 3; /* skip "ng " */
- len -= 3;
- if (!(ptr = memchr(line, ' ', len)))
+ line += 3;
+
+ if (!(ptr = memchr(line, ' ', eol - line)))
goto out_err;
len = ptr - line;
@@ -314,11 +321,11 @@ static int ng_pkt(git_pkt **out, const char *line, size_t len)
memcpy(pkt->ref, line, len);
pkt->ref[len] = '\0';
- if (len < 1)
- goto out_err;
line = ptr + 1;
- len -= 1;
- if (!(ptr = memchr(line, '\n', len)))
+ if (line >= eol)
+ goto out_err;
+
+ if (!(ptr = memchr(line, '\n', eol - line)))
goto out_err;
len = ptr - line;
@@ -343,13 +350,11 @@ static int unpack_pkt(git_pkt **out, const char *line, size_t len)
{
git_pkt_unpack *pkt;
- GIT_UNUSED(len);
-
pkt = git__malloc(sizeof(*pkt));
GITERR_CHECK_ALLOC(pkt);
-
pkt->type = GIT_PKT_UNPACK;
- if (!git__prefixcmp(line, "unpack ok"))
+
+ if (!git__prefixncmp(line, len, "unpack ok"))
pkt->unpack_ok = 1;
else
pkt->unpack_ok = 0;
@@ -358,13 +363,17 @@ static int unpack_pkt(git_pkt **out, const char *line, size_t len)
return 0;
}
-static int32_t parse_len(const char *line)
+static int parse_len(size_t *out, const char *line, size_t linelen)
{
char num[PKT_LEN_SIZE + 1];
int i, k, error;
int32_t len;
const char *num_end;
+ /* Not even enough for the length */
+ if (linelen < PKT_LEN_SIZE)
+ return GIT_EBUFS;
+
memcpy(num, line, PKT_LEN_SIZE);
num[PKT_LEN_SIZE] = '\0';
@@ -376,7 +385,7 @@ static int32_t parse_len(const char *line)
num[k] = '.';
}
}
-
+
giterr_set(GITERR_NET, "invalid hex digit in length: '%s'", num);
return -1;
}
@@ -385,7 +394,11 @@ static int32_t parse_len(const char *line)
if ((error = git__strtol32(&len, num, &num_end, 16)) < 0)
return error;
- return len;
+ if (len < 0)
+ return -1;
+
+ *out = (size_t) len;
+ return 0;
}
/*
@@ -402,35 +415,32 @@ static int32_t parse_len(const char *line)
*/
int git_pkt_parse_line(
- git_pkt **head, const char *line, const char **out, size_t bufflen)
+ git_pkt **pkt, const char **endptr, const char *line, size_t linelen)
{
- int ret;
- int32_t len;
-
- /* Not even enough for the length */
- if (bufflen > 0 && bufflen < PKT_LEN_SIZE)
- return GIT_EBUFS;
+ int error;
+ size_t len;
- len = parse_len(line);
- if (len < 0) {
+ if ((error = parse_len(&len, line, linelen)) < 0) {
/*
- * If we fail to parse the length, it might be because the
- * server is trying to send us the packfile already.
+ * If we fail to parse the length, it might be
+ * because the server is trying to send us the
+ * packfile already or because we do not yet have
+ * enough data.
*/
- if (bufflen >= 4 && !git__prefixcmp(line, "PACK")) {
- giterr_clear();
- *out = line;
- return pack_pkt(head);
- }
-
- return (int)len;
+ if (error == GIT_EBUFS)
+ ;
+ else if (!git__prefixncmp(line, linelen, "PACK"))
+ giterr_set(GITERR_NET, "unexpected pack file");
+ else
+ giterr_set(GITERR_NET, "bad packet length");
+ return error;
}
/*
- * If we were given a buffer length, then make sure there is
- * enough in the buffer to satisfy this line
+ * Make sure there is enough in the buffer to satisfy
+ * this line.
*/
- if (bufflen > 0 && bufflen < (size_t)len)
+ if (linelen < len)
return GIT_EBUFS;
/*
@@ -453,38 +463,38 @@ int git_pkt_parse_line(
}
if (len == 0) { /* Flush pkt */
- *out = line;
- return flush_pkt(head);
+ *endptr = line;
+ return flush_pkt(pkt);
}
len -= PKT_LEN_SIZE; /* the encoded length includes its own size */
if (*line == GIT_SIDE_BAND_DATA)
- ret = data_pkt(head, line, len);
+ error = data_pkt(pkt, line, len);
else if (*line == GIT_SIDE_BAND_PROGRESS)
- ret = sideband_progress_pkt(head, line, len);
+ error = sideband_progress_pkt(pkt, line, len);
else if (*line == GIT_SIDE_BAND_ERROR)
- ret = sideband_error_pkt(head, line, len);
- else if (!git__prefixcmp(line, "ACK"))
- ret = ack_pkt(head, line, len);
- else if (!git__prefixcmp(line, "NAK"))
- ret = nak_pkt(head);
- else if (!git__prefixcmp(line, "ERR "))
- ret = err_pkt(head, line, len);
+ error = sideband_error_pkt(pkt, line, len);
+ else if (!git__prefixncmp(line, len, "ACK"))
+ error = ack_pkt(pkt, line, len);
+ else if (!git__prefixncmp(line, len, "NAK"))
+ error = nak_pkt(pkt);
+ else if (!git__prefixncmp(line, len, "ERR"))
+ error = err_pkt(pkt, line, len);
else if (*line == '#')
- ret = comment_pkt(head, line, len);
- else if (!git__prefixcmp(line, "ok"))
- ret = ok_pkt(head, line, len);
- else if (!git__prefixcmp(line, "ng"))
- ret = ng_pkt(head, line, len);
- else if (!git__prefixcmp(line, "unpack"))
- ret = unpack_pkt(head, line, len);
+ error = comment_pkt(pkt, line, len);
+ else if (!git__prefixncmp(line, len, "ok"))
+ error = ok_pkt(pkt, line, len);
+ else if (!git__prefixncmp(line, len, "ng"))
+ error = ng_pkt(pkt, line, len);
+ else if (!git__prefixncmp(line, len, "unpack"))
+ error = unpack_pkt(pkt, line, len);
else
- ret = ref_pkt(head, line, len);
+ error = ref_pkt(pkt, line, len);
- *out = line + len;
+ *endptr = line + len;
- return ret;
+ return error;
}
void git_pkt_free(git_pkt *pkt)
diff --git a/src/transports/smart_protocol.c b/src/transports/smart_protocol.c
index aecfece..893ac6e 100644
--- a/src/transports/smart_protocol.c
+++ b/src/transports/smart_protocol.c
@@ -44,7 +44,7 @@ int git_smart__store_refs(transport_smart *t, int flushes)
do {
if (buf->offset > 0)
- error = git_pkt_parse_line(&pkt, buf->data, &line_end, buf->offset);
+ error = git_pkt_parse_line(&pkt, &line_end, buf->data, buf->offset);
else
error = GIT_EBUFS;
@@ -209,15 +209,15 @@ int git_smart__detect_caps(git_pkt_ref *pkt, transport_smart_caps *caps, git_vec
return 0;
}
-static int recv_pkt(git_pkt **out, gitno_buffer *buf)
+static int recv_pkt(git_pkt **out, git_pkt_type *pkt_type, gitno_buffer *buf)
{
const char *ptr = buf->data, *line_end = ptr;
git_pkt *pkt = NULL;
- int pkt_type, error = 0, ret;
+ int error = 0, ret;
do {
if (buf->offset > 0)
- error = git_pkt_parse_line(&pkt, ptr, &line_end, buf->offset);
+ error = git_pkt_parse_line(&pkt, &line_end, ptr, buf->offset);
else
error = GIT_EBUFS;
@@ -236,13 +236,14 @@ static int recv_pkt(git_pkt **out, gitno_buffer *buf)
} while (error);
gitno_consume(buf, line_end);
- pkt_type = pkt->type;
+ if (pkt_type)
+ *pkt_type = pkt->type;
if (out != NULL)
*out = pkt;
else
git__free(pkt);
- return pkt_type;
+ return error;
}
static int store_common(transport_smart *t)
@@ -252,7 +253,7 @@ static int store_common(transport_smart *t)
int error;
do {
- if ((error = recv_pkt(&pkt, buf)) < 0)
+ if ((error = recv_pkt(&pkt, NULL, buf)) < 0)
return error;
if (pkt->type == GIT_PKT_ACK) {
@@ -320,7 +321,7 @@ static int wait_while_ack(gitno_buffer *buf)
while (1) {
git__free(pkt);
- if ((error = recv_pkt((git_pkt **)&pkt, buf)) < 0)
+ if ((error = recv_pkt((git_pkt **)&pkt, NULL, buf)) < 0)
return error;
if (pkt->type == GIT_PKT_NAK)
@@ -345,7 +346,8 @@ int git_smart__negotiate_fetch(git_transport *transport, git_repository *repo, c
gitno_buffer *buf = &t->buffer;
git_buf data = GIT_BUF_INIT;
git_revwalk *walk = NULL;
- int error = -1, pkt_type;
+ int error = -1;
+ git_pkt_type pkt_type;
unsigned int i;
git_oid oid;
@@ -395,16 +397,13 @@ int git_smart__negotiate_fetch(git_transport *transport, git_repository *repo, c
if ((error = store_common(t)) < 0)
goto on_error;
} else {
- pkt_type = recv_pkt(NULL, buf);
-
- if (pkt_type == GIT_PKT_ACK) {
+ error = recv_pkt(NULL, &pkt_type, buf);
+ if (error < 0) {
+ goto on_error;
+ } else if (pkt_type == GIT_PKT_ACK) {
break;
} else if (pkt_type == GIT_PKT_NAK) {
continue;
- } else if (pkt_type < 0) {
- /* recv_pkt returned an error */
- error = pkt_type;
- goto on_error;
} else {
giterr_set(GITERR_NET, "Unexpected pkt type");
error = -1;
@@ -470,10 +469,10 @@ int git_smart__negotiate_fetch(git_transport *transport, git_repository *repo, c
/* Now let's eat up whatever the server gives us */
if (!t->caps.multi_ack && !t->caps.multi_ack_detailed) {
- pkt_type = recv_pkt(NULL, buf);
+ error = recv_pkt(NULL, &pkt_type, buf);
- if (pkt_type < 0) {
- return pkt_type;
+ if (error < 0) {
+ return error;
} else if (pkt_type != GIT_PKT_ACK && pkt_type != GIT_PKT_NAK) {
giterr_set(GITERR_NET, "Unexpected pkt type");
return -1;
@@ -594,7 +593,7 @@ int git_smart__download_pack(
goto done;
}
- if ((error = recv_pkt(&pkt, buf)) >= 0) {
+ if ((error = recv_pkt(&pkt, NULL, buf)) >= 0) {
/* Check cancellation after network call */
if (t->cancelled.val) {
giterr_clear();
@@ -752,7 +751,7 @@ static int add_push_report_sideband_pkt(git_push *push, git_pkt_data *data_pkt,
}
while (line_len > 0) {
- error = git_pkt_parse_line(&pkt, line, &line_end, line_len);
+ error = git_pkt_parse_line(&pkt, &line_end, line, line_len);
if (error == GIT_EBUFS) {
/* Buffer the data when the inner packet is split
@@ -795,8 +794,8 @@ static int parse_report(transport_smart *transport, git_push *push)
for (;;) {
if (buf->offset > 0)
- error = git_pkt_parse_line(&pkt, buf->data,
- &line_end, buf->offset);
+ error = git_pkt_parse_line(&pkt, &line_end,
+ buf->data, buf->offset);
else
error = GIT_EBUFS;
diff --git a/tests/config/include.c b/tests/config/include.c
index e440b9a..87ef406 100644
--- a/tests/config/include.c
+++ b/tests/config/include.c
@@ -35,6 +35,8 @@ void test_config_include__absolute(void)
cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.baz"));
cl_assert_equal_s("huzzah", git_buf_cstr(&buf));
+
+ cl_git_pass(p_unlink("config-include-absolute"));
}
void test_config_include__homedir(void)
@@ -48,6 +50,8 @@ void test_config_include__homedir(void)
cl_assert_equal_s("huzzah", git_buf_cstr(&buf));
cl_sandbox_set_search_path_defaults();
+
+ cl_git_pass(p_unlink("config-include-homedir"));
}
/* We need to pretend that the variables were defined where the file was included */
@@ -66,6 +70,9 @@ void test_config_include__ordering(void)
git_buf_clear(&buf);
cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.baz"));
cl_assert_equal_s("huzzah", git_buf_cstr(&buf));
+
+ cl_git_pass(p_unlink("included"));
+ cl_git_pass(p_unlink("including"));
}
/* We need to pretend that the variables were defined where the file was included */
@@ -76,8 +83,18 @@ void test_config_include__depth(void)
cl_git_fail(git_config_open_ondisk(&cfg, "a"));
- p_unlink("a");
- p_unlink("b");
+ cl_git_pass(p_unlink("a"));
+ cl_git_pass(p_unlink("b"));
+}
+
+void test_config_include__empty_path_sanely_handled(void)
+{
+ cl_git_mkfile("a", "[include]\npath");
+ cl_git_pass(git_config_open_ondisk(&cfg, "a"));
+ cl_git_pass(git_config_get_string_buf(&buf, cfg, "include.path"));
+ cl_assert_equal_s("", git_buf_cstr(&buf));
+
+ cl_git_pass(p_unlink("a"));
}
void test_config_include__missing(void)
@@ -89,6 +106,8 @@ void test_config_include__missing(void)
cl_assert(giterr_last() == NULL);
cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar"));
cl_assert_equal_s("baz", git_buf_cstr(&buf));
+
+ cl_git_pass(p_unlink("including"));
}
void test_config_include__missing_homedir(void)
@@ -103,6 +122,7 @@ void test_config_include__missing_homedir(void)
cl_assert_equal_s("baz", git_buf_cstr(&buf));
cl_sandbox_set_search_path_defaults();
+ cl_git_pass(p_unlink("including"));
}
#define replicate10(s) s s s s s s s s s s
@@ -122,6 +142,10 @@ void test_config_include__depth2(void)
git_buf_clear(&buf);
cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar2"));
cl_assert_equal_s("baz2", git_buf_cstr(&buf));
+
+ cl_git_pass(p_unlink("top-level"));
+ cl_git_pass(p_unlink("middle"));
+ cl_git_pass(p_unlink("bottom"));
}
void test_config_include__removing_include_removes_values(void)
@@ -132,6 +156,9 @@ void test_config_include__removing_include_removes_values(void)
cl_git_pass(git_config_open_ondisk(&cfg, "top-level"));
cl_git_mkfile("top-level", "");
cl_git_fail(git_config_get_string_buf(&buf, cfg, "foo.bar"));
+
+ cl_git_pass(p_unlink("top-level"));
+ cl_git_pass(p_unlink("included"));
}
void test_config_include__rewriting_include_refreshes_values(void)
@@ -145,6 +172,10 @@ void test_config_include__rewriting_include_refreshes_values(void)
cl_git_fail(git_config_get_string_buf(&buf, cfg, "foo.bar"));
cl_git_pass(git_config_get_string_buf(&buf, cfg, "first.other"));
cl_assert_equal_s(buf.ptr, "value");
+
+ cl_git_pass(p_unlink("top-level"));
+ cl_git_pass(p_unlink("first"));
+ cl_git_pass(p_unlink("second"));
}
void test_config_include__included_variables_cannot_be_deleted(void)
@@ -154,13 +185,20 @@ void test_config_include__included_variables_cannot_be_deleted(void)
cl_git_pass(git_config_open_ondisk(&cfg, "top-level"));
cl_git_fail(git_config_delete_entry(cfg, "foo.bar"));
+
+ cl_git_pass(p_unlink("top-level"));
+ cl_git_pass(p_unlink("included"));
}
void test_config_include__included_variables_cannot_be_modified(void)
{
cl_git_mkfile("top-level", "[include]\npath = included\n");
+
cl_git_mkfile("included", "[foo]\nbar = value");
cl_git_pass(git_config_open_ondisk(&cfg, "top-level"));
cl_git_fail(git_config_set_string(cfg, "foo.bar", "other-value"));
+
+ cl_git_pass(p_unlink("top-level"));
+ cl_git_pass(p_unlink("included"));
}
diff --git a/tests/submodule/inject_option.c b/tests/submodule/inject_option.c
new file mode 100644
index 0000000..c13e12c
--- /dev/null
+++ b/tests/submodule/inject_option.c
@@ -0,0 +1,80 @@
+#include "clar_libgit2.h"
+#include "posix.h"
+#include "path.h"
+#include "submodule_helpers.h"
+#include "fileops.h"
+#include "repository.h"
+
+static git_repository *g_repo = NULL;
+
+void test_submodule_inject_option__initialize(void)
+{
+ g_repo = setup_fixture_submodule_simple();
+}
+
+void test_submodule_inject_option__cleanup(void)
+{
+ cl_git_sandbox_cleanup();
+}
+
+static int find_naughty(git_submodule *sm, const char *name, void *payload)
+{
+ int *foundit = (int *) payload;
+
+ GIT_UNUSED(sm);
+
+ if (!git__strcmp("naughty", name))
+ *foundit = true;
+
+ return 0;
+}
+
+void test_submodule_inject_option__url(void)
+{
+ int foundit;
+ git_submodule *sm;
+ git_buf buf = GIT_BUF_INIT;
+
+ cl_git_pass(git_buf_joinpath(&buf, git_repository_workdir(g_repo), ".gitmodules"));
+ cl_git_rewritefile(buf.ptr,
+ "[submodule \"naughty\"]\n"
+ " path = testrepo\n"
+ " url = -u./payload\n");
+ git_buf_free(&buf);
+
+ /* We do want to find it, but with the appropriate field empty */
+ foundit = 0;
+ cl_git_pass(git_submodule_foreach(g_repo, find_naughty, &foundit));
+ cl_assert_equal_i(1, foundit);
+
+ cl_git_pass(git_submodule_lookup(&sm, g_repo, "naughty"));
+ cl_assert_equal_s("testrepo", git_submodule_path(sm));
+ cl_assert_equal_p(NULL, git_submodule_url(sm));
+
+ git_submodule_free(sm);
+}
+
+void test_submodule_inject_option__path(void)
+{
+ int foundit;
+ git_submodule *sm;
+ git_buf buf = GIT_BUF_INIT;
+
+ cl_git_pass(git_buf_joinpath(&buf, git_repository_workdir(g_repo), ".gitmodules"));
+ cl_git_rewritefile(buf.ptr,
+ "[submodule \"naughty\"]\n"
+ " path = --something\n"
+ " url = blah.git\n");
+ git_buf_free(&buf);
+
+ /* We do want to find it, but with the appropriate field empty */
+ foundit = 0;
+ cl_git_pass(git_submodule_foreach(g_repo, find_naughty, &foundit));
+ cl_assert_equal_i(1, foundit);
+
+ cl_git_pass(git_submodule_lookup(&sm, g_repo, "naughty"));
+ cl_assert_equal_s("naughty", git_submodule_path(sm));
+ cl_assert_equal_s("blah.git", git_submodule_url(sm));
+
+ git_submodule_free(sm);
+}
diff --git a/tests/transports/smart/packet.c b/tests/transports/smart/packet.c
new file mode 100644
index 0000000..5b623a3
--- /dev/null
+++ b/tests/transports/smart/packet.c
@@ -0,0 +1,340 @@
+#include "clar_libgit2.h"
+#include "transports/smart.h"
+
+enum expected_status {
+ PARSE_SUCCESS,
+ PARSE_FAILURE
+};
+
+static void assert_flush_parses(const char *line)
+{
+ size_t linelen = strlen(line) + 1;
+ const char *endptr;
+ git_pkt *pkt;
+
+ cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen));
+ cl_assert_equal_i(pkt->type, GIT_PKT_FLUSH);
+ cl_assert_equal_strn(endptr, line + 4, linelen - 4);
+
+ git_pkt_free((git_pkt *) pkt);
+}
+
+static void assert_data_pkt_parses(const char *line, const char *expected_data, size_t expected_len)
+{
+ size_t linelen = strlen(line) + 1;
+ const char *endptr;
+ git_pkt_data *pkt;
+
+ cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen));
+ cl_assert_equal_i(pkt->type, GIT_PKT_DATA);
+ cl_assert_equal_i(pkt->len, expected_len);
+ cl_assert_equal_strn(pkt->data, expected_data, expected_len);
+
+ git_pkt_free((git_pkt *) pkt);
+}
+
+static void assert_sideband_progress_parses(const char *line, const char *expected_data, size_t expected_len)
+{
+ size_t linelen = strlen(line) + 1;
+ const char *endptr;
+ git_pkt_progress *pkt;
+
+ cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen));
+ cl_assert_equal_i(pkt->type, GIT_PKT_PROGRESS);
+ cl_assert_equal_i(pkt->len, expected_len);
+ cl_assert_equal_strn(pkt->data, expected_data, expected_len);
+
+ git_pkt_free((git_pkt *) pkt);
+}
+
+static void assert_error_parses(const char *line, const char *expected_error, size_t expected_len)
+{
+ size_t linelen = strlen(line) + 1;
+ const char *endptr;
+ git_pkt_err *pkt;
+
+ cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen));
+ cl_assert_equal_i(pkt->type, GIT_PKT_ERR);
+ cl_assert_equal_i(pkt->len, expected_len);
+ cl_assert_equal_strn(pkt->error, expected_error, expected_len);
+
+ git_pkt_free((git_pkt *) pkt);
+}
+
+static void assert_ack_parses(const char *line, const char *expected_oid, enum git_ack_status expected_status)
+{
+ size_t linelen = strlen(line) + 1;
+ const char *endptr;
+ git_pkt_ack *pkt;
+ git_oid oid;
+
+ cl_git_pass(git_oid_fromstr(&oid, expected_oid));
+
+ cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen));
+ cl_assert_equal_i(pkt->type, GIT_PKT_ACK);
+ cl_assert_equal_oid(&pkt->oid, &oid);
+ cl_assert_equal_i(pkt->status, expected_status);
+
+ git_pkt_free((git_pkt *) pkt);
+}
+
+static void assert_nak_parses(const char *line)
+{
+ size_t linelen = strlen(line) + 1;
+ const char *endptr;
+ git_pkt *pkt;
+
+ cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen));
+ cl_assert_equal_i(pkt->type, GIT_PKT_NAK);
+ cl_assert_equal_strn(endptr, line + 7, linelen - 7);
+
+ git_pkt_free((git_pkt *) pkt);
+}
+
+static void assert_comment_parses(const char *line, const char *expected_comment)
+{
+ size_t linelen = strlen(line) + 1;
+ const char *endptr;
+ git_pkt_comment *pkt;
+
+ cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen));
+ cl_assert_equal_i(pkt->type, GIT_PKT_COMMENT);
+ cl_assert_equal_strn(pkt->comment, expected_comment, strlen(expected_comment));
+
+ git_pkt_free((git_pkt *) pkt);
+}
+
+static void assert_ok_parses(const char *line, const char *expected_ref)
+{
+ size_t linelen = strlen(line) + 1;
+ const char *endptr;
+ git_pkt_ok *pkt;
+
+ cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen));
+ cl_assert_equal_i(pkt->type, GIT_PKT_OK);
+ cl_assert_equal_strn(pkt->ref, expected_ref, strlen(expected_ref));
+
+ git_pkt_free((git_pkt *) pkt);
+}
+
+static void assert_unpack_parses(const char *line, bool ok)
+{
+ size_t linelen = strlen(line) + 1;
+ const char *endptr;
+ git_pkt_unpack *pkt;
+
+ cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen));
+ cl_assert_equal_i(pkt->type, GIT_PKT_UNPACK);
+ cl_assert_equal_i(pkt->unpack_ok, ok);
+
+ git_pkt_free((git_pkt *) pkt);
+}
+
+static void assert_ng_parses(const char *line, const char *expected_ref, const char *expected_msg)
+{
+ size_t linelen = strlen(line) + 1;
+ const char *endptr;
+ git_pkt_ng *pkt;
+
+ cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen));
+ cl_assert_equal_i(pkt->type, GIT_PKT_NG);
+ cl_assert_equal_strn(pkt->ref, expected_ref, strlen(expected_ref));
+ cl_assert_equal_strn(pkt->msg, expected_msg, strlen(expected_msg));
+
+ git_pkt_free((git_pkt *) pkt);
+}
+
+#define assert_ref_parses(line, expected_oid, expected_ref, expected_capabilities) \
+ assert_ref_parses_(line, sizeof(line), expected_oid, expected_ref, expected_capabilities)
+
+static void assert_ref_parses_(const char *line, size_t linelen, const char *expected_oid,
+ const char *expected_ref, const char *expected_capabilities)
+{
+ const char *endptr;
+ git_pkt_ref *pkt;
+ git_oid oid;
+
+ cl_git_pass(git_oid_fromstr(&oid, expected_oid));
+
+ cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen));
+ cl_assert_equal_i(pkt->type, GIT_PKT_REF);
+ cl_assert_equal_oid(&pkt->head.oid, &oid);
+ cl_assert_equal_strn(pkt->head.name, expected_ref, strlen(expected_ref));
+ if (expected_capabilities)
+ cl_assert_equal_strn(pkt->capabilities, expected_capabilities, strlen(expected_capabilities));
+ else
+ cl_assert_equal_p(NULL, pkt->capabilities);
+
+ git_pkt_free((git_pkt *) pkt);
+}
+
+static void assert_pkt_fails(const char *line)
+{
+ const char *endptr;
+ git_pkt *pkt;
+ cl_git_fail(git_pkt_parse_line(&pkt, &endptr, line, strlen(line) + 1));
+}
+
+void test_transports_smart_packet__parsing_garbage_fails(void)
+{
+ assert_pkt_fails("0foobar");
+ assert_pkt_fails("00foobar");
+ assert_pkt_fails("000foobar");
+ assert_pkt_fails("0001");
+ assert_pkt_fails("");
+ assert_pkt_fails("0");
+ assert_pkt_fails("0i00");
+ assert_pkt_fails("f");
+}
+
+void test_transports_smart_packet__flush_parses(void)
+{
+ assert_flush_parses("0000");
+ assert_flush_parses("0000foobar");
+}
+
+void test_transports_smart_packet__data_pkt(void)
+{
+ assert_pkt_fails("000foobar");
+ assert_pkt_fails("0001o");
+ assert_pkt_fails("0001\1");
+ assert_data_pkt_parses("0005\1", "", 0);
+ assert_pkt_fails("0009\1o");
+ assert_data_pkt_parses("0009\1data", "data", 4);
+ assert_data_pkt_parses("000a\1data", "data", 5);
+}
+
+void test_transports_smart_packet__sideband_progress_pkt(void)
+{
+ assert_pkt_fails("0001\2");
+ assert_sideband_progress_parses("0005\2", "", 0);
+ assert_pkt_fails("0009\2o");
+ assert_sideband_progress_parses("0009\2data", "data", 4);
+ assert_sideband_progress_parses("000a\2data", "data", 5);
+}
+
+void test_transports_smart_packet__sideband_err_pkt(void)
+{
+ assert_pkt_fails("0001\3");
+ assert_error_parses("0005\3", "", 0);
+ assert_pkt_fails("0009\3o");
+ assert_error_parses("0009\3data", "data", 4);
+ assert_error_parses("000a\3data", "data", 5);
+}
+
+void test_transports_smart_packet__ack_pkt(void)
+{
+ assert_ack_parses("0030ACK 0000000000000000000000000000000000000000",
+ "0000000000000000000000000000000000000000", 0);
+ assert_ack_parses("0039ACK 0000000000000000000000000000000000000000 continue",
+ "0000000000000000000000000000000000000000",
+ GIT_ACK_CONTINUE);
+ assert_ack_parses("0037ACK 0000000000000000000000000000000000000000 common",
+ "0000000000000000000000000000000000000000",
+ GIT_ACK_COMMON);
+ assert_ack_parses("0037ACK 0000000000000000000000000000000000000000 ready",
+ "0000000000000000000000000000000000000000",
+ GIT_ACK_READY);
+
+ /* these should fail as they don't have OIDs */
+ assert_pkt_fails("0007ACK");
+ assert_pkt_fails("0008ACK ");
+
+ /* this one is missing a space and should thus fail */
+ assert_pkt_fails("0036ACK00000000000000000x0000000000000000000000 ready");
+
+ /* the following ones have invalid OIDs and should thus fail */
+ assert_pkt_fails("0037ACK 00000000000000000x0000000000000000000000 ready");
+ assert_pkt_fails("0036ACK 000000000000000000000000000000000000000 ready");
+ assert_pkt_fails("0036ACK 00000000000000000x0000000000000000000000ready");
+
+ /* this one has an invalid status and should thus fail */
+ assert_pkt_fails("0036ACK 0000000000000000000000000000000000000000 read");
+}
+
+void test_transports_smart_packet__nak_pkt(void)
+{
+ assert_nak_parses("0007NAK");
+ assert_pkt_fails("0007NaK");
+ assert_pkt_fails("0007nak");
+ assert_nak_parses("0007NAKfoobar");
+ assert_pkt_fails("0007nakfoobar");
+ assert_pkt_fails("0007 NAK");
+}
+
+void test_transports_smart_packet__error_pkt(void)
+{
+ assert_pkt_fails("0007ERR");
+ assert_pkt_fails("0008ERRx");
+ assert_error_parses("0008ERR ", "", 0);
+ assert_error_parses("000EERR ERRMSG", "ERRMSG", 6);
+}
+
+void test_transports_smart_packet__comment_pkt(void)
+{
+ assert_comment_parses("0005#", "");
+ assert_comment_parses("000B#foobar", "#fooba");
+ assert_comment_parses("000C#foobar", "#foobar");
+ assert_comment_parses("001A#this is a comment\nfoo", "#this is a comment\nfoo");
+}
+
+void test_transports_smart_packet__ok_pkt(void)
+{
+ assert_pkt_fails("0007ok\n");
+ assert_ok_parses("0007ok ", "");
+ assert_ok_parses("0008ok \n", "");
+ assert_ok_parses("0008ok x", "x");
+ assert_ok_parses("0009ok x\n", "x");
+ assert_pkt_fails("001OK ref/foo/bar");
+ assert_ok_parses("0012ok ref/foo/bar", "ref/foo/bar");
+ assert_pkt_fails("0013OK ref/foo/bar\n");
+ assert_ok_parses("0013ok ref/foo/bar\n", "ref/foo/bar");
+}
+
+void test_transports_smart_packet__ng_pkt(void)
+{
+ /* TODO: same as for ok pkt */
+ assert_pkt_fails("0007ng\n");
+ assert_pkt_fails("0008ng \n");
+ assert_pkt_fails("000Bng ref\n");
+ assert_pkt_fails("000Bng ref\n");
+ /* TODO: is this a valid packet line? Probably not. */
+ assert_ng_parses("000Ang x\n", "", "x");
+ assert_ng_parses("000Fng ref msg\n", "ref", "msg");
+ assert_ng_parses("000Fng ref msg\n", "ref", "msg");
+}
+
+void test_transports_smart_packet__unpack_pkt(void)
+{
+ assert_unpack_parses("000Dunpack ok", 1);
+ assert_unpack_parses("000Dunpack ng error-msg", 0);
+ /* TODO: the following tests should fail */
+ assert_unpack_parses("000Aunpack", 0);
+ assert_unpack_parses("0011unpack foobar", 0);
+ assert_unpack_parses("0010unpack ng ok", 0);
+ assert_unpack_parses("0010unpack okfoo", 1);
+}
+
+void test_transports_smart_packet__ref_pkt(void)
+{
+ assert_pkt_fails("002C0000000000000000000000000000000000000000");
+ assert_pkt_fails("002D0000000000000000000000000000000000000000\n");
+ assert_pkt_fails("00300000000000000000000000000000000000000000HEAD");
+ assert_pkt_fails("004800000000x0000000000000000000000000000000 refs/heads/master\0multi_ack");
+ assert_ref_parses(
+ "003F0000000000000000000000000000000000000000 refs/heads/master\0",
+ "0000000000000000000000000000000000000000", "refs/heads/master", "");
+ assert_ref_parses(
+ "00480000000000000000000000000000000000000000 refs/heads/master\0multi_ack",
+ "0000000000000000000000000000000000000000", "refs/heads/master", "multi_ack");
+ assert_ref_parses(
+ "00460000000000000000000000000000000000000000 refs/heads/master\0one two",
+ "0000000000000000000000000000000000000000", "refs/heads/master", "one two");
+ assert_ref_parses(
+ "00310000000000000000000000000000000000000000 HEAD",
+ "0000000000000000000000000000000000000000", "HEAD", NULL);
+ assert_pkt_fails("0031000000000000000000000000000000000000000 HEAD");
+ assert_ref_parses(
+ "00360000000000000000000000000000000000000000 HEAD HEAD",
+ "0000000000000000000000000000000000000000", "HEAD HEAD", NULL);
+}