Commit 21c17f12d490c7f4b55a696178331c5b490a18d4

Stefan Sperling 2020-06-26T07:09:35

fix merging with files that do not contain a newline character; ok millert@

diff --git a/lib/diff3.c b/lib/diff3.c
index f17c676..e8841fa 100644
--- a/lib/diff3.c
+++ b/lib/diff3.c
@@ -1002,10 +1002,17 @@ edscript(int n, struct diff3_state *d3s)
 				return got_error_from_errno("fseeko");
 			k = (size_t)(d3s->de[n].oldo.to - d3s->de[n].oldo.from);
 			for (; k > 0; k -= len) {
+				size_t r;
 				len = k > BUFSIZ ? BUFSIZ : k;
-				if (fread(block, 1, len, d3s->fp[1]) != len)
+				r = fread(block, 1, len, d3s->fp[1]);
+				if (r == 0) {
+					if (feof(d3s->fp[1]))
+						break;
 					return got_ferror(d3s->fp[1],
 					    GOT_ERR_IO);
+				}
+				if (r != len)
+					len = r;
 				block[len] = '\0';
 				err = diff_output(d3s->diffbuf, "%s", block);
 				if (err)
@@ -1026,9 +1033,17 @@ edscript(int n, struct diff3_state *d3s)
 			return got_error_from_errno("fseek");
 		k = (size_t)(d3s->de[n].newo.to - d3s->de[n].newo.from);
 		for (; k > 0; k -= len) {
+			size_t r;
 			len = k > BUFSIZ ? BUFSIZ : k;
-			if (fread(block, 1, len, d3s->fp[2]) != len)
-				return got_ferror(d3s->fp[2], GOT_ERR_IO);
+			r = fread(block, 1, len, d3s->fp[2]);
+			if (r == 0) {
+				if (feof(d3s->fp[2]))
+					break;
+				return got_ferror(d3s->fp[2],
+				    GOT_ERR_IO);
+			}
+			if (r != len)
+				len = r;
 			block[len] = '\0';
 			err = diff_output(d3s->diffbuf, "%s", block);
 			if (err)