SPACE tspace;
regex_t *re;
size_t re_off;
+ size_t re_eoff;
int n;
char *s;
+ char *eos;
s = ps;
+ eos = s + strlen(s);
re = cp->u.s->re;
if (re == NULL) {
if (defpreg != NULL && cp->u.s->maxbref > defpreg->re_nsub) {
do {
/* Locate start of replaced string. */
re_off = match[0].rm_so;
+ re_eoff = match[0].rm_eo;
/* Copy leading retained string. */
cspace(&SS, s, re_off, APPEND);
/* Add in regular expression. */
regsub(&SS, s, cp->u.s->new);
/* Move past this match. */
s += match[0].rm_eo;
- } while(regexec_e(re, s, REG_NOTBOL, 0));
+ } while(*s && re_eoff && regexec_e(re, s, REG_NOTBOL, 0));
+ if (eos - s > 0 && !re_eoff)
+ err(FATAL, "infinite substitution loop");
/* Copy trailing retained string. */
cspace(&SS, s, strlen(s), APPEND);
break;
* need two extra bytes, one for the newline, one for a terminating
* NULL.
*/
- tlen = sp->len + len + spflag == APPENDNL ? 2 : 1;
+/* tlen = sp->len + len + spflag == APPENDNL ? 2 : 1; */
+ tlen = sp->len + len + (spflag == APPENDNL ? 2 : 1); /* XXX */
if (tlen > sp->blen) {
sp->blen = tlen + 1024;
sp->space = sp->back = xrealloc(sp->back, sp->blen);