tech@mandoc.bsd.lv
 help / color / mirror / Atom feed
* compat_vasprintf.c: use va_copy
@ 2015-03-20 14:36 Christian Neukirchen
  2015-03-20 15:45 ` Ingo Schwarze
  0 siblings, 1 reply; 2+ messages in thread
From: Christian Neukirchen @ 2015-03-20 14:36 UTC (permalink / raw)
  To: tech

Hi,

When building CVS HEAD on Linux x86_64/glibc, the vasprintf check
fails when -D_GNU_SOURCE is not passed, and mdocml falls back to
compat_vasprintf.c.  This version of vasprintf.c segfaults on glibc,
because the same va_list is used twice.  Probably affects other
architectures too.

The solution is to use va_copy to get a second va_list.

--- compat_vasprintf.c
+++ compat_vasprintf.c
@@ -38,9 +38,13 @@ vasprintf(char **ret, const char *format, va_list ap)
 {
 	char	 buf[2];
 	int	 sz;
+	va_list	 ap2;
 
-	if ((sz = vsnprintf(buf, sizeof(buf), format, ap)) != -1 &&
-	    (*ret = malloc(sz + 1)) != NULL) {
+	va_copy(ap2, ap);
+	sz = vsnprintf(buf, sizeof(buf), format, ap2);
+	va_end(ap2);
+
+	if (sz != -1 && (*ret = malloc(sz + 1)) != NULL) {
 		if (vsnprintf(*ret, sz + 1, format, ap) == sz)
 			return(sz);
 		free(*ret);

cu,
-- 
Christian Neukirchen  <chneukirchen@gmail.com>  http://chneukirchen.org
--
 To unsubscribe send an email to tech+unsubscribe@mdocml.bsd.lv

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: compat_vasprintf.c: use va_copy
  2015-03-20 14:36 compat_vasprintf.c: use va_copy Christian Neukirchen
@ 2015-03-20 15:45 ` Ingo Schwarze
  0 siblings, 0 replies; 2+ messages in thread
From: Ingo Schwarze @ 2015-03-20 15:45 UTC (permalink / raw)
  To: Christian Neukirchen; +Cc: tech

Hi Christian,

Christian Neukirchen wrote on Fri, Mar 20, 2015 at 03:36:49PM +0100:

> When building CVS HEAD on Linux x86_64/glibc, the vasprintf check
> fails when -D_GNU_SOURCE is not passed, and mdocml falls back to
> compat_vasprintf.c.  This version of vasprintf.c segfaults on glibc,
> because the same va_list is used twice.  Probably affects other
> architectures too.
> 
> The solution is to use va_copy to get a second va_list.

I have put this in, it can hardly do harm.

Besides, i have added _GNU_SOURCE to test-vasprintf.c, you are
right that it was missing, see the patch below.  Note that the
other files using vasprintf() don't need it because config.h
already defines it.

Thanks,
  Ingo


> --- compat_vasprintf.c
> +++ compat_vasprintf.c
> @@ -38,9 +38,13 @@ vasprintf(char **ret, const char *format, va_list ap)
>  {
>  	char	 buf[2];
>  	int	 sz;
> +	va_list	 ap2;
>  
> -	if ((sz = vsnprintf(buf, sizeof(buf), format, ap)) != -1 &&
> -	    (*ret = malloc(sz + 1)) != NULL) {
> +	va_copy(ap2, ap);
> +	sz = vsnprintf(buf, sizeof(buf), format, ap2);
> +	va_end(ap2);
> +
> +	if (sz != -1 && (*ret = malloc(sz + 1)) != NULL) {
>  		if (vsnprintf(*ret, sz + 1, format, ap) == sz)
>  			return(sz);
>  		free(*ret);


Log Message:
-----------
vasprintf(3) needs _GNU_SOURCE on Linux;
pointed out by Christian Neukirchen <chneukirchen at gmail dot com>.

Modified Files:
--------------
    mdocml:
        test-vasprintf.c

Revision Data
-------------
Index: test-vasprintf.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/test-vasprintf.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -Ltest-vasprintf.c -Ltest-vasprintf.c -u -p -r1.1 -r1.2
--- test-vasprintf.c
+++ test-vasprintf.c
@@ -15,6 +15,10 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#if defined(__linux__) || defined(__MINT__)
+#define _GNU_SOURCE /* vasprintf() */
+#endif
+
 #include <stdarg.h>
 #include <stdio.h>
 #include <string.h>
--
 To unsubscribe send an email to tech+unsubscribe@mdocml.bsd.lv

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-03-20 15:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-20 14:36 compat_vasprintf.c: use va_copy Christian Neukirchen
2015-03-20 15:45 ` Ingo Schwarze

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).