aboutsummaryrefslogtreecommitdiff
path: root/usr.bin/diff3
diff options
context:
space:
mode:
authorPiotr Pawel Stefaniak <pstef@FreeBSD.org>2021-08-20 21:35:24 +0000
committerPiotr Pawel Stefaniak <pstef@FreeBSD.org>2021-08-23 05:04:28 +0000
commit7f7b03f3897f0196e3cc7a3b71c7359cc206ba61 (patch)
tree2fbfefe9177c50558935db1900645426aef967b1 /usr.bin/diff3
parentf8e50dd2c67baea2bbd75f94218911d5a4d39597 (diff)
Diffstat (limited to 'usr.bin/diff3')
-rw-r--r--usr.bin/diff3/diff3.c21
1 files 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));