check for errors from diff_output() in diff3.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
diff --git a/lib/diff3.c b/lib/diff3.c
index 962e41a..d5408e8 100644
--- a/lib/diff3.c
+++ b/lib/diff3.c
@@ -168,9 +168,9 @@ static const struct got_error *merge(size_t, size_t, struct diff3_state *);
static const struct got_error *change(int, struct range *, int,
struct diff3_state *);
static const struct got_error *keep(int, struct range *, struct diff3_state *);
-static void prange(struct range *, struct diff3_state *);
+static const struct got_error *prange(struct range *, struct diff3_state *);
static const struct got_error *repos(int, struct diff3_state *);
-static void separate(const char *, struct diff3_state *);
+static const struct got_error *separate(const char *, struct diff3_state *);
static const struct got_error *increase(struct diff3_state *);
static const struct got_error *diff3_internal(char *, char *, char *,
char *, char *, const char *, const char *, struct diff3_state *,
@@ -735,7 +735,9 @@ merge(size_t m1, size_t m2, struct diff3_state *d3s)
if (!t2 || (t1 && d1->new.to < d2->new.from)) {
/* stuff peculiar to 1st file */
if (d3s->eflag == 0) {
- separate("1", d3s);
+ err = separate("1", d3s);
+ if (err)
+ return err;
err = change(1, &d1->old, 0, d3s);
if (err)
return err;
@@ -753,7 +755,9 @@ merge(size_t m1, size_t m2, struct diff3_state *d3s)
/* second file is different from others */
if (!t1 || (t2 && d2->new.to < d1->new.from)) {
if (d3s->eflag == 0) {
- separate("2", d3s);
+ err = separate("2", d3s);
+ if (err)
+ return err;
err = keep(1, &d2->new, d3s);
if (err)
return err;
@@ -797,7 +801,9 @@ merge(size_t m1, size_t m2, struct diff3_state *d3s)
* dpl = 1 means files 1 and 2 identical
*/
if (d3s->eflag == 0) {
- separate(dpl ? "3" : "", d3s);
+ err = separate(dpl ? "3" : "", d3s);
+ if (err)
+ return err;
err = change(1, &d1->old, dpl, d3s);
if (err)
return err;
@@ -841,10 +847,10 @@ merge(size_t m1, size_t m2, struct diff3_state *d3s)
return (edscript(j, d3s));
}
-static void
+static const struct got_error *
separate(const char *s, struct diff3_state *d3s)
{
- diff_output(d3s->diffbuf, "====%s\n", s);
+ return diff_output(d3s->diffbuf, "====%s\n", s);
}
/*
@@ -858,9 +864,13 @@ change(int i, struct range *rold, int fdup, struct diff3_state *d3s)
const struct got_error *err = NULL;
int nskipped;
- diff_output(d3s->diffbuf, "%d:", i);
+ err = diff_output(d3s->diffbuf, "%d:", i);
+ if (err)
+ return err;
d3s->last[i] = rold->to;
- prange(rold, d3s);
+ err = prange(rold, d3s);
+ if (err)
+ return err;
if (fdup || d3s->debug)
return NULL;
i--;
@@ -873,17 +883,30 @@ change(int i, struct range *rold, int fdup, struct diff3_state *d3s)
/*
* print the range of line numbers, rold.from thru rold.to, as n1,n2 or n1
*/
-static void
+static const struct got_error *
prange(struct range *rold, struct diff3_state *d3s)
{
- if (rold->to <= rold->from)
- diff_output(d3s->diffbuf, "%da\n", rold->from - 1);
- else {
- diff_output(d3s->diffbuf, "%d", rold->from);
- if (rold->to > rold->from+1)
- diff_output(d3s->diffbuf, ",%d", rold->to - 1);
- diff_output(d3s->diffbuf, "c\n");
+ const struct got_error *err = NULL;
+
+ if (rold->to <= rold->from) {
+ err = diff_output(d3s->diffbuf, "%da\n", rold->from - 1);
+ if (err)
+ return err;
+ } else {
+ err = diff_output(d3s->diffbuf, "%d", rold->from);
+ if (err)
+ return err;
+ if (rold->to > rold->from + 1) {
+ err = diff_output(d3s->diffbuf, ",%d", rold->to - 1);
+ if (err)
+ return err;
+ }
+ err = diff_output(d3s->diffbuf, "c\n");
+ if (err)
+ return err;
}
+
+ return NULL;
}
/*
@@ -919,8 +942,11 @@ skip(int *nskipped, int i, int from, char *pr, struct diff3_state *d3s)
err = get_line(&line, d3s->fp[i], &j, d3s);
if (err)
return err;
- if (pr != NULL)
- diff_output(d3s->diffbuf, "%s%s", pr, line);
+ if (pr != NULL) {
+ err = diff_output(d3s->diffbuf, "%s%s", pr, line);
+ if (err)
+ return err;
+ }
d3s->cline[i]++;
}
*nskipped = n;
@@ -1014,15 +1040,21 @@ edit(struct diff *diff, int fdup, int *j, struct diff3_state *d3s)
static const struct got_error *
edscript(int n, struct diff3_state *d3s)
{
+ const struct got_error *err = NULL;
int j, k;
char block[BUFSIZ+1];
for (; n > 0; n--) {
- if (!d3s->overlap[n])
- prange(&d3s->de[n].old, d3s);
- else
- diff_output(d3s->diffbuf, "%da\n%s\n",
+ if (!d3s->overlap[n]) {
+ err = prange(&d3s->de[n].old, d3s);
+ if (err)
+ return err;
+ } else {
+ err = diff_output(d3s->diffbuf, "%da\n%s\n",
d3s->de[n].old.to -1, GOT_DIFF_CONFLICT_MARKER_SEP);
+ if (err)
+ return err;
+ }
(void)fseek(d3s->fp[2], (long)d3s->de[n].new.from, SEEK_SET);
k = d3s->de[n].new.to - d3s->de[n].new.from;
for (; k > 0; k-= j) {
@@ -1030,15 +1062,23 @@ edscript(int n, struct diff3_state *d3s)
if (fread(block, 1, j, d3s->fp[2]) != (size_t)j)
return got_ferror(d3s->fp[2], GOT_ERR_IO);
block[j] = '\0';
- diff_output(d3s->diffbuf, "%s", block);
+ err = diff_output(d3s->diffbuf, "%s", block);
+ if (err)
+ return err;
}
- if (!d3s->overlap[n])
- diff_output(d3s->diffbuf, ".\n");
- else {
- diff_output(d3s->diffbuf, "%s\n.\n", d3s->f3mark);
- diff_output(d3s->diffbuf, "%da\n%s\n.\n",
+ if (!d3s->overlap[n]) {
+ err = diff_output(d3s->diffbuf, ".\n");
+ if (err)
+ return err;
+ } else {
+ err = diff_output(d3s->diffbuf, "%s\n.\n", d3s->f3mark);
+ if (err)
+ return err;
+ err = diff_output(d3s->diffbuf, "%da\n%s\n.\n",
d3s->de[n].old.from - 1, d3s->f1mark);
+ if (err)
+ return err;
}
}