Re: [PATCH] error.c: fix gcc warning (vasprintf)

Alexey Tourbin <[email protected]>
Newsgroups gmane.comp.web.links
Message-ID <[email protected]>
On Tue, Feb 27, 2007 at 10:43:13PM +0200, Kalle Olavi Niemitalo wrote:
> Alexey Tourbin <[email protected]> writes:
> 
> >  	va_start(params, fmt);
> > -	vasprintf((char **) &buf, fmt, params);
> > +	rv = vasprintf((char **) &buf, fmt, params);
> > +	if (rv < 0) {
> > +		perror("vasprintf");
> > +		return;
> > +	}
> >  	va_end(params);
> >  	elinks_internal("assertion failed: %s", buf);
> 
> This is wrong.  va_end and elinks_internal must be called even if
> vasprintf fails.
> 
> Possibilities to be handled:
> (a) vasprintf returns an error and doesn't alter buf.  Because
>     buf is initialized as NULL, this is the same as (b).
> (b) vasprintf returns an error and sets buf = NULL.  This NULL
>     should not be passed to elinks_internal because it will go
>     to snprintf from there and perhaps cause a crash.  The NULL
>     could be replaced with an empty string.  Displaying the
>     error from vasprintf may be useful but may also distract
>     users into reporting that error instead of the file name and
>     line number.
> (c) vasprintf returns an error but sets buf != NULL.  In this
>     case the string probably contains something sensible so it
>     should be used.
> (d) vasprintf returns success but sets buf = NULL.  That should
>     not be possible.
> (e) vasprintf returns success and sets buf != NULL.  This is the
>     usual case.

commit 021a1d43da2db001436d777eeb2f533f011aa471
Author: Alexey Tourbin <[email protected]>
Date:   Sun Oct 15 17:42:31 2006 +0400

    error.c: fix gcc warning (vasprintf)
    (cherry picked from commit 55dff3d9c23e2c8f3358fa265fc4276616713ed5)
    
    Actually if vasprintf fails, use plain fmt string.

diff --git a/src/util/error.c b/src/util/error.c
index 34e4c88..ba6b0ee 100644
--- a/src/util/error.c
+++ b/src/util/error.c
@@ -145,13 +145,16 @@ void
 elinks_assertm(int x, unsigned char *fmt, ...)
 {
 	unsigned char *buf = NULL;
+	int rv;
 	va_list params;
 
 	if (assert_failed) return;
 	if (!(assert_failed = !x)) return;
 
 	va_start(params, fmt);
-	vasprintf((char **) &buf, fmt, params);
+	rv = vasprintf((char **) &buf, fmt, params);
+	if (rv < 0)
+		buf = fmt;
 	va_end(params);
 	elinks_internal("assertion failed: %s", buf);
 	if (buf) free(buf);

_______________________________________________
elinks-dev mailing list
[email protected]
http://linuxfromscratch.org/mailman/listinfo/elinks-dev
signature.asc (application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFGI+opfBKgtDjnu0YRAovyAKDH3uBmOiRGXuFewbauSWE50zigMQCgiGWq
/oOm4RbfHh6sXq0SwZeGees=
=Q5Bl
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.