Commit dfce44aa authored by Rémi Duraffort's avatar Rémi Duraffort

* Fix memleak (#2352)

* Use NULL instead of 0
* cosmetics.
parent e7754805
...@@ -193,26 +193,32 @@ static xml_reader_t *ReaderCreate( xml_t *p_xml, stream_t *s ) ...@@ -193,26 +193,32 @@ static xml_reader_t *ReaderCreate( xml_t *p_xml, stream_t *s )
} }
p_buffer = p_new; p_buffer = p_new;
} }
p_buffer[ i_pos + i_size ] = 0; /* 0 terminated string */
if( i_pos + i_size == 0 ) if( i_pos + i_size == 0 )
{ {
msg_Dbg( p_xml, "empty XML" ); msg_Dbg( p_xml, "empty XML" );
free( p_buffer ); free( p_buffer );
return 0; return NULL;
} }
p_buffer[ i_pos + i_size ] = '\0'; /* 0 terminated string */
p_root = xtag_new_parse( p_buffer, i_buffer ); p_root = xtag_new_parse( p_buffer, i_buffer );
if( !p_root ) if( !p_root )
{ {
msg_Warn( p_xml, "couldn't parse XML" ); msg_Warn( p_xml, "couldn't parse XML" );
free( p_buffer ); free( p_buffer );
return 0; return NULL;
} }
free( p_buffer ); free( p_buffer );
p_reader = malloc( sizeof(xml_reader_t) ); p_reader = malloc( sizeof(xml_reader_t) );
if( !p_reader )
return NULL;
p_reader->p_sys = malloc( sizeof(xml_reader_sys_t) ); p_reader->p_sys = malloc( sizeof(xml_reader_sys_t) );
if( !p_reader->p_sys )
{
free( p_reader );
return NULL;
}
p_reader->p_sys->p_root = p_root; p_reader->p_sys->p_root = p_root;
p_reader->p_sys->p_curtag = NULL; p_reader->p_sys->p_curtag = NULL;
p_reader->p_sys->p_curattr = NULL; p_reader->p_sys->p_curattr = NULL;
...@@ -252,12 +258,12 @@ static int ReaderRead( xml_reader_t *p_reader ) ...@@ -252,12 +258,12 @@ static int ReaderRead( xml_reader_t *p_reader )
return 1; return 1;
} }
while( 1 ) while( true )
{ {
if( (p_child = xtag_next_child( p_reader->p_sys->p_curtag, 0 )) ) if( (p_child = xtag_next_child( p_reader->p_sys->p_curtag, 0 )) )
{ {
p_reader->p_sys->p_curtag = p_child; p_reader->p_sys->p_curtag = p_child;
p_reader->p_sys->p_curattr = 0; p_reader->p_sys->p_curattr = NULL;
p_reader->p_sys->b_endtag = false; p_reader->p_sys->b_endtag = false;
return 1; return 1;
} }
...@@ -279,10 +285,12 @@ static int ReaderRead( xml_reader_t *p_reader ) ...@@ -279,10 +285,12 @@ static int ReaderRead( xml_reader_t *p_reader )
static int ReaderNodeType( xml_reader_t *p_reader ) static int ReaderNodeType( xml_reader_t *p_reader )
{ {
if( p_reader->p_sys->p_curtag->name && if( p_reader->p_sys->p_curtag->name && p_reader->p_sys->b_endtag )
p_reader->p_sys->b_endtag ) return XML_READER_ENDELEM; return XML_READER_ENDELEM;
if( p_reader->p_sys->p_curtag->name ) return XML_READER_STARTELEM; if( p_reader->p_sys->p_curtag->name )
if( p_reader->p_sys->p_curtag->pcdata ) return XML_READER_TEXT; return XML_READER_STARTELEM;
if( p_reader->p_sys->p_curtag->pcdata )
return XML_READER_TEXT;
return XML_READER_NONE; return XML_READER_NONE;
} }
...@@ -300,8 +308,7 @@ static char *ReaderName( xml_reader_t *p_reader ) ...@@ -300,8 +308,7 @@ static char *ReaderName( xml_reader_t *p_reader )
else else
psz_name = ((XAttribute *)p_reader->p_sys->p_curattr->data)->name; psz_name = ((XAttribute *)p_reader->p_sys->p_curattr->data)->name;
if( psz_name ) return strdup( psz_name ); return psz_name ? strdup( psz_name ) : NULL;
else return 0;
} }
static char *ReaderValue( xml_reader_t *p_reader ) static char *ReaderValue( xml_reader_t *p_reader )
...@@ -315,7 +322,7 @@ static char *ReaderValue( xml_reader_t *p_reader ) ...@@ -315,7 +322,7 @@ static char *ReaderValue( xml_reader_t *p_reader )
return strdup( p_reader->p_sys->p_curtag->pcdata ); return strdup( p_reader->p_sys->p_curtag->pcdata );
} }
if( !p_reader->p_sys->p_curattr ) return 0; if( !p_reader->p_sys->p_curattr ) return NULL;
#ifdef XTAG_DEBUG #ifdef XTAG_DEBUG
fprintf( stderr, "%s=%s\n", ((XAttribute *)p_reader->p_sys->p_curattr->data)->name, fprintf( stderr, "%s=%s\n", ((XAttribute *)p_reader->p_sys->p_curattr->data)->name,
...@@ -324,8 +331,7 @@ static char *ReaderValue( xml_reader_t *p_reader ) ...@@ -324,8 +331,7 @@ static char *ReaderValue( xml_reader_t *p_reader )
psz_name = ((XAttribute *)p_reader->p_sys->p_curattr->data)->value; psz_name = ((XAttribute *)p_reader->p_sys->p_curattr->data)->value;
if( psz_name ) return strdup( psz_name ); return psz_name ? strdup( psz_name ) : NULL;
else return 0;
} }
static int ReaderNextAttr( xml_reader_t *p_reader ) static int ReaderNextAttr( xml_reader_t *p_reader )
...@@ -335,8 +341,7 @@ static int ReaderNextAttr( xml_reader_t *p_reader ) ...@@ -335,8 +341,7 @@ static int ReaderNextAttr( xml_reader_t *p_reader )
else if( p_reader->p_sys->p_curattr ) else if( p_reader->p_sys->p_curattr )
p_reader->p_sys->p_curattr = p_reader->p_sys->p_curattr->next; p_reader->p_sys->p_curattr = p_reader->p_sys->p_curattr->next;
if( p_reader->p_sys->p_curattr ) return VLC_SUCCESS; return p_reader->p_sys->p_curattr ? VLC_SUCCESS : VLC_EGENERIC;
else return VLC_EGENERIC;
} }
/***************************************************************************** /*****************************************************************************
...@@ -351,10 +356,14 @@ static XList *xlist_append( XList *list, void *data ) ...@@ -351,10 +356,14 @@ static XList *xlist_append( XList *list, void *data )
l->prev = l->next = NULL; l->prev = l->next = NULL;
l->data = data; l->data = data;
if( list == NULL ) return l; if( !list )
return l;
for( last = list; last; last = last->next ) for( last = list; last; last = last->next )
if( last->next == NULL ) break; {
if( !last->next )
break;
}
if( last ) last->next = l; if( last ) last->next = l;
l->prev = last; l->prev = last;
...@@ -507,7 +516,11 @@ static char *xtag_slurp_quoted( XTagParser *parser ) ...@@ -507,7 +516,11 @@ static char *xtag_slurp_quoted( XTagParser *parser )
ret[xi] = '\0'; ret[xi] = '\0';
parser->start = &s[xi]; parser->start = &s[xi];
if( !xtag_assert_and_pass( parser, quote ) ) return NULL; if( !xtag_assert_and_pass( parser, quote ) )
{
free( ret );
return NULL;
}
return ret; return ret;
} }
...@@ -518,12 +531,14 @@ static XAttribute *xtag_parse_attribute( XTagParser *parser ) ...@@ -518,12 +531,14 @@ static XAttribute *xtag_parse_attribute( XTagParser *parser )
char *name, *value; char *name, *value;
char *s; char *s;
if( !parser->valid ) return NULL; if( !parser->valid )
return NULL;
xtag_skip_whitespace( parser ); xtag_skip_whitespace( parser );
name = xtag_slurp_to( parser, X_WHITESPACE|X_EQUAL, X_SLASH|X_CLOSETAG ); name = xtag_slurp_to( parser, X_WHITESPACE|X_EQUAL, X_SLASH|X_CLOSETAG );
if( name == NULL ) return NULL; if( !name )
return NULL;
xtag_skip_whitespace( parser ); xtag_skip_whitespace( parser );
s = parser->start; s = parser->start;
...@@ -747,7 +762,8 @@ static XTag *xtag_free( XTag *xtag ) ...@@ -747,7 +762,8 @@ static XTag *xtag_free( XTag *xtag )
XAttribute *attr; XAttribute *attr;
XTag *child; XTag *child;
if( xtag == NULL ) return NULL; if( !xtag )
return NULL;
free( xtag->name ); free( xtag->name );
free( xtag->pcdata ); free( xtag->pcdata );
...@@ -1052,3 +1068,4 @@ static int xtag_snprint( char *buf, int n, XTag *xtag ) ...@@ -1052,3 +1068,4 @@ static int xtag_snprint( char *buf, int n, XTag *xtag )
return written; return written;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment