git: 4bfbc5fbc93b - stable/13 - diff3: sync with upstream

Piotr Pawel Stefaniak pstef at FreeBSD.org
Sat Sep 25 08:41:33 UTC 2021


The branch stable/13 has been updated by pstef:

URL: https://cgit.FreeBSD.org/src/commit/?id=4bfbc5fbc93bec4f1098b7c7174411ec92404cb0

commit 4bfbc5fbc93bec4f1098b7c7174411ec92404cb0
Author:     Piotr Pawel Stefaniak <pstef at FreeBSD.org>
AuthorDate: 2021-08-20 21:35:24 +0000
Commit:     Piotr Pawel Stefaniak <pstef at FreeBSD.org>
CommitDate: 2021-09-25 08:34:39 +0000

    diff3: sync with upstream
    
     * replace realloc calls with reallocarray calls
     * fix merging of files that lack newlines
    
    Obtained from:  OpenBSD
    
    (cherry picked from commit 7f7b03f3897f0196e3cc7a3b71c7359cc206ba61)
---
 usr.bin/diff3/diff3.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/usr.bin/diff3/diff3.c b/usr.bin/diff3/diff3.c
index 03cd24283a13..80d4a202728f 100644
--- a/usr.bin/diff3/diff3.c
+++ b/usr.bin/diff3/diff3.c
@@ -459,6 +459,8 @@ duplicate(struct range *r1, struct range *r2)
 		do {
 			c = getc(fp[0]);
 			d = getc(fp[1]);
+			if (c == -1 && d == -1)
+				break;
 			if (c == -1 || d== -1)
 				errx(EXIT_FAILURE, "logic error");
 			nchar++;
@@ -528,10 +530,17 @@ edscript(int n)
 		}
 		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;
 				errx(2, "logic error");
-			fwrite(block, 1, j, stdout);
+			}
+			if (r != j)
+				j = r;
+			(void)fwrite(block, 1, j, stdout);
 		}
 		if (!oflag || !overlap[n])
 			printf(".\n");
@@ -557,22 +566,22 @@ increase(void)
 	newsz = szchanges == 0 ? 64 : 2 * szchanges;
 	incr = newsz - szchanges;
 
-	p = realloc(d13, newsz * sizeof(struct diff));
+	p = reallocarray(d13, newsz, sizeof(struct diff));
 	if (p == NULL)
 		err(1, NULL);
 	memset(p + szchanges, 0, incr * sizeof(struct diff));
 	d13 = p;
-	p = realloc(d23, newsz * sizeof(struct diff));
+	p = reallocarray(d23, newsz, sizeof(struct diff));
 	if (p == NULL)
 		err(1, NULL);
 	memset(p + szchanges, 0, incr * sizeof(struct diff));
 	d23 = p;
-	p = realloc(de, newsz * sizeof(struct diff));
+	p = reallocarray(de, newsz, sizeof(struct diff));
 	if (p == NULL)
 		err(1, NULL);
 	memset(p + szchanges, 0, incr * sizeof(struct diff));
 	de = p;
-	q = realloc(overlap, newsz * sizeof(char));
+	q = reallocarray(overlap, newsz, sizeof(char));
 	if (q == NULL)
 		err(1, NULL);
 	memset(q + szchanges, 0, incr * sizeof(char));


More information about the dev-commits-src-branches mailing list