get-tar-commit-id: parse comment record
Parse pax comment records properly and get rid of magic numbers for acceptable comment length. This simplifies a later change to handle longer hashes. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Rene Scharfe committed
Feb 19, 2019 at 00:05 UTC
3548726cd7f5c5d2d8a1359ac21ae0354bd4c895
1 file changed
+11
-2
builtin/get-tar-commit-id.c
+11
-2
@@ -21,6 +21,8 @@ int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
21
char *content = buffer + RECORDSIZE;
22
const char *comment;
23
ssize_t n;
24
+ long len;
25
+ char *end;
26
27
if (argc != 1)
28
usage(builtin_get_tar_commit_id_usage);
@@ -32,10 +34,17 @@ int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
34
die_errno("git get-tar-commit-id: EOF before reading tar header");
35
if (header->typeflag[0] != 'g')
36
return 1;
35
- if (!skip_prefix(content, "52 comment=", &comment))
37
+
38
+ len = strtol(content, &end, 10);
39
+ if (errno == ERANGE || end == content || len < 0)
40
+ return 1;
41
+ if (!skip_prefix(end, " comment=", &comment))
42
+ return 1;
43
+ len -= comment - content;
44
+ if (len != GIT_SHA1_HEXSZ + 1)
45
return 1;
46
38
- if (write_in_full(1, comment, 41) < 0)
47
+ if (write_in_full(1, comment, len) < 0)
48
die_errno("git get-tar-commit-id: write error");
49
50
return 0;