[3/6] wpp: Manage malloc, realloc and strdup failures

Matteo Bruni matteo.mystral at gmail.com
Thu Sep 10 10:40:50 CDT 2009


-------------- next part --------------
From c2db28a23f4a4d2f6a30d6fee1dc319a6d5f2b75 Mon Sep 17 00:00:00 2001
From: Matteo Bruni <matteo.mystral at gmail.com>
Date: Fri, 4 Sep 2009 14:08:19 +0200
Subject: wpp: Manage malloc, realloc and strdup failures

---
 libs/wpp/ppl.l         |   98 ++++++++++++++++++++++++++++++++++++++----------
 libs/wpp/ppy.y         |   64 +++++++++++++++++++++++++------
 libs/wpp/preproc.c     |   74 +++++++++++++++++++++++++++++++-----
 libs/wpp/wpp.c         |   36 +++++++++++++++--
 libs/wpp/wpp_private.h |    2 +-
 5 files changed, 225 insertions(+), 49 deletions(-)

diff --git a/libs/wpp/ppl.l b/libs/wpp/ppl.l
index 52f0f04..f0a39a8 100644
--- a/libs/wpp/ppl.l
+++ b/libs/wpp/ppl.l
@@ -317,10 +317,13 @@ void writestring( const char *format, ... )
 	int len;
 	static char *buffer;
 	static int buffercapacity;
+	char *new_buffer;
 
 	if(buffercapacity == 0)
 	{
 		buffer = pp_xmalloc(BUFFERINITIALCAPACITY);
+		if(buffer == NULL)
+			return;
 		buffercapacity = BUFFERINITIALCAPACITY;
 	}
 
@@ -336,7 +339,13 @@ void writestring( const char *format, ... )
 			buffercapacity *= 2;
 		} while(len > buffercapacity);
 
-		buffer = pp_xrealloc(buffer, buffercapacity);
+		new_buffer = pp_xrealloc(buffer, buffercapacity);
+		if(new_buffer == NULL)
+		{
+			va_end(valist);
+			return;
+		}
+		buffer = new_buffer;
 		len = vsnprintf(buffer, buffercapacity,
 				format, valist);
 	}
@@ -484,7 +493,7 @@ void writestring( const char *format, ... )
 	/*
 	 * Handle left side of #define
 	 */
-<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); 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); yy_pp_state(pp_define); return tDEFINE;
 <pp_def>{ws}+			;
 <pp_def>\\\r?\n			newline(0);
@@ -958,19 +967,26 @@ static void expand_special(pp_entry_t *ppp)
 {
 	const char *dbgtext = "?";
 	static char *buf = NULL;
+	char *new_buf;
 
 	assert(ppp->type == def_special);
 
 	if(!strcmp(ppp->ident, "__LINE__"))
 	{
 		dbgtext = "def_special(__LINE__)";
-		buf = pp_xrealloc(buf, 32);
+		new_buf = pp_xrealloc(buf, 32);
+		if(!new_buf)
+			return;
+		buf = new_buf;
 		sprintf(buf, "%d", pp_status.line_number);
 	}
 	else if(!strcmp(ppp->ident, "__FILE__"))
 	{
 		dbgtext = "def_special(__FILE__)";
-		buf = pp_xrealloc(buf, strlen(pp_status.input) + 3);
+		new_buf = pp_xrealloc(buf, strlen(pp_status.input) + 3);
+		if(!new_buf)
+			return;
+		buf = new_buf;
 		sprintf(buf, "\"%s\"", pp_status.input);
 	}
 	else
@@ -1015,12 +1031,19 @@ static char *curdef_text = NULL;
 
 static void add_text(const char *str, int len)
 {
+	int new_alloc;
+	char *new_text;
+
 	if(len == 0)
 		return;
 	if(curdef_idx >= curdef_alloc || curdef_alloc - curdef_idx < len)
 	{
-		curdef_alloc += (len + ALLOCBLOCKSIZE-1) & ~(ALLOCBLOCKSIZE-1);
-		curdef_text = pp_xrealloc(curdef_text, curdef_alloc * sizeof(curdef_text[0]));
+		new_alloc = curdef_alloc + ((len + ALLOCBLOCKSIZE-1) & ~(ALLOCBLOCKSIZE-1));
+		new_text = pp_xrealloc(curdef_text, new_alloc * sizeof(curdef_text[0]));
+		if(!new_text)
+			return;
+		curdef_text = new_text;
+		curdef_alloc = new_alloc;
 		if(curdef_alloc > 65536)
 			ppy_warning("Reallocating macro-expansion buffer larger than 64kB");
 	}
@@ -1218,12 +1241,19 @@ static void new_string(void)
 
 static void add_string(const char *str, int len)
 {
+	int new_alloc;
+	char *new_buffer;
+
 	if(len == 0)
 		return;
 	if(strbuf_idx >= strbuf_alloc || strbuf_alloc - strbuf_idx < len)
 	{
-		strbuf_alloc += (len + ALLOCBLOCKSIZE-1) & ~(ALLOCBLOCKSIZE-1);
-		strbuffer = pp_xrealloc(strbuffer, strbuf_alloc * sizeof(strbuffer[0]));
+		new_alloc = strbuf_alloc + ((len + ALLOCBLOCKSIZE-1) & ~(ALLOCBLOCKSIZE-1));
+		new_buffer = pp_xrealloc(strbuffer, new_alloc * sizeof(strbuffer[0]));
+		if(!new_buffer)
+			return;
+		strbuffer = new_buffer;
+		strbuf_alloc = new_alloc;
 		if(strbuf_alloc > 65536)
 			ppy_warning("Reallocating string buffer larger than 64kB");
 	}
@@ -1234,6 +1264,8 @@ 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
@@ -1310,14 +1342,10 @@ static bufferstackentry_t *pop_buffer(void)
 		bufferstack[bufferstackidx].define->expanding = 0;
 	else
 	{
-		pp_status.line_number = bufferstack[bufferstackidx].line_number;
-		pp_status.char_number = bufferstack[bufferstackidx].char_number;
-		pp_status.input  = bufferstack[bufferstackidx].filename;
-		ncontinuations = bufferstack[bufferstackidx].ncontinuations;
 		if(!bufferstack[bufferstackidx].should_pop)
 		{
 			io_callback->close(currentfile);
-			writestring("# %d \"%s\" 2\n", pp_status.line_number, pp_status.input);
+			writestring("# %d \"%s\" 2\n", bufferstack[bufferstackidx].line_number, bufferstack[bufferstackidx].filename);
 
 			/* We have EOF, check the include logic */
 			if(pp_incl_state.state == 2 && !pp_incl_state.seen_junk && pp_incl_state.ppp)
@@ -1326,6 +1354,9 @@ static bufferstackentry_t *pop_buffer(void)
 				if(ppp)
 				{
 					includelogicentry_t *iep = pp_xmalloc(sizeof(includelogicentry_t));
+					if(!iep)
+						return NULL;
+
 					iep->ppp = ppp;
 					ppp->iep = iep;
 					iep->filename = bufferstack[bufferstackidx].include_filename;
@@ -1335,7 +1366,7 @@ static bufferstackentry_t *pop_buffer(void)
 						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", pp_status.input, pp_status.line_number, pp_incl_state.ppp, iep->filename);
+						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);
 				}
 				else
 					free(bufferstack[bufferstackidx].include_filename);
@@ -1344,6 +1375,10 @@ static bufferstackentry_t *pop_buffer(void)
 			pp_incl_state	= bufferstack[bufferstackidx].incl;
 
 		}
+		pp_status.line_number = bufferstack[bufferstackidx].line_number;
+		pp_status.char_number = bufferstack[bufferstackidx].char_number;
+		pp_status.input  = bufferstack[bufferstackidx].filename;
+		ncontinuations = bufferstack[bufferstackidx].ncontinuations;
 	}
 
 	if(ppy_debug)
@@ -1387,6 +1422,8 @@ 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++;
@@ -1424,8 +1461,13 @@ static void add_text_to_macro(const char *text, int len)
 
 	if(mep->curargalloc - mep->curargsize <= len+1)	/* +1 for '\0' */
 	{
-		mep->curargalloc += (ALLOCBLOCKSIZE > len+1) ? ALLOCBLOCKSIZE : len+1;
-		mep->curarg = pp_xrealloc(mep->curarg, mep->curargalloc * sizeof(mep->curarg[0]));
+		char *new_curarg;
+		int new_alloc =	mep->curargalloc + (ALLOCBLOCKSIZE > len+1) ? ALLOCBLOCKSIZE : len+1;
+		new_curarg = pp_xrealloc(mep->curarg, new_alloc * sizeof(mep->curarg[0]));
+		if(!new_curarg)
+			return;
+		mep->curarg = new_curarg;
+		mep->curargalloc = new_alloc;
 	}
 	memcpy(mep->curarg + mep->curargsize, text, len);
 	mep->curargsize += len;
@@ -1436,14 +1478,30 @@ 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);
 
-	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]));
+	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[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')))
 	{
@@ -1488,7 +1546,7 @@ static void macro_add_expansion(void)
 			pp_status.input,
 			pp_status.line_number,
 			mep->nargs-1,
-			mep->ppargs[mep->nargs-1]);
+			mep->ppargs[mep->nargs-1] ? mep->ppargs[mep->nargs-1] : "");
 }
 
 
diff --git a/libs/wpp/ppy.y b/libs/wpp/ppy.y
index 505987a..c5aed4d 100644
--- a/libs/wpp/ppy.y
+++ b/libs/wpp/ppy.y
@@ -295,11 +295,17 @@ preprocessor
 	| tPRAGMA opt_text tNL	{ 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 {
-                int nl=strlen($2) +3;
-                char *fn=pp_xmalloc(nl);
-                sprintf(fn,"\"%s\"",$2);
-		free($2);
-		pp_do_include(fn,1);
+                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);
+                }
 	}
 	| tRCINCLUDE tDQSTRING {
 		pp_do_include($2,1);
@@ -361,7 +367,7 @@ mtext	: tLITERAL	{ $$ = new_mtext($1, 0, exp_text); }
 		int mat = marg_index($1);
 		if(mat >= 0)
 			$$ = new_mtext(NULL, mat, exp_subst);
-		else
+		else if($1)
 			$$ = new_mtext($1, 0, exp_text);
 		}
 	;
@@ -546,6 +552,8 @@ 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;
@@ -554,16 +562,27 @@ 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 *ma = new_marg(str, 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;
+	ma = new_marg(str, type);
+	if(!ma)
+		return NULL;
+	macro_args[nmacro_args] = ma;
 	nmacro_args++;
-	macro_args = pp_xrealloc(macro_args, nmacro_args * sizeof(macro_args[0]));
-	macro_args[nmacro_args-1] = ma;
 	return ma;
 }
 
 static int marg_index(char *id)
 {
 	int t;
+	if(!id)
+		return -1;
 	for(t = 0; t < nmacro_args; t++)
 	{
 		if(!strcmp(id, macro_args[t]->arg))
@@ -575,6 +594,8 @@ 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
@@ -594,7 +615,11 @@ static mtext_t *combine_mtext(mtext_t *tail, mtext_t *mtp)
 
 	if(tail->type == exp_text && mtp->type == exp_text)
 	{
-		tail->subst.text = pp_xrealloc(tail->subst.text, strlen(tail->subst.text)+strlen(mtp->subst.text)+1);
+		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;
 		strcat(tail->subst.text, mtp->subst.text);
 		free(mtp->subst.text);
 		free(mtp);
@@ -658,9 +683,22 @@ static mtext_t *combine_mtext(mtext_t *tail, mtext_t *mtp)
 
 static char *merge_text(char *s1, char *s2)
 {
-	int l1 = strlen(s1);
-	int l2 = strlen(s2);
-	s1 = pp_xrealloc(s1, l1+l2+1);
+	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;
 	memcpy(s1+l1, s2, l2+1);
 	free(s2);
 	return s1;
diff --git a/libs/wpp/preproc.c b/libs/wpp/preproc.c
index ff34668..1c1e582 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;
 }
@@ -110,6 +108,8 @@ char *pp_xstrdup(const char *str)
 	assert(str != NULL);
 	len = strlen(str)+1;
 	s = pp_xmalloc(len);
+	if(!s)
+		return NULL;
 	return memcpy(s, str, len);
 }
 
@@ -123,6 +123,8 @@ char *wpp_default_lookup( 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++)
@@ -148,6 +150,11 @@ char *wpp_default_lookup( 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 );
@@ -163,6 +170,11 @@ char *wpp_default_lookup( const char *name, 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);
@@ -216,9 +228,12 @@ static int pphash(const char *str)
 
 pp_entry_t *pplookup(const char *ident)
 {
-	int idx = pphash(ident);
+	int idx;
 	pp_entry_t *ppp;
 
+	if(!ident)
+		return NULL;
+	idx = pphash(ident);
 	for(ppp = pp_def_state->defines[idx]; ppp; ppp = ppp->next)
 	{
 		if(!strcmp(ident, ppp->ident))
@@ -264,13 +279,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 */
@@ -310,9 +328,12 @@ pp_entry_t *pp_add_define(char *def, char *text)
 {
 	int len;
 	char *cptr;
-	int idx = pphash(def);
+	int idx;
 	pp_entry_t *ppp;
 
+	if(!def)
+		return NULL;
+	idx = pphash(def);
 	if((ppp = pplookup(def)) != NULL)
 	{
 		if(pp_status.pedantic)
@@ -320,11 +341,18 @@ pp_entry_t *pp_add_define(char *def, char *text)
 		pp_del_define(def);
 	}
 	ppp = pp_xmalloc(sizeof(pp_entry_t));
+	if(!ppp)
+		return NULL;
 	memset( ppp, 0, sizeof(*ppp) );
 	ppp->ident = def;
 	ppp->type = def_define;
 	ppp->subst.text = text;
 	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;
@@ -352,9 +380,12 @@ pp_entry_t *pp_add_define(char *def, char *text)
 
 pp_entry_t *pp_add_macro(char *id, marg_t *args[], int nargs, mtext_t *exp)
 {
-	int idx = pphash(id);
+	int idx;
 	pp_entry_t *ppp;
 
+	if(!id)
+		return NULL;
+	idx = pphash(id);
 	if((ppp = pplookup(id)) != NULL)
 	{
 		if(pp_status.pedantic)
@@ -362,6 +393,8 @@ 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;
@@ -369,6 +402,11 @@ 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;
@@ -420,6 +458,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)
@@ -427,7 +467,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 */
@@ -439,12 +483,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);
 }
 
@@ -638,11 +689,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 1e9d4ba..befdda4 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 b064fc8..75f272b 100644
--- a/libs/wpp/wpp_private.h
+++ b/libs/wpp/wpp_private.h
@@ -205,7 +205,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