[or-cvs] Make sure control command bodies are always followed by a NUL

Nick Mathewson nickm at seul.org
Thu Nov 4 22:30:51 UTC 2004


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

Modified Files:
	buffers.c 
Log Message:
Make sure control command bodies are always followed by a NUL

Index: buffers.c
===================================================================
RCS file: /home/or/cvsroot/src/or/buffers.c,v
retrieving revision 1.110
retrieving revision 1.111
diff -u -d -r1.110 -r1.111
--- buffers.c	3 Nov 2004 01:32:26 -0000	1.110
+++ buffers.c	4 Nov 2004 22:30:49 -0000	1.111
@@ -636,7 +636,16 @@
   }
 }
 
-/* DOCDOC: 1 if complete, 0 if pending, -1 on error. */
+/** If there is a complete control message waiting on buf, then store
+ * its contents into *<b>type_out</b>, store its body's length into
+ * *<b>len_out</b>, allocate and store a string for its body into
+ * *<b>body_out</b>, and return -1.  (body_out will always be NUL-terminated,
+ * even if the control message body doesn't end with NUL.)
+ *
+ * If there is not a complete control message waiting, return 0.
+ *
+ * Return -1 on error.
+ */
 int fetch_from_buf_control(buf_t *buf, uint16_t *len_out, uint16_t *type_out,
                            char **body_out)
 {
@@ -657,8 +666,9 @@
   *len_out = len;
   *type_out = ntohs(get_uint16(buf->mem+2));
   if (len) {
-    *body_out = tor_malloc(len);
+    *body_out = tor_malloc(len+1);
     memcpy(*body_out, buf->mem+4, len);
+    body_out[len] = '\0';
   } else {
     *body_out = NULL;
   }



More information about the tor-commits mailing list