worktree: check the result of read_in_full()
We try to read "len" bytes into a buffer and just assume that it happened correctly. In practice this should usually be the case, since we just stat'd the file to get the length. But we could be fooled by transient errors or by other processes racily truncating the file. Let's be more careful. There's a slim chance this could catch a real error, but it also prevents people and tools from getting worried while reading the code. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Sep 27, 2017 at 02:02 UTC
8a1a8d2ad1b41a0a28d37d1d21ee9620a23e91eb
1 file changed
+18
-1
builtin/worktree.c
+18
-1
@@ -40,6 +40,7 @@ static int prune_worktree(const char *id, struct strbuf *reason)
40
char *path;
41
int fd;
42
size_t len;
43
+ ssize_t read_result;
44
45
if (!is_directory(git_path("worktrees/%s", id))) {
46
strbuf_addf(reason, _("Removing worktrees/%s: not a valid directory"), id);
@@ -59,8 +60,24 @@ static int prune_worktree(const char *id, struct strbuf *reason)
60
}
61
len = xsize_t(st.st_size);
62
path = xmallocz(len);
62
- read_in_full(fd, path, len);
63
+
64
+ read_result = read_in_full(fd, path, len);
65
+ if (read_result < 0) {
66
+ strbuf_addf(reason, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
67
+ id, strerror(errno));
68
+ close(fd);
69
+ free(path);
70
+ return 1;
71
+ }
72
close(fd);
73
+
74
+ if (read_result != len) {
75
+ strbuf_addf(reason,
76
+ _("Removing worktrees/%s: short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"),
77
+ id, (uintmax_t)len, (uintmax_t)read_result);
78
+ free(path);
79
+ return 1;
80
+ }
81
while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
82
len--;
83
if (!len) {