Skip to content

Commit 228740b

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Reviewed-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 41dcc4d commit 228740b

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

builtin/worktree.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ static int prune_worktree(const char *id, struct strbuf *reason)
3838
{
3939
struct stat st;
4040
char *path;
41-
int fd, len;
41+
int fd;
42+
size_t len;
4243

4344
if (!is_directory(git_path("worktrees/%s", id))) {
4445
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)
5657
id, strerror(errno));
5758
return 1;
5859
}
59-
len = st.st_size;
60+
len = xsize_t(st.st_size);
6061
path = xmallocz(len);
6162
read_in_full(fd, path, len);
6263
close(fd);

0 commit comments

Comments
 (0)