[5/9] wpp: Handle remaining memory allocation failures [try 2]

Matteo Bruni matteo.mystral at gmail.com
Thu Oct 1 07:21:07 CDT 2009


-------------- next part --------------
From 5ace7982238248e0dbe6b54ce8faa28816b229f9 Mon Sep 17 00:00:00 2001
From: Matteo Bruni <matteo.mystral at gmail.com>
Date: Mon, 28 Sep 2009 21:32:39 +0200
Subject: wpp: Handle remaining memory allocation failures

---
 libs/wpp/preproc.c     |   43 ++++++++++++++++++++++++++++++++++++-------
 libs/wpp/wpp.c         |   36 +++++++++++++++++++++++++++++++-----
 libs/wpp/wpp_private.h |    2 +-
 3 files changed, 68 insertions(+), 13 deletions(-)

diff --git a/libs/wpp/preproc.c b/libs/wpp/preproc.c
index 1d9aed6..0293bdf 100644
--- a/libs/wpp/preproc.c
+++ b/libs/wpp/preproc.c
@@ -82,8 +82,7 @@ void *pp_xmalloc(size_t size)
     res = malloc(size);
     if(res == NULL)
     {
-        fprintf(stderr, "Virtual memory exhausted.\n");
-        exit(2);
+        pp_internal_error(__FILE__, __LINE__, "Virtual memory exhausted.\n");
     }
     return res;
 }
@@ -96,8 +95,7 @@ void *pp_xrealloc(void *p, size_t size)
     res = realloc(p, size);
     if(res == NULL)
     {
-        fprintf(stderr, "Virtual memory exhausted.\n");
-        exit(2);
+        pp_internal_error(__FILE__, __LINE__, "Virtual memory exhausted.\n");
     }
     return res;
 }
@@ -177,13 +175,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 */
@@ -353,6 +354,8 @@ void wpp_add_include_path(const char *path)
 {
 	char *tok;
 	char *cpy = pp_xstrdup(path);
+	if(!cpy)
+		return;
 
 	tok = strtok(cpy, INCLUDESEPARATOR);
 	while(tok)
@@ -360,7 +363,11 @@ void wpp_add_include_path(const char *path)
 		if(*tok) {
 			char *dir;
 			char *cptr;
+			char **new_path;
+
 			dir = pp_xstrdup(tok);
+			if(!dir)
+				goto cleanup;
 			for(cptr = dir; *cptr; cptr++)
 			{
 				/* Convert to forward slash */
@@ -372,12 +379,19 @@ 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);
+				goto cleanup;
+			}
+			includepath = new_path;
+			includepath[nincludepath] = dir;
 			nincludepath++;
-			includepath = pp_xrealloc(includepath, nincludepath * sizeof(*includepath));
-			includepath[nincludepath-1] = dir;
 		}
 		tok = strtok(NULL, INCLUDESEPARATOR);
 	}
+cleanup:
 	free(cpy);
 }
 
@@ -390,6 +404,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 +431,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 +451,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 +656,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");
 }
diff --git a/libs/wpp/wpp.c b/libs/wpp/wpp.c
index a7597ec..faae893 100644
--- a/libs/wpp/wpp.c
+++ b/libs/wpp/wpp.c
@@ -62,10 +62,12 @@ 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;
 }
 
 
@@ -80,16 +82,33 @@ 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;
             free( def->value );
-            def->value = pp_xstrdup(value);
+            def->value = new_value;
+
             return;
         }
     }
 
     def = pp_xmalloc( sizeof(*def) );
+    if(!def)
+        return;
     def->next  = cmdline_defines;
     def->name  = pp_xstrdup(name);
+    if(!def->name)
+    {
+        free(def);
+        return;
+    }
     def->value = pp_xstrdup(value);
+    if(!def->value)
+    {
+        free(def->name);
+        free(def);
+        return;
+    }
     cmdline_defines = def;
 }
 
@@ -114,8 +133,11 @@ 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 )
 {
+    char *p;
     char *str = pp_xstrdup(value);
-    char *p = strchr( str, '=' );
+    if(!str)
+        return;
+    p = strchr( str, '=' );
     if (p) *p++ = 0;
     wpp_add_define( str, p );
     free( str );
@@ -146,7 +168,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 +206,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" );
 
diff --git a/libs/wpp/wpp_private.h b/libs/wpp/wpp_private.h
index b485d70..c0b95a4 100644
--- a/libs/wpp/wpp_private.h
+++ b/libs/wpp/wpp_private.h
@@ -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);
-- 
1.6.3.3


More information about the wine-patches mailing list