Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 18c4ae7

Browse files
pks-tEdward Thomson
authored and
Edward Thomson
committed
filebuf: handle write error in lock_file
When writing to a file with locking not check if writing the locked file actually succeeds. Fix the issue by returning error code and message when writing fails.
1 parent f17ed63 commit 18c4ae7

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/filebuf.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ static int lock_file(git_filebuf *file, int flags, mode_t mode)
7070
git_file source;
7171
char buffer[FILEIO_BUFSIZE];
7272
ssize_t read_bytes;
73+
int error;
7374

7475
source = p_open(file->path_original, O_RDONLY);
7576
if (source < 0) {
@@ -80,7 +81,8 @@ static int lock_file(git_filebuf *file, int flags, mode_t mode)
8081
}
8182

8283
while ((read_bytes = p_read(source, buffer, sizeof(buffer))) > 0) {
83-
p_write(file->fd, buffer, read_bytes);
84+
if ((error = p_write(file->fd, buffer, read_bytes)) < 0)
85+
break;
8486
if (file->compute_digest)
8587
git_hash_update(&file->digest, buffer, read_bytes);
8688
}
@@ -90,6 +92,9 @@ static int lock_file(git_filebuf *file, int flags, mode_t mode)
9092
if (read_bytes < 0) {
9193
giterr_set(GITERR_OS, "Failed to read file '%s'", file->path_original);
9294
return -1;
95+
} else if (error < 0) {
96+
giterr_set(GITERR_OS, "Failed to write file '%s'", file->path_lock);
97+
return -1;
9398
}
9499
}
95100

0 commit comments

Comments
 (0)