diff options
author | Junio C Hamano <gitster@pobox.com> | 2017-10-03 15:42:49 +0900 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2017-10-03 15:42:49 +0900 |
commit | cb1083ca23c2f78140b66b925ed0b82fe400eea5 (patch) | |
tree | c78509d2b9fbe79c99ed1acb83bf43886b4d0c0c /builtin/worktree.c | |
parent | d4e93836a6a072e392b20d7daf604fd41e15ecf9 (diff) | |
parent | 8a1a8d2ad1b41a0a28d37d1d21ee9620a23e91eb (diff) | |
download | git-cb1083ca23c2f78140b66b925ed0b82fe400eea5.tar.gz git-cb1083ca23c2f78140b66b925ed0b82fe400eea5.tar.xz |
Merge branch 'jk/read-in-full'
Code clean-up to prevent future mistakes by copying and pasting
code that checks the result of read_in_full() function.
* jk/read-in-full:
worktree: check the result of read_in_full()
worktree: use xsize_t to access file size
distinguish error versus short read from read_in_full()
avoid looking at errno for short read_in_full() returns
prefer "!=" when checking read_in_full() result
notes-merge: drop dead zero-write code
files-backend: prefer "0" for write_in_full() error check
Diffstat (limited to 'builtin/worktree.c')
-rw-r--r-- | builtin/worktree.c | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/builtin/worktree.c b/builtin/worktree.c index de26849f5..7b9307aa5 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -38,7 +38,9 @@ static int prune_worktree(const char *id, struct strbuf *reason) { struct stat st; char *path; - int fd, len; + int fd; + size_t len; + ssize_t read_result; if (!is_directory(git_path("worktrees/%s", id))) { strbuf_addf(reason, _("Removing worktrees/%s: not a valid directory"), id); @@ -56,10 +58,26 @@ static int prune_worktree(const char *id, struct strbuf *reason) id, strerror(errno)); return 1; } - len = st.st_size; + len = xsize_t(st.st_size); path = xmallocz(len); - read_in_full(fd, path, len); + + read_result = read_in_full(fd, path, len); + if (read_result < 0) { + strbuf_addf(reason, _("Removing worktrees/%s: unable to read gitdir file (%s)"), + id, strerror(errno)); + close(fd); + free(path); + return 1; + } close(fd); + + if (read_result != len) { + strbuf_addf(reason, + _("Removing worktrees/%s: short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"), + id, (uintmax_t)len, (uintmax_t)read_result); + free(path); + return 1; + } while (len && (path[len - 1] == '\n' || path[len - 1] == '\r')) len--; if (!len) { |