[Dnsmasq-discuss] Incorrect use of variable arguments

Cedric Duval cedricduval at free.fr
Mon Feb 11 08:36:11 GMT 2008


Hi,

when debug mode is activated, my_syslog() uses its va_list twice without
properly resetting its state, causing dnsmasq to crash on powerpc.

The first patch fixes this, and the second one (incremental) just puts
the va_ macros in more "obvious" places. While not stricly necessary, I
think it decreases the potential for future bugs caused by an overlooked
macro mismatch. :)

No use of C99's va_copy as it may reduce portability for no real gain.

Cheers,
-- 
Cédric
-------------- next part --------------
diff -r bbeaa4a7d1fc src/log.c
--- a/src/log.c	Sat Feb 09 20:46:20 2008 +0100
+++ b/src/log.c	Sat Feb 09 21:04:44 2008 +0100
@@ -249,14 +249,16 @@ void my_syslog(int priority, const char 
   size_t len;
   pid_t pid = getpid();
 
-  va_start(ap, format); 
-  
   if (log_stderr) 
     {
       fprintf(stderr, "dnsmasq: ");
+      va_start(ap, format);
       vfprintf(stderr, format, ap);
+      va_end(ap);
       fputc('\n', stderr);
     }
+
+  va_start(ap, format);
   
   if (log_fd == -1)
     {
-------------- next part --------------
diff -r 16cc651cd606 src/log.c
--- a/src/log.c	Sat Feb 09 21:04:44 2008 +0100
+++ b/src/log.c	Sat Feb 09 21:37:12 2008 +0100
@@ -258,8 +258,6 @@ void my_syslog(int priority, const char 
       fputc('\n', stderr);
     }
 
-  va_start(ap, format);
-  
   if (log_fd == -1)
     {
       /* fall-back to syslog if we die during startup or fail during running. */
@@ -269,6 +267,7 @@ void my_syslog(int priority, const char 
 	  openlog("dnsmasq", LOG_PID, log_fac);
 	  isopen = 1;
 	}
+      va_start(ap, format);  
       vsyslog(priority, format, ap);
       va_end(ap);
       return;
@@ -301,7 +300,9 @@ void my_syslog(int priority, const char 
       
       p += sprintf(p, "%.15s dnsmasq[%d]: ", ctime(&time_now) + 4, (int)pid);
       len = p - entry->payload;
+      va_start(ap, format);  
       len += vsnprintf(p, MAX_MESSAGE - len, format, ap) + 1; /* include zero-terminator */
+      va_end(ap);
       entry->length = len > MAX_MESSAGE ? MAX_MESSAGE : len;
       entry->offset = 0;
       entry->pid = pid;
@@ -348,8 +349,6 @@ void my_syslog(int priority, const char 
 	  log_write();
 	}
     } 
- 
-  va_end(ap);
 }
 
 void set_log_writer(fd_set *set, int *maxfdp)


More information about the Dnsmasq-discuss mailing list