Skip to content

Commit cb1083c

Browse files
committed
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
2 parents d4e9383 + 8a1a8d2 commit cb1083c

File tree

10 files changed

+55
-17
lines changed

10 files changed

+55
-17
lines changed

builtin/get-tar-commit-id.c

Lines changed: 4 additions & 2 deletions
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 < HEADERSIZE)
30-
die("git get-tar-commit-id: read error");
29+
if (n < 0)
30+
die_errno("git get-tar-commit-id: read error");
31+
if (n != HEADERSIZE)
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))

builtin/worktree.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ 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;
43+
ssize_t read_result;
4244

4345
if (!is_directory(git_path("worktrees/%s", id))) {
4446
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)
5658
id, strerror(errno));
5759
return 1;
5860
}
59-
len = st.st_size;
61+
len = xsize_t(st.st_size);
6062
path = xmallocz(len);
61-
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+
}
6272
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+
}
6381
while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
6482
len--;
6583
if (!len) {

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;

csum-file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static void flush(struct sha1file *f, const void *buf, unsigned int count)
1919

2020
if (ret < 0)
2121
die_errno("%s: sha1 file read error", f->name);
22-
if (ret < count)
22+
if (ret != count)
2323
die("%s: sha1 file truncated", f->name);
2424
if (memcmp(buf, check_buffer, count))
2525
die("sha1 file '%s' validation error", f->name);

notes-merge.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,6 @@ static void write_buf_to_worktree(const struct object_id *obj,
308308
if (errno == EPIPE)
309309
break;
310310
die_errno("notes-merge");
311-
} else if (!ret) {
312-
die("notes-merge: disk full?");
313311
}
314312
size -= ret;
315313
buf += ret;

pack-write.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,19 @@ void fixup_pack_header_footer(int pack_fd,
213213
git_SHA_CTX old_sha1_ctx, new_sha1_ctx;
214214
struct pack_header hdr;
215215
char *buf;
216+
ssize_t read_result;
216217

217218
git_SHA1_Init(&old_sha1_ctx);
218219
git_SHA1_Init(&new_sha1_ctx);
219220

220221
if (lseek(pack_fd, 0, SEEK_SET) != 0)
221222
die_errno("Failed seeking to start of '%s'", pack_name);
222-
if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
223+
read_result = read_in_full(pack_fd, &hdr, sizeof(hdr));
224+
if (read_result < 0)
223225
die_errno("Unable to reread header of '%s'", pack_name);
226+
else if (read_result != sizeof(hdr))
227+
die_errno("Unexpected short read for header of '%s'",
228+
pack_name);
224229
if (lseek(pack_fd, 0, SEEK_SET) != 0)
225230
die_errno("Failed seeking to start of '%s'", pack_name);
226231
git_SHA1_Update(&old_sha1_ctx, &hdr, sizeof(hdr));

packfile.c

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

446447
if (!p->index_data && open_pack_index(p))
447448
return error("packfile %s index unavailable", p->pack_name);
@@ -483,7 +484,10 @@ static int open_packed_git_1(struct packed_git *p)
483484
return error("cannot set FD_CLOEXEC");
484485

485486
/* Verify we recognize this pack file format. */
486-
if (read_in_full(p->pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
487+
read_result = read_in_full(p->pack_fd, &hdr, sizeof(hdr));
488+
if (read_result < 0)
489+
return error_errno("error reading from %s", p->pack_name);
490+
if (read_result != sizeof(hdr))
487491
return error("file %s is far too short to be a packfile", p->pack_name);
488492
if (hdr.hdr_signature != htonl(PACK_SIGNATURE))
489493
return error("file %s is not a GIT packfile", p->pack_name);
@@ -500,7 +504,10 @@ static int open_packed_git_1(struct packed_git *p)
500504
p->num_objects);
501505
if (lseek(p->pack_fd, p->pack_size - sizeof(sha1), SEEK_SET) == -1)
502506
return error("end of packfile %s is unavailable", p->pack_name);
503-
if (read_in_full(p->pack_fd, sha1, sizeof(sha1)) != sizeof(sha1))
507+
read_result = read_in_full(p->pack_fd, sha1, sizeof(sha1));
508+
if (read_result < 0)
509+
return error_errno("error reading from %s", p->pack_name);
510+
if (read_result != sizeof(sha1))
504511
return error("packfile %s signature is unavailable", p->pack_name);
505512
idx_sha1 = ((unsigned char *)p->index_data) + p->index_size - 40;
506513
if (hashcmp(sha1, idx_sha1))

pkt-line.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ static int get_packet_data(int fd, char **src_buf, size_t *src_size,
258258
}
259259

260260
/* And complain if we didn't get enough bytes to satisfy the read. */
261-
if (ret < size) {
261+
if (ret != size) {
262262
if (options & PACKET_READ_GENTLE_ON_EOF)
263263
return -1;
264264

refs/files-backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3035,7 +3035,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
30353035
} else if (update &&
30363036
(write_in_full(get_lock_file_fd(&lock->lk),
30373037
oid_to_hex(&cb.last_kept_oid), GIT_SHA1_HEXSZ) < 0 ||
3038-
write_str_in_full(get_lock_file_fd(&lock->lk), "\n") < 1 ||
3038+
write_str_in_full(get_lock_file_fd(&lock->lk), "\n") < 0 ||
30393039
close_ref_gently(lock) < 0)) {
30403040
status |= error("couldn't write %s",
30413041
get_lock_file_path(&lock->lk));

sha1_file.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,10 +1748,15 @@ static int index_core(unsigned char *sha1, int fd, size_t size,
17481748
ret = index_mem(sha1, "", size, type, path, flags);
17491749
} else if (size <= SMALL_FILE_SIZE) {
17501750
char *buf = xmalloc(size);
1751-
if (size == read_in_full(fd, buf, size))
1752-
ret = index_mem(sha1, buf, size, type, path, flags);
1751+
ssize_t read_result = read_in_full(fd, buf, size);
1752+
if (read_result < 0)
1753+
ret = error_errno("read error while indexing %s",
1754+
path ? path : "<unknown>");
1755+
else if (read_result != size)
1756+
ret = error("short read while indexing %s",
1757+
path ? path : "<unknown>");
17531758
else
1754-
ret = error_errno("short read");
1759+
ret = index_mem(sha1, buf, size, type, path, flags);
17551760
free(buf);
17561761
} else {
17571762
void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);

0 commit comments

Comments
 (0)