]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
fix a segfault on truncated log lines
authorRoger Dingledine <arma@torproject.org>
Fri, 19 Sep 2003 09:30:34 +0000 (09:30 +0000)
committerRoger Dingledine <arma@torproject.org>
Fri, 19 Sep 2003 09:30:34 +0000 (09:30 +0000)
svn:r473

src/common/log.c

index c4a5e683c906f59a2f0553dba8c32d53a63e128e..0f75237c6d57fe0e172009c98e925861fb6365f8 100644 (file)
@@ -49,14 +49,22 @@ static INLINE void format_msg(char *buf, size_t buf_len,
   my_gettimeofday(&now);
   t = (time_t)now.tv_sec;
   
-  n  = strftime(buf, buf_len, "%b %d %H:%M:%S", localtime(&t));
+  n = strftime(buf, buf_len, "%b %d %H:%M:%S", localtime(&t));
   n += snprintf(buf+n, buf_len-n,
-               ".%.3ld [%s] ", 
-               (long)now.tv_usec / 1000, sev_to_string(severity));
-  if (funcname)
+                ".%.3ld [%s] ", 
+                (long)now.tv_usec / 1000, sev_to_string(severity));
+  if(n > buf_len)
+    n = buf_len; /* the *nprintf funcs return how many bytes they
+                  * _would_ print, if the output is truncated */
+  if (funcname) {
     n += snprintf(buf+n, buf_len-n, "%s(): ", funcname);
+    if(n > buf_len)
+      n = buf_len;
+  }
 
   n += vsnprintf(buf+n,buf_len-n,format,ap);
+  if(n > buf_len)
+    n = buf_len;
   buf[n]='\n';
   buf[n+1]='\0';
 }