Skip to content

Commit 41dcc4d

Browse files
peffgitster
authored andcommitted
distinguish error versus short read from read_in_full()
Many callers of read_in_full() expect to see the exact number of bytes requested, but their error handling lumps together true read errors and short reads due to unexpected EOF. We can give more specific error messages by separating these cases (showing errno when appropriate, and otherwise describing the short read). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 90dca67 commit 41dcc4d

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

builtin/get-tar-commit-id.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
2626
usage(builtin_get_tar_commit_id_usage);
2727

2828
n = read_in_full(0, buffer, HEADERSIZE);
29+
if (n < 0)
30+
die_errno("git get-tar-commit-id: read error");
2931
if (n != HEADERSIZE)
30-
die("git get-tar-commit-id: read error");
32+
die_errno("git get-tar-commit-id: EOF before reading tar header");
3133
if (header->typeflag[0] != 'g')
3234
return 1;
3335
if (!skip_prefix(content, "52 comment=", &comment))

bulk-checkin.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ static int stream_to_pack(struct bulk_checkin_state *state,
115115

116116
if (size && !s.avail_in) {
117117
ssize_t rsize = size < sizeof(ibuf) ? size : sizeof(ibuf);
118-
if (read_in_full(fd, ibuf, rsize) != rsize)
118+
ssize_t read_result = read_in_full(fd, ibuf, rsize);
119+
if (read_result < 0)
120+
die_errno("failed to read from '%s'", path);
121+
if (read_result != rsize)
119122
die("failed to read %d bytes from '%s'",
120123
(int)rsize, path);
121124
offset += rsize;

packfile.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ static int open_packed_git_1(struct packed_git *p)
444444
unsigned char sha1[20];
445445
unsigned char *idx_sha1;
446446
long fd_flag;
447+
ssize_t read_result;
447448

448449
if (!p->index_data && open_pack_index(p))
449450
return error("packfile %s index unavailable", p->pack_name);
@@ -485,7 +486,10 @@ static int open_packed_git_1(struct packed_git *p)
485486
return error("cannot set FD_CLOEXEC");
486487

487488
/* Verify we recognize this pack file format. */
488-
if (read_in_full(p->pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
489+
read_result = read_in_full(p->pack_fd, &hdr, sizeof(hdr));
490+
if (read_result < 0)
491+
return error_errno("error reading from %s", p->pack_name);
492+
if (read_result != sizeof(hdr))
489493
return error("file %s is far too short to be a packfile", p->pack_name);
490494
if (hdr.hdr_signature != htonl(PACK_SIGNATURE))
491495
return error("file %s is not a GIT packfile", p->pack_name);
@@ -502,7 +506,10 @@ static int open_packed_git_1(struct packed_git *p)
502506
p->num_objects);
503507
if (lseek(p->pack_fd, p->pack_size - sizeof(sha1), SEEK_SET) == -1)
504508
return error("end of packfile %s is unavailable", p->pack_name);
505-
if (read_in_full(p->pack_fd, sha1, sizeof(sha1)) != sizeof(sha1))
509+
read_result = read_in_full(p->pack_fd, sha1, sizeof(sha1));
510+
if (read_result < 0)
511+
return error_errno("error reading from %s", p->pack_name);
512+
if (read_result != sizeof(sha1))
506513
return error("packfile %s signature is unavailable", p->pack_name);
507514
idx_sha1 = ((unsigned char *)p->index_data) + p->index_size - 40;
508515
if (hashcmp(sha1, idx_sha1))

0 commit comments

Comments
 (0)