From 4b76cd922db22e0fac65d26b49882c140a4a18b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?IOhannes=20m=20zm=C3=B6lnig?= Date: Thu, 22 Jul 2004 12:19:20 +0000 Subject: fixed a memory-leak in [l2s] (thanks to ix@replic.net) svn path=/trunk/externals/zexy/; revision=1883 --- src/z_strings.c | 78 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 48 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/z_strings.c b/src/z_strings.c index e2f8e21..7ec8d40 100644 --- a/src/z_strings.c +++ b/src/z_strings.c @@ -3,12 +3,12 @@ #include #ifdef NT -#pragma warning( disable : 4244 ) -#pragma warning( disable : 4305 ) -#define sqrtf sqrt -#define STATIC_INLINE -#else -#define STATIC_INLINE static +# pragma warning( disable : 4244 ) +# pragma warning( disable : 4305 ) +# define sqrtf sqrt +# define STATIC_INLINE +#else +# define STATIC_INLINE static #endif /* @@ -191,39 +191,54 @@ static void list2symbol_bang(t_list2symbol *x) t_atom *argv=x->ap; int argc=x->ac; char *result = 0; - char string[MAXPDSTRING]; - int length = 0; + int length = 0, len=0; int i= argc; + char *connector=0; + char connlen=0; + if(x->connector)connector=x->connector->s_name; + if(connector)connlen=strlen(connector); + /* 1st get the length of the symbol */ + if(x->s)length+=strlen(x->s->s_name); + else length-=connlen; + + length+=i*connlen; + + while(i--){ + char buffer[MAXPDSTRING]; + atom_string(argv++, buffer, MAXPDSTRING); + length+=strlen(buffer); + } + + if (length<0){ + outlet_symbol(x->x_obj.ob_outlet, gensym("")); + return; + } + + result = (char*)getbytes((length+1)*sizeof(char)); + + /* 2nd create the symbol */ if (x->s){ char *buf = x->s->s_name; - int newlen = length + strlen(buf); - strcpy(string+length, buf); - length = newlen; - if(i && x->connector){ - char *buf = x->connector->s_name; - newlen = length + strlen(buf); - strcpy(string+length, buf); - length = newlen; + strcpy(result+len, buf); + len+=strlen(buf); + if(i && connector){ + strcpy(result+len, connector); + len += connlen; } } + i=argc; + argv=x->ap; while(i--){ char buffer[MAXPDSTRING]; - int newlen; atom_string(argv++, buffer, MAXPDSTRING); - newlen = length + strlen(buffer); - strcpy(string+length, buffer); - length = newlen; - if(i && x->connector){ - char *buf = x->connector->s_name; - newlen = length + strlen(buf); - strcpy(string+length, buf); - length = newlen; + strcpy(result+len, buffer); + len += strlen(buffer); + if(i && connector){ + strcpy(result+len, connector); + len += connlen; } } - length = strlen(string); - result = (char*)getbytes((length+1)*sizeof(char)); - strcpy(result, string); result[length]=0; outlet_symbol(x->x_obj.ob_outlet, gensym(result)); freebytes(result, (length+1)*sizeof(char)); @@ -335,12 +350,15 @@ static void symbol2list_process(t_symbol2list *x) deli=x->delimiter->s_name; dell=strlen(deli); + /* get the number of tokens */ while((d=strstr(cp, deli))){ - if (d!=NULL && d!=cp)i++; + if (d!=NULL && d!=cp){ + i++; + } cp=d+dell; } - + /* resize the list-buffer if necessary */ if(x->argnumargv, x->argnum*sizeof(t_atom)); -- cgit v1.2.1