Fixed numerous bugs in preprocessor (string literals being parsed, mismatched args being wiped)

This commit is contained in:
David Anderson 2005-09-10 05:04:23 +00:00
parent 52b1d67ca2
commit 52cc204787

View File

@ -1407,7 +1407,7 @@ static int substpattern(unsigned char *line,size_t buffersize,char *pattern,char
int prefixlen; int prefixlen;
const unsigned char *p,*s,*e; const unsigned char *p,*s,*e;
unsigned char *args[10]; unsigned char *args[10];
int match,arg,len; int match,arg,len,argsnum=0;
memset(args,0,sizeof args); memset(args,0,sizeof args);
@ -1447,6 +1447,8 @@ static int substpattern(unsigned char *line,size_t buffersize,char *pattern,char
/* store the parameter (overrule any earlier) */ /* store the parameter (overrule any earlier) */
if (args[arg]!=NULL) if (args[arg]!=NULL)
free(args[arg]); free(args[arg]);
else
argsnum++;
len=(int)(e-s); len=(int)(e-s);
args[arg]=(unsigned char*)malloc(len+1); args[arg]=(unsigned char*)malloc(len+1);
if (args[arg]==NULL) if (args[arg]==NULL)
@ -1503,12 +1505,15 @@ static int substpattern(unsigned char *line,size_t buffersize,char *pattern,char
if (match) { if (match) {
/* calculate the length of the substituted string */ /* calculate the length of the substituted string */
for (e=(unsigned char*)substitution,len=0; *e!='\0'; e++) { for (e=(unsigned char*)substitution,len=0; *e!='\0'; e++) {
if (*e=='%' && isdigit(*(e+1))) { if (*e=='%' && isdigit(*(e+1)) && argsnum) {
arg=*(e+1)-'0'; arg=*(e+1)-'0';
assert(arg>=0 && arg<=9); assert(arg>=0 && arg<=9);
if (args[arg]!=NULL) if (args[arg]!=NULL) {
len+=strlen((char*)args[arg]); len+=strlen((char*)args[arg]);
e++; /* skip %, digit is skipped later */ e++; /* skip %, digit is skipped later */
} else {
len++;
}
} else { } else {
len++; len++;
} /* if */ } /* if */
@ -1526,8 +1531,21 @@ static int substpattern(unsigned char *line,size_t buffersize,char *pattern,char
if (args[arg]!=NULL) { if (args[arg]!=NULL) {
strins((char*)s,(char*)args[arg],strlen((char*)args[arg])); strins((char*)s,(char*)args[arg],strlen((char*)args[arg]));
s+=strlen((char*)args[arg]); s+=strlen((char*)args[arg]);
} /* if */ e++; /* skip %, digit is skipped later */
e++; /* skip %, digit is skipped later */ } else {
strins((char*)s,(char*)e,1);
s++;
} /* if */
} else if (*e=='"') {
p=e;
if (is_startstring(e)) { /* skip strings */
e=skipstring(e);
strins((char*)s,(char*)p,(e-p+1));
s+=(e-p+1);
} else {
strins((char*)s,(char*)e,1);
s++;
}
} else { } else {
strins((char*)s,(char*)e,1); strins((char*)s,(char*)e,1);
s++; s++;