* Confirm that the headers of a commit or tag object end in a reasonable way, * either with the usual "\n\n" separator, or at least with a trailing newline * on the final header line. * * This property is important for the memory safety of our callers. It allows * them to scan the buffer linewise without constantly checking the remaining * size as long as: * * - they check that there are
| 827 | * OK for them to use helpers like parse_oid_hex(), or even skip_prefix(). |
| 828 | */ |
| 829 | static int verify_headers(const void *data, unsigned long size, |
| 830 | const struct object_id *oid, enum object_type type, |
| 831 | struct fsck_options *options) |
| 832 | { |
| 833 | const char *buffer = (const char *)data; |
| 834 | unsigned long i; |
| 835 | |
| 836 | for (i = 0; i < size; i++) { |
| 837 | switch (buffer[i]) { |
| 838 | case '\0': |
| 839 | return report(options, oid, type, |
| 840 | FSCK_MSG_NUL_IN_HEADER, |
| 841 | "unterminated header: NUL at offset %ld", i); |
| 842 | case '\n': |
| 843 | if (i + 1 < size && buffer[i + 1] == '\n') |
| 844 | return 0; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | /* |
| 849 | * We did not find double-LF that separates the header |
| 850 | * and the body. Not having a body is not a crime but |
| 851 | * we do want to see the terminating LF for the last header |
| 852 | * line. |
| 853 | */ |
| 854 | if (size && buffer[size - 1] == '\n') |
| 855 | return 0; |
| 856 | |
| 857 | return report(options, oid, type, |
| 858 | FSCK_MSG_UNTERMINATED_HEADER, "unterminated header"); |
| 859 | } |
| 860 | |
| 861 | static timestamp_t parse_timestamp_from_buf(const char **start, const char *end) |
| 862 | { |
no test coverage detected