sed: fix error handling for "Output line too long" and other stuff

sed just continued writing past genbuf when it should stop
with "Output line too long".

quit when we get unspecified options.

stupid casts from long to char* for no reason.

some 0 vs nil cleanup.
This commit is contained in:
cinap_lenrek 2014-11-16 01:48:18 +01:00
parent d069c9b486
commit 242007de7c

View file

@ -96,7 +96,7 @@ SedCom pspace[MAXCMDS]; /* Command storage */
SedCom *pend = pspace+MAXCMDS; /* End of command storage */ SedCom *pend = pspace+MAXCMDS; /* End of command storage */
SedCom *rep = pspace; /* Current fill point */ SedCom *rep = pspace; /* Current fill point */
Reprog *lastre = 0; /* Last regular expression */ Reprog *lastre; /* Last regular expression */
Resub subexp[MAXSUB]; /* sub-patterns of pattern match*/ Resub subexp[MAXSUB]; /* sub-patterns of pattern match*/
Rune addspace[ADDSIZE]; /* Buffer for a, c, & i commands */ Rune addspace[ADDSIZE]; /* Buffer for a, c, & i commands */
@ -116,10 +116,10 @@ struct { /* Sed program input control block */
}; };
} prog; } prog;
Rune genbuf[LBSIZE]; /* Miscellaneous buffer */ Rune genbuf[LBSIZE+1]; /* Miscellaneous buffer */
FileCache *fhead = 0; /* Head of File Cache Chain */ FileCache *fhead; /* Head of File Cache Chain */
FileCache *ftail = 0; /* Tail of File Cache Chain */ FileCache *ftail; /* Tail of File Cache Chain */
Rune *loc1; /* Start of pattern match */ Rune *loc1; /* Start of pattern match */
Rune *loc2; /* End of pattern match */ Rune *loc2; /* End of pattern match */
@ -142,21 +142,21 @@ int sflag; /* Set when substitution done */
int jflag; /* Set when jump required */ int jflag; /* Set when jump required */
int delflag; /* Delete current line when set */ int delflag; /* Delete current line when set */
long lnum = 0; /* Input line count */ long lnum; /* Input line count */
char fname[MAXFILES][40]; /* File name cache */ char fname[MAXFILES][40]; /* File name cache */
Biobuf *fcode[MAXFILES]; /* File ID cache */ Biobuf *fcode[MAXFILES]; /* File ID cache */
int nfiles = 0; /* Cache fill point */ int nfiles; /* Cache fill point */
Biobuf fout; /* Output stream */ Biobuf fout; /* Output stream */
Biobuf stdin; /* Default input */ Biobuf stdin; /* Default input */
Biobuf* f = 0; /* Input data */ Biobuf* f; /* Input data */
Label ltab[LABSIZE]; /* Label name symbol table */ Label ltab[LABSIZE]; /* Label name symbol table */
Label *labend = ltab+LABSIZE; /* End of label table */ Label *labend = ltab+LABSIZE; /* End of label table */
Label *lab = ltab+1; /* Current Fill point */ Label *lab = ltab+1; /* Current Fill point */
int depth = 0; /* {} stack pointer */ int depth; /* {} stack pointer */
Rune bad; /* Dummy err ptr reference */ Rune bad; /* Dummy err ptr reference */
Rune *badp = &bad; Rune *badp = &bad;
@ -211,7 +211,7 @@ main(int argc, char **argv)
compfl = 0; compfl = 0;
if(argc == 1) if(argc == 1)
exits(0); exits(nil);
ARGBEGIN{ ARGBEGIN{
case 'e': case 'e':
if (argc <= 1) if (argc <= 1)
@ -234,8 +234,7 @@ main(int argc, char **argv)
nflag++; nflag++;
continue; continue;
default: default:
fprint(2, "sed: Unknown flag: %c\n", ARGC()); quit("Unknown flag: %c", ARGC());
continue;
} ARGEND } ARGEND
if(compfl == 0) { if(compfl == 0) {
@ -253,12 +252,12 @@ main(int argc, char **argv)
dechain(); dechain();
if(argc <= 0) if(argc <= 0)
enroll(0); /* Add stdin to cache */ enroll(nil); /* Add stdin to cache */
else else
while(--argc >= 0) while(--argc >= 0)
enroll(*argv++); enroll(*argv++);
execute(); execute();
exits(0); exits(nil);
} }
void void
@ -518,7 +517,7 @@ jtcommon:
seof = *cp++; seof = *cp++;
if ((rep->re1 = compile()) == 0) { if ((rep->re1 = compile()) == 0) {
if(!lastre) if(!lastre)
quit("First RE may not be null."); quit("First RE may not be null");
rep->re1 = lastre; rep->re1 = lastre;
} }
rep->rhs = p; rep->rhs = p;
@ -1061,19 +1060,18 @@ dosub(Rune *rhsbuf)
continue; continue;
} }
else { else {
fprint(2, "sed: Invalid back reference \\%d\n",n); quit("Invalid back reference \\%d", n);
errexit();
} }
} }
*sp++ = c; *sp++ = c;
if (sp >= &genbuf[LBSIZE]) if (sp >= &genbuf[LBSIZE])
fprint(2, "sed: Output line too long.\n"); quit("Output line too long");
} }
lp = loc2; lp = loc2;
loc2 = sp - genbuf + linebuf; loc2 = sp - genbuf + linebuf;
while (*sp++ = *lp++) while (*sp++ = *lp++)
if (sp >= &genbuf[LBSIZE]) if (sp >= &genbuf[LBSIZE])
fprint(2, "sed: Output line too long.\n"); quit("Output line too long");
lp = linebuf; lp = linebuf;
sp = genbuf; sp = genbuf;
while (*lp++ = *sp++) while (*lp++ = *sp++)
@ -1087,7 +1085,7 @@ place(Rune *sp, Rune *l1, Rune *l2)
while (l1 < l2) { while (l1 < l2) {
*sp++ = *l1++; *sp++ = *l1++;
if (sp >= &genbuf[LBSIZE]) if (sp >= &genbuf[LBSIZE])
fprint(2, "sed: Output line too long.\n"); quit("Output line too long");
} }
return sp; return sp;
} }
@ -1128,8 +1126,7 @@ command(SedCom *ipc)
case ACOM: case ACOM:
*aptr++ = ipc; *aptr++ = ipc;
if(aptr >= abuf+MAXADDS) if(aptr >= abuf+MAXADDS)
quit("sed: Too many appends after line %ld\n", quit("Too many appends after line %ld", lnum);
(char *)lnum);
*aptr = 0; *aptr = 0;
break; break;
case CCOM: case CCOM:
@ -1260,12 +1257,11 @@ cpcom:
putline(&fout, linebuf, spend-linebuf); putline(&fout, linebuf, spend-linebuf);
if(aptr > abuf) if(aptr > abuf)
arout(); arout();
exits(0); exits(nil);
case RCOM: case RCOM:
*aptr++ = ipc; *aptr++ = ipc;
if(aptr >= &abuf[MAXADDS]) if(aptr >= &abuf[MAXADDS])
quit("sed: Too many reads after line %ld\n", quit("Too many reads after line %ld", lnum);
(char *)lnum);
*aptr = 0; *aptr = 0;
break; break;
case SCOM: case SCOM:
@ -1393,7 +1389,7 @@ gline(Rune *addr)
p = addr; p = addr;
for (c = (peekc? peekc: Bgetrune(f)); c >= 0; c = Bgetrune(f)) { for (c = (peekc? peekc: Bgetrune(f)); c >= 0; c = Bgetrune(f)) {
if (c == '\n') { if (c == '\n') {
if ((peekc = Bgetrune(f)) < 0 && fhead == 0) if ((peekc = Bgetrune(f)) < 0 && fhead == nil)
dolflag = 1; dolflag = 1;
*p = '\0'; *p = '\0';
return p; return p;
@ -1405,7 +1401,7 @@ gline(Rune *addr)
if(p != addr) { if(p != addr) {
*p = '\0'; *p = '\0';
peekc = -1; peekc = -1;
if (fhead == 0) if (fhead == nil)
dolflag = 1; dolflag = 1;
return p; return p;
} }