plumber: fail on buffer exhaustion or runaway quotes in string expansion

This commit is contained in:
Igor Burago 2022-08-16 20:25:27 +08:00 committed by Dan Cross
parent a4af25bc0d
commit e4b913866b

View file

@ -289,22 +289,35 @@ dollar(Exec *e, char *s, int *namelen)
return variable(s, n); return variable(s, n);
} }
static void
ruleerror(char *msg)
{
if(parsing){
parsing = 0;
parseerror("%s", msg);
}
error("%s", msg);
}
/* expand one blank-terminated string, processing quotes and $ signs */ /* expand one blank-terminated string, processing quotes and $ signs */
char* char*
expand(Exec *e, char *s, char **ends) expand(Exec *e, char *s, char **ends)
{ {
char *p, *ep, *val; char *p, *ep, *val;
int namelen, quoting; int namelen, vallen, quoting, inputleft;
p = ebuf; p = ebuf;
ep = ebuf+sizeof ebuf-1; ep = ebuf+sizeof ebuf-1;
quoting = 0; quoting = 0;
while(p<ep && *s!='\0' && (quoting || (*s!=' ' && *s!='\t'))){ for(;;){
inputleft = (*s!='\0' && (quoting || (*s!=' ' && *s!='\t')));
if(!inputleft || p==ep)
break;
if(*s == '\''){ if(*s == '\''){
s++; s++;
if(!quoting) if(!quoting)
quoting = 1; quoting = 1;
else if(*s == '\''){ else if(*s == '\''){
*p++ = '\''; *p++ = '\'';
s++; s++;
}else }else
@ -321,12 +334,17 @@ expand(Exec *e, char *s, char **ends)
*p++ = '$'; *p++ = '$';
continue; continue;
} }
if(ep-p < strlen(val)) vallen = strlen(val);
return "string-too-long"; if(ep-p < vallen)
break;
strcpy(p, val); strcpy(p, val);
p += strlen(val); p += vallen;
s += namelen; s += namelen;
} }
if(inputleft)
ruleerror("expanded string too long");
else if(quoting)
ruleerror("runaway quoted string literal");
if(ends) if(ends)
*ends = s; *ends = s;
*p = '\0'; *p = '\0';
@ -336,11 +354,7 @@ expand(Exec *e, char *s, char **ends)
void void
regerror(char *msg) regerror(char *msg)
{ {
if(parsing){ ruleerror(msg);
parsing = 0;
parseerror("%s", msg);
}
error("%s", msg);
} }
void void