wpp: Handle remaining memory allocation failures.

This commit is contained in:
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> #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_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_debug( int lex_debug, int parser_debug, int msg_debug );
extern void wpp_set_pedantic( int on ); 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 char *wpp_find_include( const char *name, const char *parent_name );
extern int wpp_parse( const char *input, FILE *output ); extern int wpp_parse( const char *input, FILE *output );
extern int wpp_parse_temp( const char *input, const char *output_base, char **output_name ); 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); res = malloc(size);
if(res == NULL) if(res == NULL)
{ {
fprintf(stderr, "Virtual memory exhausted.\n"); /* Set the error flag */
exit(2); pp_status.state = 1;
} }
return res; return res;
} }
@ -96,8 +96,8 @@ void *pp_xrealloc(void *p, size_t size)
res = realloc(p, size); res = realloc(p, size);
if(res == NULL) if(res == NULL)
{ {
fprintf(stderr, "Virtual memory exhausted.\n"); /* Set the error flag */
exit(2); pp_status.state = 1;
} }
return res; return res;
} }
@ -177,13 +177,16 @@ static void free_pp_entry( pp_entry_t *ppp, int idx )
} }
/* push a new (empty) define state */ /* 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) ); pp_def_state_t *state = pp_xmalloc( sizeof(*state) );
if(!state)
return 1;
memset( state->defines, 0, sizeof(state->defines) ); memset( state->defines, 0, sizeof(state->defines) );
state->next = pp_def_state; state->next = pp_def_state;
pp_def_state = state; pp_def_state = state;
return 0;
} }
/* pop the current define state */ /* 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 char **includepath;
static int nincludepath = 0; static int nincludepath = 0;
void wpp_add_include_path(const char *path) int wpp_add_include_path(const char *path)
{ {
char *tok; char *tok;
char *cpy = pp_xstrdup(path); char *cpy = pp_xstrdup(path);
if(!cpy)
return 1;
tok = strtok(cpy, INCLUDESEPARATOR); tok = strtok(cpy, INCLUDESEPARATOR);
while(tok) while(tok)
@ -360,7 +365,14 @@ void wpp_add_include_path(const char *path)
if(*tok) { if(*tok) {
char *dir; char *dir;
char *cptr; char *cptr;
char **new_path;
dir = pp_xstrdup(tok); dir = pp_xstrdup(tok);
if(!dir)
{
free(cpy);
return 1;
}
for(cptr = dir; *cptr; cptr++) for(cptr = dir; *cptr; cptr++)
{ {
/* Convert to forward slash */ /* Convert to forward slash */
@ -372,13 +384,21 @@ void wpp_add_include_path(const char *path)
*cptr = '\0'; *cptr = '\0';
/* Add to list */ /* 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++; nincludepath++;
includepath = pp_xrealloc(includepath, nincludepath * sizeof(*includepath));
includepath[nincludepath-1] = dir;
} }
tok = strtok(NULL, INCLUDESEPARATOR); tok = strtok(NULL, INCLUDESEPARATOR);
} }
free(cpy); free(cpy);
return 0;
} }
char *wpp_find_include(const char *name, const char *parent_name) 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; int i, fd;
cpy = pp_xmalloc(strlen(name)+1); cpy = pp_xmalloc(strlen(name)+1);
if(!cpy)
return NULL;
cptr = cpy; cptr = cpy;
for(ccptr = name; *ccptr; ccptr++) 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++; if ((p = strrchr( parent_name, '/' ))) p++;
else p = parent_name; else p = parent_name;
path = pp_xmalloc( (p - parent_name) + strlen(cpy) + 1 ); path = pp_xmalloc( (p - parent_name) + strlen(cpy) + 1 );
if(!path)
{
free(cpy);
return NULL;
}
memcpy( path, parent_name, p - parent_name ); memcpy( path, parent_name, p - parent_name );
strcpy( path + (p - parent_name), cpy ); strcpy( path + (p - parent_name), cpy );
fd = open( path, O_RDONLY ); 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++) for(i = 0; i < nincludepath; i++)
{ {
path = pp_xmalloc(strlen(includepath[i]) + strlen(cpy) + 2); path = pp_xmalloc(strlen(includepath[i]) + strlen(cpy) + 2);
if(!path)
{
free(cpy);
return NULL;
}
strcpy(path, includepath[i]); strcpy(path, includepath[i]);
strcat(path, "/"); strcat(path, "/");
strcat(path, cpy); 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) if(n)
{ {
cpy = pp_xstrdup(n); cpy = pp_xstrdup(n);
if(!cpy)
goto end;
for (p = cpy; *p; p++) if(!isprint(*p)) *p = ' '; for (p = cpy; *p; p++) if(!isprint(*p)) *p = ' ';
fprintf(stderr, " near '%s'", cpy); fprintf(stderr, " near '%s'", cpy);
free(cpy); free(cpy);
} }
} }
end:
#endif #endif
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }

View File

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

View File

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