[tor-commits] [tor/master] Extract the argument-splitting part of control.c's parser

dgoulet at torproject.org dgoulet at torproject.org
Tue Apr 30 15:57:51 UTC 2019


commit f18b7dc4731bcb853db92a0faaa4ec03d6ef5586
Author: Nick Mathewson <nickm at torproject.org>
Date:   Tue Apr 2 10:29:07 2019 -0400

    Extract the argument-splitting part of control.c's parser
    
    This is preliminary work for fixing 29984; no behavior has changed.
---
 src/feature/control/control.c | 42 ++++++++++++++++++++++++++++--------------
 src/feature/control/control.h |  5 +++++
 2 files changed, 33 insertions(+), 14 deletions(-)

diff --git a/src/feature/control/control.c b/src/feature/control/control.c
index 41e21c0a1..308f1936b 100644
--- a/src/feature/control/control.c
+++ b/src/feature/control/control.c
@@ -33,6 +33,7 @@
  **/
 
 #define CONTROL_MODULE_PRIVATE
+#define CONTROL_PRIVATE
 
 #include "core/or/or.h"
 #include "app/config/config.h"
@@ -274,6 +275,31 @@ peek_connection_has_http_command(connection_t *conn)
   return peek_buf_has_http_command(conn->inbuf);
 }
 
+/**
+ * Helper: take a nul-terminated command of given length, and find where
+ * the command starts and the argument begins.  Separate them with a NUL,
+ * and return a pointer to the arguments.
+ **/
+STATIC char *
+control_split_incoming_command(char *incoming_cmd, size_t *data_len)
+{
+  size_t cmd_len = 0;
+  while (cmd_len < *data_len
+         && !TOR_ISSPACE(incoming_cmd[cmd_len]))
+    ++cmd_len;
+
+  incoming_cmd[cmd_len]='\0';
+  char *args = incoming_cmd+cmd_len+1;
+  tor_assert(*data_len>cmd_len);
+  *data_len -= (cmd_len+1); /* skip the command and NUL we added after it */
+  while (TOR_ISSPACE(*args)) {
+    ++args;
+    --*data_len;
+  }
+
+  return args;
+}
+
 static const char CONTROLPORT_IS_NOT_AN_HTTP_PROXY_MSG[] =
   "HTTP/1.0 501 Tor ControlPort is not an HTTP proxy"
   "\r\nContent-Type: text/html; charset=iso-8859-1\r\n\r\n"
@@ -308,7 +334,6 @@ connection_control_process_inbuf(control_connection_t *conn)
 {
   size_t data_len;
   uint32_t cmd_data_len;
-  int cmd_len;
   char *args;
 
   tor_assert(conn);
@@ -400,22 +425,11 @@ connection_control_process_inbuf(control_connection_t *conn)
     /* Otherwise, read another line. */
   }
   data_len = conn->incoming_cmd_cur_len;
+
   /* Okay, we now have a command sitting on conn->incoming_cmd. See if we
    * recognize it.
    */
-  cmd_len = 0;
-  while ((size_t)cmd_len < data_len
-         && !TOR_ISSPACE(conn->incoming_cmd[cmd_len]))
-    ++cmd_len;
-
-  conn->incoming_cmd[cmd_len]='\0';
-  args = conn->incoming_cmd+cmd_len+1;
-  tor_assert(data_len>(size_t)cmd_len);
-  data_len -= (cmd_len+1); /* skip the command and NUL we added after it */
-  while (TOR_ISSPACE(*args)) {
-    ++args;
-    --data_len;
-  }
+  args = control_split_incoming_command(conn->incoming_cmd, &data_len);
 
   /* If the connection is already closing, ignore further commands */
   if (TO_CONN(conn)->marked_for_close) {
diff --git a/src/feature/control/control.h b/src/feature/control/control.h
index 308383793..6fc1c40ca 100644
--- a/src/feature/control/control.h
+++ b/src/feature/control/control.h
@@ -60,4 +60,9 @@ int get_cached_network_liveness(void);
 void set_cached_network_liveness(int liveness);
 #endif /* defined(CONTROL_MODULE_PRIVATE) */
 
+#ifdef CONTROL_PRIVATE
+STATIC char *control_split_incoming_command(char *incoming_cmd,
+                                            size_t *data_len);
+#endif
+
 #endif /* !defined(TOR_CONTROL_H) */





More information about the tor-commits mailing list