[or-cvs] clarify why strlcpy is safe in truncated log messages

Nick Mathewson nickm at seul.org
Tue Nov 30 06:15:09 UTC 2004


Update of /home/or/cvsroot/tor/src/common
In directory moria.mit.edu:/tmp/cvs-serv3404/src/common

Modified Files:
	log.c 
Log Message:
clarify why strlcpy is safe in truncated log messages

Index: log.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/log.c,v
retrieving revision 1.82
retrieving revision 1.83
diff -u -d -r1.82 -r1.83
--- log.c	29 Nov 2004 22:25:28 -0000	1.82
+++ log.c	30 Nov 2004 06:15:06 -0000	1.83
@@ -143,15 +143,14 @@
     /* The message was too long; overwrite the end of the buffer with
      * "[...truncated]" */
     if (buf_len >= TRUNCATED_STR_LEN) {
-      /* This is safe, since we have an extra character after buf_len
-         to hold the \0. */
-      strlcpy(buf+buf_len-TRUNCATED_STR_LEN, TRUNCATED_STR,
-              buf_len-(buf_len-TRUNCATED_STR_LEN-1));
+      int offset = buf_len-TRUNCATED_STR_LEN;
+      /* We have an extra 2 characters after buf_len to hold the \n\0,
+       * so it's safe to add 1 to the size here. */
+      strlcpy(buf+offset, TRUNCATED_STR, buf_len-offset+1);
     }
     /* Set 'n' to the end of the buffer, where we'll be writing \n\0.
      * Since we already subtracted 2 from buf_len, this is safe.*/
     n = buf_len;
-
   } else {
     n += r;
   }



More information about the tor-commits mailing list