1
0
mirror of https://github.com/wine-mirror/wine synced 2024-07-08 20:06:18 +00:00

wpp: Don't try to recover from memory allocation errors.

wpp is only used in short-lived tools.

Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Alexandre Julliard 2020-12-03 13:23:45 +01:00
parent d3a4477bad
commit de2f4ceeb8
6 changed files with 50 additions and 203 deletions

View File

@ -24,14 +24,14 @@
#include <stdio.h>
#include <stdarg.h>
/* Return value == 0 means successful execution */
extern int wpp_add_define( const char *name, const char *value );
extern void wpp_add_define( const char *name, const char *value );
extern void wpp_del_define( const char *name );
extern int wpp_add_cmdline_define( const char *value );
extern void wpp_add_cmdline_define( const char *value );
extern void wpp_set_debug( int lex_debug, int parser_debug, int msg_debug );
extern void wpp_set_pedantic( int on );
extern int wpp_add_include_path( const char *path );
extern void wpp_add_include_path( const char *path );
extern char *wpp_find_include( const char *name, const char *parent_name );
/* Return value == 0 means successful execution */
extern int wpp_parse( const char *input, FILE *output );
#endif /* __WINE_WPP_H */

View File

@ -322,8 +322,6 @@ void pp_writestring(const char *format, ...)
if(buffercapacity == 0)
{
buffer = pp_xmalloc(BUFFERINITIALCAPACITY);
if(buffer == NULL)
return;
buffercapacity = BUFFERINITIALCAPACITY;
}
@ -498,7 +496,7 @@ void pp_writestring(const char *format, ...)
/*
* Handle left side of #define
*/
<pp_def>{cident}\( ppy_lval.cptr = pp_xstrdup(ppy_text); if(ppy_lval.cptr) ppy_lval.cptr[ppy_leng-1] = '\0'; yy_pp_state(pp_macro); return tMACRO;
<pp_def>{cident}\( ppy_lval.cptr = pp_xstrdup(ppy_text); ppy_lval.cptr[ppy_leng-1] = '\0'; yy_pp_state(pp_macro); return tMACRO;
<pp_def>{cident} ppy_lval.cptr = pp_xstrdup(ppy_text); yy_pp_state(pp_define); return tDEFINE;
<pp_def>{ws}+ ;
<pp_def>\\\r?\n newline(0);
@ -1258,8 +1256,7 @@ static void add_string(const char *str, int len)
static char *get_string(void)
{
char *str = pp_xmalloc(strbuf_idx + 1);
if(!str)
return NULL;
memcpy(str, strbuffer, strbuf_idx);
str[strbuf_idx] = '\0';
#ifdef DEBUG
@ -1350,20 +1347,17 @@ static bufferstackentry_t *pop_buffer(void)
if(ppp)
{
iep = pp_xmalloc(sizeof(includelogicentry_t));
if (iep)
{
iep->ppp = ppp;
ppp->iep = iep;
iep->filename = bufferstack[bufferstackidx].include_filename;
iep->prev = NULL;
iep->next = pp_includelogiclist;
if(iep->next)
iep->next->prev = iep;
pp_includelogiclist = iep;
if(pp_status.debug)
fprintf(stderr, "pop_buffer: %s:%d: includelogic added, include_ppp='%s', file='%s'\n",
bufferstack[bufferstackidx].filename, bufferstack[bufferstackidx].line_number, pp_incl_state.ppp, iep->filename);
}
iep->ppp = ppp;
ppp->iep = iep;
iep->filename = bufferstack[bufferstackidx].include_filename;
iep->prev = NULL;
iep->next = pp_includelogiclist;
if(iep->next)
iep->next->prev = iep;
pp_includelogiclist = iep;
if(pp_status.debug)
fprintf(stderr, "pop_buffer: %s:%d: includelogic added, include_ppp='%s', file='%s'\n",
bufferstack[bufferstackidx].filename, bufferstack[bufferstackidx].line_number, pp_incl_state.ppp, iep->filename);
}
}
free(pp_incl_state.ppp);
@ -1423,8 +1417,6 @@ static void push_macro(pp_entry_t *ppp)
}
macexpstack[macexpstackidx] = pp_xmalloc(sizeof(macexpstack[0][0]));
if(!macexpstack[macexpstackidx])
return;
memset( macexpstack[macexpstackidx], 0, sizeof(macexpstack[0][0]));
macexpstack[macexpstackidx]->ppp = ppp;
macexpstackidx++;
@ -1479,30 +1471,15 @@ static void macro_add_arg(int last)
{
int nnl = 0;
char *cptr;
char **new_args, **new_ppargs;
int *new_nnls;
macexpstackentry_t *mep = top_macro();
assert(mep->ppp->expanding == 0);
new_args = pp_xrealloc(mep->args, (mep->nargs+1) * sizeof(mep->args[0]));
if(!new_args)
return;
mep->args = new_args;
new_ppargs = pp_xrealloc(mep->ppargs, (mep->nargs+1) * sizeof(mep->ppargs[0]));
if(!new_ppargs)
return;
mep->ppargs = new_ppargs;
new_nnls = pp_xrealloc(mep->nnls, (mep->nargs+1) * sizeof(mep->nnls[0]));
if(!new_nnls)
return;
mep->nnls = new_nnls;
mep->args = pp_xrealloc(mep->args, (mep->nargs+1) * sizeof(mep->args[0]));
mep->ppargs = pp_xrealloc(mep->ppargs, (mep->nargs+1) * sizeof(mep->ppargs[0]));
mep->nnls = pp_xrealloc(mep->nnls, (mep->nargs+1) * sizeof(mep->nnls[0]));
mep->args[mep->nargs] = pp_xstrdup(mep->curarg ? mep->curarg : "");
if(!mep->args[mep->nargs])
return;
cptr = mep->args[mep->nargs]-1;
while((cptr = strchr(cptr+1, '\n')))
{

View File

@ -295,17 +295,11 @@ preprocessor
| tPRAGMA opt_text tNL { pp_writestring("#pragma %s\n", $2 ? $2 : ""); free($2); }
| tPPIDENT opt_text tNL { if(pp_status.pedantic) ppy_warning("#ident ignored (arg: '%s')", $2); free($2); }
| tRCINCLUDE tRCINCLUDEPATH {
if($2)
{
int nl=strlen($2) +3;
char *fn=pp_xmalloc(nl);
if(fn)
{
sprintf(fn,"\"%s\"",$2);
pp_do_include(fn,1);
}
free($2);
}
int nl=strlen($2) +3;
char *fn=pp_xmalloc(nl);
sprintf(fn,"\"%s\"",$2);
pp_do_include(fn,1);
free($2);
}
| tRCINCLUDE tDQSTRING {
pp_do_include($2,1);
@ -552,8 +546,7 @@ static int boolean(cval_t *v)
static marg_t *new_marg(char *str, def_arg_t type)
{
marg_t *ma = pp_xmalloc(sizeof(marg_t));
if(!ma)
return NULL;
ma->arg = str;
ma->type = type;
ma->nnl = 0;
@ -562,17 +555,11 @@ static marg_t *new_marg(char *str, def_arg_t type)
static marg_t *add_new_marg(char *str, def_arg_t type)
{
marg_t **new_macro_args;
marg_t *ma;
if(!str)
return NULL;
new_macro_args = pp_xrealloc(macro_args, (nmacro_args+1) * sizeof(macro_args[0]));
if(!new_macro_args)
return NULL;
macro_args = new_macro_args;
macro_args = pp_xrealloc(macro_args, (nmacro_args+1) * sizeof(macro_args[0]));
ma = new_marg(str, type);
if(!ma)
return NULL;
macro_args[nmacro_args] = ma;
nmacro_args++;
return ma;
@ -594,8 +581,7 @@ static int marg_index(char *id)
static mtext_t *new_mtext(char *str, int idx, def_exp_t type)
{
mtext_t *mt = pp_xmalloc(sizeof(mtext_t));
if(!mt)
return NULL;
if(str == NULL)
mt->subst.argidx = idx;
else
@ -615,11 +601,7 @@ static mtext_t *combine_mtext(mtext_t *tail, mtext_t *mtp)
if(tail->type == exp_text && mtp->type == exp_text)
{
char *new_text;
new_text = pp_xrealloc(tail->subst.text, strlen(tail->subst.text)+strlen(mtp->subst.text)+1);
if(!new_text)
return mtp;
tail->subst.text = new_text;
tail->subst.text = pp_xrealloc(tail->subst.text, strlen(tail->subst.text)+strlen(mtp->subst.text)+1);
strcat(tail->subst.text, mtp->subst.text);
free(mtp->subst.text);
free(mtp);
@ -683,22 +665,9 @@ static mtext_t *combine_mtext(mtext_t *tail, mtext_t *mtp)
static char *merge_text(char *s1, char *s2)
{
int l1;
int l2;
char *snew;
if(!s1)
return s2;
if(!s2)
return s1;
l1 = strlen(s1);
l2 = strlen(s2);
snew = pp_xrealloc(s1, l1+l2+1);
if(!snew)
{
free(s2);
return s1;
}
s1 = snew;
int l1 = strlen(s1);
int l2 = strlen(s2);
s1 = pp_xrealloc(s1, l1+l2+1);
memcpy(s1+l1, s2, l2+1);
free(s2);
return s1;

View File

@ -79,8 +79,8 @@ void *pp_xmalloc(size_t size)
res = malloc(size);
if(res == NULL)
{
/* Set the error flag */
pp_status.state = 1;
fprintf( stderr, "Virtual memory exhausted\n" );
exit(1);
}
return res;
}
@ -93,23 +93,16 @@ void *pp_xrealloc(void *p, size_t size)
res = realloc(p, size);
if(res == NULL)
{
/* Set the error flag */
pp_status.state = 1;
fprintf( stderr, "Virtual memory exhausted\n" );
exit(1);
}
return res;
}
char *pp_xstrdup(const char *str)
{
char *s;
int len;
assert(str != NULL);
len = strlen(str)+1;
s = pp_xmalloc(len);
if(!s)
return NULL;
return memcpy(s, str, len);
int len = strlen(str)+1;
return memcpy(pp_xmalloc(len), str, len);
}
char *wpp_lookup(const char *name, int type, const char *parent_name,
@ -122,8 +115,6 @@ char *wpp_lookup(const char *name, int type, const char *parent_name,
int i, fd;
cpy = pp_xmalloc(strlen(name)+1);
if(!cpy)
return NULL;
cptr = cpy;
for(ccptr = name; *ccptr; ccptr++)
@ -149,11 +140,6 @@ char *wpp_lookup(const char *name, int type, const char *parent_name,
if ((p = strrchr( parent_name, '/' ))) p++;
else p = parent_name;
path = pp_xmalloc( (p - parent_name) + strlen(cpy) + 1 );
if(!path)
{
free(cpy);
return NULL;
}
memcpy( path, parent_name, p - parent_name );
strcpy( path + (p - parent_name), cpy );
fd = open( path, O_RDONLY );
@ -169,11 +155,6 @@ char *wpp_lookup(const char *name, int type, const char *parent_name,
for(i = 0; i < include_path_count; i++)
{
path = pp_xmalloc(strlen(include_path[i]) + strlen(cpy) + 2);
if(!path)
{
free(cpy);
return NULL;
}
strcpy(path, include_path[i]);
strcat(path, "/");
strcat(path, cpy);
@ -252,16 +233,13 @@ static void free_pp_entry( pp_entry_t *ppp, int idx )
}
/* push a new (empty) define state */
int pp_push_define_state(void)
void pp_push_define_state(void)
{
pp_def_state_t *state = pp_xmalloc( sizeof(*state) );
if(!state)
return 1;
memset( state->defines, 0, sizeof(state->defines) );
state->next = pp_def_state;
pp_def_state = state;
return 0;
}
/* pop the current define state */
@ -308,8 +286,6 @@ pp_entry_t *pp_add_define(const char *def, const char *text)
int idx;
pp_entry_t *ppp;
if(!def)
return NULL;
idx = pphash(def);
if((ppp = pplookup(def)) != NULL)
{
@ -318,19 +294,11 @@ pp_entry_t *pp_add_define(const char *def, const char *text)
pp_del_define(def);
}
ppp = pp_xmalloc(sizeof(pp_entry_t));
if(!ppp)
return NULL;
memset( ppp, 0, sizeof(*ppp) );
ppp->ident = pp_xstrdup(def);
if(!ppp->ident)
goto error;
ppp->type = def_define;
ppp->subst.text = text ? pp_xstrdup(text) : NULL;
if(text && !ppp->subst.text)
goto error;
ppp->filename = pp_xstrdup(pp_status.input ? pp_status.input : "<internal or cmdline>");
if(!ppp->filename)
goto error;
ppp->linenumber = pp_status.input ? pp_status.line_number : 0;
ppp->next = pp_def_state->defines[idx];
pp_def_state->defines[idx] = ppp;
@ -354,12 +322,6 @@ pp_entry_t *pp_add_define(const char *def, const char *text)
printf("Added define (%s, %d) <%s> to <%s>\n", pp_status.input, pp_status.line_number, ppp->ident, ppp->subst.text ? ppp->subst.text : "(null)");
return ppp;
error:
free(ppp->ident);
free(ppp->subst.text);
free(ppp);
return NULL;
}
pp_entry_t *pp_add_macro(char *id, marg_t *args[], int nargs, mtext_t *exp)
@ -367,8 +329,6 @@ pp_entry_t *pp_add_macro(char *id, marg_t *args[], int nargs, mtext_t *exp)
int idx;
pp_entry_t *ppp;
if(!id)
return NULL;
idx = pphash(id);
if((ppp = pplookup(id)) != NULL)
{
@ -377,8 +337,6 @@ pp_entry_t *pp_add_macro(char *id, marg_t *args[], int nargs, mtext_t *exp)
pp_del_define(id);
}
ppp = pp_xmalloc(sizeof(pp_entry_t));
if(!ppp)
return NULL;
memset( ppp, 0, sizeof(*ppp) );
ppp->ident = id;
ppp->type = def_macro;
@ -386,11 +344,6 @@ pp_entry_t *pp_add_macro(char *id, marg_t *args[], int nargs, mtext_t *exp)
ppp->nargs = nargs;
ppp->subst.mtext= exp;
ppp->filename = pp_xstrdup(pp_status.input ? pp_status.input : "<internal or cmdline>");
if(!ppp->filename)
{
free(ppp);
return NULL;
}
ppp->linenumber = pp_status.input ? pp_status.line_number : 0;
ppp->next = pp_def_state->defines[idx];
pp_def_state->defines[idx] = ppp;
@ -438,12 +391,10 @@ pp_entry_t *pp_add_macro(char *id, marg_t *args[], int nargs, mtext_t *exp)
static char **includepath;
static int nincludepath = 0;
int wpp_add_include_path(const char *path)
void wpp_add_include_path(const char *path)
{
char *tok;
char *cpy = pp_xstrdup(path);
if(!cpy)
return 1;
tok = strtok(cpy, INCLUDESEPARATOR);
while(tok)
@ -451,14 +402,8 @@ int wpp_add_include_path(const char *path)
if(*tok) {
char *dir;
char *cptr;
char **new_path;
dir = pp_xstrdup(tok);
if(!dir)
{
free(cpy);
return 1;
}
for(cptr = dir; *cptr; cptr++)
{
/* Convert to forward slash */
@ -470,21 +415,13 @@ int wpp_add_include_path(const char *path)
*cptr = '\0';
/* Add to list */
new_path = pp_xrealloc(includepath, (nincludepath+1) * sizeof(*includepath));
if(!new_path)
{
free(dir);
free(cpy);
return 1;
}
includepath = new_path;
includepath = pp_xrealloc(includepath, (nincludepath+1) * sizeof(*includepath));
includepath[nincludepath] = dir;
nincludepath++;
}
tok = strtok(NULL, INCLUDESEPARATOR);
}
free(cpy);
return 0;
}
char *wpp_find_include(const char *name, const char *parent_name)
@ -676,8 +613,6 @@ static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
if(n)
{
cpy = pp_xstrdup(n);
if(!cpy)
goto end;
for (p = cpy; *p; p++) if(!isprint(*p)) *p = ' ';
fprintf(stderr, " near '%s'", cpy);
free(cpy);

View File

@ -72,12 +72,10 @@ static void add_special_defines(void)
pp_add_define( "__TIME__", buf );
ppp = pp_add_define( "__FILE__", "" );
if(ppp)
ppp->type = def_special;
ppp->type = def_special;
ppp = pp_add_define( "__LINE__", "" );
if(ppp)
ppp->type = def_special;
ppp->type = def_special;
}
static void del_special_defines(void)
@ -90,7 +88,7 @@ static void del_special_defines(void)
/* add a define to the preprocessor list */
int wpp_add_define( const char *name, const char *value )
void wpp_add_define( const char *name, const char *value )
{
struct define *def;
@ -100,35 +98,17 @@ int wpp_add_define( const char *name, const char *value )
{
if (!strcmp( def->name, name ))
{
char *new_value = pp_xstrdup(value);
if(!new_value)
return 1;
free( def->value );
def->value = new_value;
return 0;
def->value = pp_xstrdup(value);
return;
}
}
def = pp_xmalloc( sizeof(*def) );
if(!def)
return 1;
def->next = cmdline_defines;
def->name = pp_xstrdup(name);
if(!def->name)
{
free(def);
return 1;
}
def->value = pp_xstrdup(value);
if(!def->value)
{
free(def->name);
free(def);
return 1;
}
cmdline_defines = def;
return 0;
}
@ -150,17 +130,15 @@ void wpp_del_define( const char *name )
/* add a command-line define of the form NAME=VALUE */
int wpp_add_cmdline_define( const char *value )
void wpp_add_cmdline_define( const char *value )
{
char *p;
char *str = pp_xstrdup(value);
if(!str)
return 1;
p = strchr( str, '=' );
if (p) *p++ = 0;
wpp_add_define( str, p );
free( str );
return 0;
}
@ -188,23 +166,14 @@ int wpp_parse( const char *input, FILE *output )
pp_status.input = NULL;
pp_status.line_number = 1;
pp_status.char_number = 1;
pp_status.state = 0;
ret = pp_push_define_state();
if(ret)
return ret;
pp_push_define_state();
add_cmdline_defines();
add_special_defines();
if (!input) pp_status.file = stdin;
else if (!(pp_status.file = fopen(input, "rt")))
{
ppy_error("Could not open %s\n", input);
del_special_defines();
del_cmdline_defines();
pp_pop_define_state();
return 2;
}
pp_status.input = input ? pp_xstrdup(input) : NULL;
@ -212,8 +181,6 @@ int wpp_parse( const char *input, FILE *output )
pp_writestring("# 1 \"%s\" 1\n", input ? input : "");
ret = ppy_parse();
/* If there were errors during processing, return an error code */
if (!ret && pp_status.state) ret = pp_status.state;
if (input)
{

View File

@ -179,7 +179,7 @@ void *pp_xmalloc(size_t);
void *pp_xrealloc(void *, size_t);
char *pp_xstrdup(const char *str);
pp_entry_t *pplookup(const char *ident);
int pp_push_define_state(void);
void pp_push_define_state(void);
void pp_pop_define_state(void);
pp_entry_t *pp_add_define(const char *def, const char *text);
pp_entry_t *pp_add_macro(char *ident, marg_t *args[], int nargs, mtext_t *exp);
@ -211,7 +211,6 @@ struct pp_status
void *file; /* current input file descriptor */
int line_number; /* current line number */
int char_number; /* current char number in line */
int state; /* current error state */
int pedantic; /* pedantic option */
int debug; /* debug messages flag */
};