wpp: Handle remaining memory allocation failures.

oldstable
Matteo Bruni 2009-09-28 21:32:39 +02:00 committed by Alexandre Julliard
parent 239265fd8b
commit 9086c59d32
4 changed files with 84 additions and 20 deletions

View File

@ -23,12 +23,13 @@
#include <stdio.h>
extern void wpp_add_define( const char *name, const char *value );
/* Return value == 0 means successful execution */
extern int wpp_add_define( const char *name, const char *value );
extern void wpp_del_define( const char *name );
extern void wpp_add_cmdline_define( const char *value );
extern int 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 void wpp_add_include_path( const char *path );
extern int wpp_add_include_path( const char *path );
extern char *wpp_find_include( const char *name, const char *parent_name );
extern int wpp_parse( const char *input, FILE *output );
extern int wpp_parse_temp( const char *input, const char *output_base, char **output_name );

View File

@ -82,8 +82,8 @@ void *pp_xmalloc(size_t size)
res = malloc(size);
if(res == NULL)
{
fprintf(stderr, "Virtual memory exhausted.\n");
exit(2);
/* Set the error flag */
pp_status.state = 1;
}
return res;
}
@ -96,8 +96,8 @@ void *pp_xrealloc(void *p, size_t size)
res = realloc(p, size);
if(res == NULL)
{
fprintf(stderr, "Virtual memory exhausted.\n");
exit(2);
/* Set the error flag */
pp_status.state = 1;
}
return res;
}
@ -177,13 +177,16 @@ static void free_pp_entry( pp_entry_t *ppp, int idx )
}
/* push a new (empty) define state */
void pp_push_define_state(void)
int 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 */
@ -349,10 +352,12 @@ pp_entry_t *pp_add_macro(char *id, marg_t *args[], int nargs, mtext_t *exp)
static char **includepath;
static int nincludepath = 0;
void wpp_add_include_path(const char *path)
int wpp_add_include_path(const char *path)
{
char *tok;
char *cpy = pp_xstrdup(path);
if(!cpy)
return 1;
tok = strtok(cpy, INCLUDESEPARATOR);
while(tok)
@ -360,7 +365,14 @@ void 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 */
@ -372,13 +384,21 @@ void 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[nincludepath] = dir;
nincludepath++;
includepath = pp_xrealloc(includepath, nincludepath * sizeof(*includepath));
includepath[nincludepath-1] = dir;
}
tok = strtok(NULL, INCLUDESEPARATOR);
}
free(cpy);
return 0;
}
char *wpp_find_include(const char *name, const char *parent_name)
@ -390,6 +410,8 @@ char *wpp_find_include(const char *name, const char *parent_name)
int i, fd;
cpy = pp_xmalloc(strlen(name)+1);
if(!cpy)
return NULL;
cptr = cpy;
for(ccptr = name; *ccptr; ccptr++)
@ -415,6 +437,11 @@ char *wpp_find_include(const char *name, 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 );
@ -430,6 +457,11 @@ char *wpp_find_include(const char *name, const char *parent_name)
for(i = 0; i < nincludepath; i++)
{
path = pp_xmalloc(strlen(includepath[i]) + strlen(cpy) + 2);
if(!path)
{
free(cpy);
return NULL;
}
strcpy(path, includepath[i]);
strcat(path, "/");
strcat(path, cpy);
@ -630,11 +662,14 @@ 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);
}
}
end:
#endif
fprintf(stderr, "\n");
}

View File

@ -62,15 +62,17 @@ static void add_special_defines(void)
pp_add_define( pp_xstrdup("__TIME__"), pp_xstrdup(buf) );
ppp = pp_add_define( pp_xstrdup("__FILE__"), pp_xstrdup("") );
ppp->type = def_special;
if(ppp)
ppp->type = def_special;
ppp = pp_add_define( pp_xstrdup("__LINE__"), pp_xstrdup("") );
ppp->type = def_special;
if(ppp)
ppp->type = def_special;
}
/* add a define to the preprocessor list */
void wpp_add_define( const char *name, const char *value )
int wpp_add_define( const char *name, const char *value )
{
struct define *def;
@ -80,17 +82,35 @@ void 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 = pp_xstrdup(value);
return;
def->value = new_value;
return 0;
}
}
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;
}
@ -112,13 +132,17 @@ void wpp_del_define( const char *name )
/* add a command-line define of the form NAME=VALUE */
void wpp_add_cmdline_define( const char *value )
int wpp_add_cmdline_define( const char *value )
{
char *p;
char *str = pp_xstrdup(value);
char *p = strchr( str, '=' );
if(!str)
return 1;
p = strchr( str, '=' );
if (p) *p++ = 0;
wpp_add_define( str, p );
free( str );
return 0;
}
@ -146,7 +170,9 @@ int wpp_parse( const char *input, FILE *output )
pp_status.input = NULL;
pp_status.state = 0;
pp_push_define_state();
ret = pp_push_define_state();
if(ret)
return ret;
add_cmdline_defines();
add_special_defines();
@ -182,6 +208,8 @@ int wpp_parse_temp( const char *input, const char *output_base, char **output_na
if (!output_base || !output_base[0]) output_base = "wpptmp";
temp_name = pp_xmalloc( strlen(output_base) + 8 );
if(!temp_name)
return 1;
strcpy( temp_name, output_base );
strcat( temp_name, ".XXXXXX" );

View File

@ -202,7 +202,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);
void pp_push_define_state(void);
int pp_push_define_state(void);
void pp_pop_define_state(void);
pp_entry_t *pp_add_define(char *def, char *text);
pp_entry_t *pp_add_macro(char *ident, marg_t *args[], int nargs, mtext_t *exp);