* Find file diff header * * Returns: * -1 if no header was found * -128 in case of error * the size of the header in bytes (called "offset") otherwise */
| 1586 | * the size of the header in bytes (called "offset") otherwise |
| 1587 | */ |
| 1588 | static int find_header(struct apply_state *state, |
| 1589 | const char *line, |
| 1590 | unsigned long size, |
| 1591 | int *hdrsize, |
| 1592 | struct patch *patch) |
| 1593 | { |
| 1594 | unsigned long offset, len; |
| 1595 | |
| 1596 | patch->is_toplevel_relative = 0; |
| 1597 | patch->is_rename = patch->is_copy = 0; |
| 1598 | patch->is_new = patch->is_delete = -1; |
| 1599 | patch->old_mode = patch->new_mode = 0; |
| 1600 | patch->old_name = patch->new_name = NULL; |
| 1601 | for (offset = 0; size > 0; offset += len, size -= len, line += len, state->linenr++) { |
| 1602 | unsigned long nextlen; |
| 1603 | |
| 1604 | len = linelen(line, size); |
| 1605 | if (!len) |
| 1606 | break; |
| 1607 | |
| 1608 | /* Testing this early allows us to take a few shortcuts.. */ |
| 1609 | if (len < 6) |
| 1610 | continue; |
| 1611 | |
| 1612 | /* |
| 1613 | * Make sure we don't find any unconnected patch fragments. |
| 1614 | * That's a sign that we didn't find a header, and that a |
| 1615 | * patch has become corrupted/broken up. |
| 1616 | */ |
| 1617 | if (!memcmp("@@ -", line, 4)) { |
| 1618 | struct fragment dummy; |
| 1619 | if (parse_fragment_header(line, len, &dummy) < 0) |
| 1620 | continue; |
| 1621 | error(_("patch fragment without header at %s:%d: %.*s"), |
| 1622 | state->patch_input_file, state->linenr, |
| 1623 | (int)len-1, line); |
| 1624 | return -128; |
| 1625 | } |
| 1626 | |
| 1627 | if (size < len + 6) |
| 1628 | break; |
| 1629 | |
| 1630 | /* |
| 1631 | * Git patch? It might not have a real patch, just a rename |
| 1632 | * or mode change, so we handle that specially |
| 1633 | */ |
| 1634 | if (!memcmp("diff --git ", line, 11)) { |
| 1635 | int git_hdr_len = parse_git_diff_header(&state->root, |
| 1636 | state->patch_input_file, |
| 1637 | &state->linenr, |
| 1638 | state->p_value, line, len, |
| 1639 | size, patch); |
| 1640 | if (git_hdr_len < 0) |
| 1641 | return -128; |
| 1642 | if (git_hdr_len <= len) |
| 1643 | continue; |
| 1644 | *hdrsize = git_hdr_len; |
| 1645 | return offset; |
no test coverage detected