commit - 303e14b5e91b152d78219ff8bbca1f9bfbbb094e
commit + 6dea73dfe78ac9b1bfcc7b80edecc00974c50c38
blob - 89b27a76d95a86dc536d024d98960d0bb5f2db78
blob + e0d39fea2dc4bc2bc3f44c7a12c733643e58951e
--- lib/diff3.c
+++ lib/diff3.c
char f1mark[PATH_MAX], f3mark[PATH_MAX]; /* markers for -E and -X */
char *buf;
- size_t bufsize;
BUF *diffbuf;
};
static char *
get_line(FILE *b, size_t *n, struct diff3_state *d3s)
{
- char *cp;
- size_t len;
+ char *cp = NULL;
+ size_t size, len;
char *new;
+ char *ret = NULL;
- if ((cp = fgetln(b, &len)) == NULL)
- return (NULL);
+ len = getline(&cp, &size, b);
+ if (len == -1)
+ goto done;
- if (cp[len - 1] != '\n')
+ if (cp[len - 1] != '\n') {
len++;
- if (len + 1 > d3s->bufsize) {
- do {
- d3s->bufsize += 1024;
- } while (len + 1 > d3s->bufsize);
- new = reallocarray(d3s->buf, 1, d3s->bufsize);
- if (new == NULL)
- return NULL;
- d3s->buf = new;
+ if (len + 1 > size) {
+ new = realloc(cp, len + 1);
+ if (new == NULL)
+ goto done;
+ cp = new;
+ }
+ cp[len - 1] = '\n';
+ cp[len] = '\0';
}
- memcpy(d3s->buf, cp, len - 1);
- d3s->buf[len - 1] = '\n';
- d3s->buf[len] = '\0';
+
+ free(d3s->buf);
+ ret = d3s->buf = cp;
+ cp = NULL;
if (n != NULL)
*n = len;
-
- return (d3s->buf);
+done:
+ free(cp);
+ return (ret);
}
static int