commit 12bdc69e781567bac907074a1ef25ba90500b628
parent 758264ed68da13a748b33d2f29c2bc51d3dab4b4
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Sat, 12 Dec 2015 09:50:33 +0100
Convert compile() array into a dynamic array
It removes one out of bounds error in compile
and avoid the arbitrary limit in the size of
the regular expression.
Diffstat:
M | ed.c | | | 32 | ++++++++++++++++---------------- |
1 file changed, 16 insertions(+), 16 deletions(-)
diff --git a/ed.c b/ed.c
@@ -26,6 +26,7 @@ struct hline {
char *prompt = "*";
regex_t *pattern;
+char *lastre;
int optverbose, optprompt, exstatus, optdiag = 1;
int marks['z' - 'a'];
@@ -275,13 +276,15 @@ setscratch()
static void
compile(int delim)
{
- static char regerrbuf[BUFSIZ];
- char *bp, buff[REGEXSIZE];
- int c, ret, bracket = 0;
+ int n, ret, c,bracket;
+ static size_t siz, cap;
+ static char buf[BUFSIZ];
if (!isgraph(delim))
error("invalid pattern delimiter");
- for (bp = buff; bp < &buff[BUFSIZ-1]; *bp++ = c) {
+
+ bracket = siz = 0;
+ for (n = 0;; ++n) {
if ((c = input()) == delim && !bracket)
break;
if (c == '\n' || c == EOF) {
@@ -290,32 +293,29 @@ compile(int delim)
}
if (c == '\\') {
- if (bp == &buff[BUFSIZ-2])
- break;
- *bp++ = c;
+ lastre = addchar(c, lastre, &cap, &siz);
c = input();
} else if (c == '[') {
bracket = 1;
} else if (c == ']') {
bracket = 0;
}
+ lastre = addchar(c, lastre, &cap, &siz);
}
- if (bp == buff) {
+ if (n == 0) {
if (!pattern)
error("no previous pattern");
return;
}
- if (bp == &buff[BUFSIZ-1]) {
- /* TODO: Use addchar here! */
- error("too long regular expression");
- }
- buff[bp - buff] = '\0';
+ lastre = addchar('\0', lastre, &cap, &siz);
+ if (pattern)
+ regfree(pattern);
if (!pattern && (!(pattern = malloc(sizeof(*pattern)))))
error("out of memory");
- if ((ret = regcomp(pattern, buff, 0))) {
- regerror(ret, pattern, regerrbuf, sizeof(regerrbuf));
- error(regerrbuf);
+ if ((ret = regcomp(pattern, lastre, 0))) {
+ regerror(ret, pattern, buf, sizeof(buf));
+ error(buf);
}
}