Skip to content

Commit 0cf1f44

Browse files
committed
diff_print: handle errors when printing to file
When printing the diff to a `FILE *` handle, we neither check the return value of fputc(3P) nor the one of fwrite(3P). As a result, we'll silently return successful even if we didn't print anything at all. Futhermore, the arguments to fwrite(3P) are reversed: we have one item of length `content_len`, and not `content_len` items of one byte. Fix both issues by checking return values as well as reversing the arguments to fwrite(3P).
1 parent d60bf00 commit 0cf1f44

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/diff_print.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -742,14 +742,27 @@ int git_diff_print_callback__to_file_handle(
742742
void *payload)
743743
{
744744
FILE *fp = payload ? payload : stdout;
745+
int error;
745746

746-
GIT_UNUSED(delta); GIT_UNUSED(hunk);
747+
GIT_UNUSED(delta);
748+
GIT_UNUSED(hunk);
747749

748750
if (line->origin == GIT_DIFF_LINE_CONTEXT ||
749-
line->origin == GIT_DIFF_LINE_ADDITION ||
750-
line->origin == GIT_DIFF_LINE_DELETION)
751-
fputc(line->origin, fp);
752-
fwrite(line->content, 1, line->content_len, fp);
751+
line->origin == GIT_DIFF_LINE_ADDITION ||
752+
line->origin == GIT_DIFF_LINE_DELETION) {
753+
while ((error = fputc(line->origin, fp)) == EINTR)
754+
continue;
755+
if (error) {
756+
git_error_set(GIT_ERROR_OS, "could not write status");
757+
return -1;
758+
}
759+
}
760+
761+
if (fwrite(line->content, line->content_len, 1, fp) != 1) {
762+
git_error_set(GIT_ERROR_OS, "could not write line");
763+
return -1;
764+
}
765+
753766
return 0;
754767
}
755768

0 commit comments

Comments
 (0)