worktree: use xsize_t to access file size

To read the "gitdir" file into memory, we stat the file and allocate a buffer. But we store the size in an "int", which may be truncated. We should use a size_t and xsize_t(), which will detect truncation. An overflow is unlikely for a "gitdir" file, but it's a good practice to model. Signed-off-by: Jeff King <peff@peff.net> Reviewed-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 27, 2017 at 02:02 UTC 228740b67b55f4ee23637bd1472a73ae50efe93a
1 file changed +3 -2
builtin/worktree.c
+3 -2
@@ -38,7 +38,8 @@ static int prune_worktree(const char *id, struct strbuf *reason)
38 {
39 struct stat st;
40 char *path;
41 - int fd, len;
41 + int fd;
42 + size_t len;
43
44 if (!is_directory(git_path("worktrees/%s", id))) {
45 strbuf_addf(reason, _("Removing worktrees/%s: not a valid directory"), id);
@@ -56,7 +57,7 @@ static int prune_worktree(const char *id, struct strbuf *reason)
57 id, strerror(errno));
58 return 1;
59 }
59 - len = st.st_size;
60 + len = xsize_t(st.st_size);
61 path = xmallocz(len);
62 read_in_full(fd, path, len);
63 close(fd);