From c0c93af82c3cd03bb5d4a2c82b9679eb6f329afe Mon Sep 17 00:00:00 2001 From: stsp Date: Fri, 26 Jun 2020 07:28:46 +0000 Subject: Fix merging of files that lack newlines for diff(3), OpenRCS, and OpenCVS. Merges with a file that lacks newlines (\n) were triggering a fatal error. This could be easily reproduced with merge(1) and diff3(1): $ echo foo > foo $ echo bar > bar $ echo -n baz > baz $ merge -p foo bar baz merge: failed to merge $ diff3 -E foo bar baz 1a ======= diff3prog: logic error $ Fix this by properly handling short reads from the third file argument. Only the third file argument triggered the problem. The other input files were already handled correctly. ok millert@ --- usr.bin/diff3/diff3prog.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'usr.bin/diff3/diff3prog.c') diff --git a/usr.bin/diff3/diff3prog.c b/usr.bin/diff3/diff3prog.c index 992037b6d85..a56bfc7ce7a 100644 --- a/usr.bin/diff3/diff3prog.c +++ b/usr.bin/diff3/diff3prog.c @@ -1,4 +1,4 @@ -/* $OpenBSD: diff3prog.c,v 1.19 2016/10/18 21:06:52 millert Exp $ */ +/* $OpenBSD: diff3prog.c,v 1.20 2020/06/26 07:28:47 stsp Exp $ */ /* * Copyright (C) Caldera International Inc. 2001-2002. @@ -555,9 +555,16 @@ edscript(int n) printf("%da\n=======\n", de[n].old.to -1); (void)fseek(fp[2], (long)de[n].new.from, SEEK_SET); for (k = de[n].new.to-de[n].new.from; k > 0; k-= j) { + size_t r; j = k > BUFSIZ ? BUFSIZ : k; - if (fread(block, 1, j, fp[2]) != j) + r = fread(block, 1, j, fp[2]); + if (r == 0) { + if (feof(fp[2])) + break; trouble(); + } + if (r != j) + j = r; (void)fwrite(block, 1, j, stdout); } if (!oflag || !overlap[n]) -- cgit v1.2.3-59-g8ed1b