You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@guacamole.apache.org by mj...@apache.org on 2018/04/02 19:20:43 UTC

[10/23] guacamole-server git commit: GUACAMOLE-269: Get rid of dynamic allocation and properly free up data structures.

GUACAMOLE-269: Get rid of dynamic allocation and properly free up data structures.


Project: http://git-wip-us.apache.org/repos/asf/guacamole-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/guacamole-server/commit/dd7522bd
Tree: http://git-wip-us.apache.org/repos/asf/guacamole-server/tree/dd7522bd
Diff: http://git-wip-us.apache.org/repos/asf/guacamole-server/diff/dd7522bd

Branch: refs/heads/master
Commit: dd7522bd9fb8c23ea5f64309741b69a2764d20b4
Parents: c3e1b2a
Author: Nick Couchman <vn...@apache.org>
Authored: Sat Mar 3 21:15:04 2018 -0500
Committer: Nick Couchman <vn...@apache.org>
Committed: Thu Mar 8 10:48:22 2018 -0500

----------------------------------------------------------------------
 src/protocols/ssh/ssh.c     | 14 ++++++++++---
 src/protocols/ssh/ttymode.c | 15 +++++---------
 src/protocols/ssh/ttymode.h | 44 ++++++++++++++++++++++++++++++++++++++--
 3 files changed, 58 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/guacamole-server/blob/dd7522bd/src/protocols/ssh/ssh.c
----------------------------------------------------------------------
diff --git a/src/protocols/ssh/ssh.c b/src/protocols/ssh/ssh.c
index eac4ece..35d4013 100644
--- a/src/protocols/ssh/ssh.c
+++ b/src/protocols/ssh/ssh.c
@@ -193,7 +193,7 @@ void* ssh_client_thread(void* data) {
     }
 
     /* Initialize a ttymode array */
-    guac_ssh_ttymodes* ssh_ttymodes = guac_ssh_ttymodes_init();
+    guac_ssh_ttymodes* ssh_ttymodes = guac_ssh_ttymodes_init(1);
 
     /* Set up screen recording, if requested */
     if (settings->recording_path != NULL) {
@@ -303,14 +303,22 @@ void* ssh_client_thread(void* data) {
 
     }
 
+    /* Get char pointer array for TTY Mode Encoding */
+    int ttymode_size = guac_ssh_ttymodes_size(ssh_ttymodes);
+    char* ttymode_array = guac_ssh_ttymodes_to_array(ssh_ttymodes, ttymode_size);
+
     /* Request PTY */
     if (libssh2_channel_request_pty_ex(ssh_client->term_channel, "linux", sizeof("linux")-1,
-            guac_ssh_ttymodes_to_array(ssh_ttymodes), guac_ssh_ttymodes_size(ssh_ttymodes),
-            ssh_client->term->term_width, ssh_client->term->term_height, 0, 0)) {
+            ttymode_array, ttymode_size, ssh_client->term->term_width,
+            ssh_client->term->term_height, 0, 0)) {
         guac_client_abort(client, GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR, "Unable to allocate PTY.");
         return NULL;
     }
 
+    /* We're done with TTY Mode Encoding, so free structures. */
+    free(ttymode_array);
+    free(ssh_ttymodes);
+
     /* If a command is specified, run that instead of a shell */
     if (settings->command != NULL) {
         if (libssh2_channel_exec(ssh_client->term_channel, settings->command)) {

http://git-wip-us.apache.org/repos/asf/guacamole-server/blob/dd7522bd/src/protocols/ssh/ttymode.c
----------------------------------------------------------------------
diff --git a/src/protocols/ssh/ttymode.c b/src/protocols/ssh/ttymode.c
index 3983090..1541da6 100644
--- a/src/protocols/ssh/ttymode.c
+++ b/src/protocols/ssh/ttymode.c
@@ -23,9 +23,9 @@
 #include <stdlib.h>
 #include <string.h>
 
-guac_ssh_ttymodes* guac_ssh_ttymodes_init() {
-    /* Simple allocation for a placeholder */
-    guac_ssh_ttymode* ttymode_array = malloc(1);
+guac_ssh_ttymodes* guac_ssh_ttymodes_init(int max_opcodes) {
+    /* Allocate enough space for the max opcodes */
+    guac_ssh_ttymode* ttymode_array = malloc(sizeof(guac_ssh_ttymode) * max_opcodes);
     
     /* Set up the initial data structure. */
     guac_ssh_ttymodes* empty_modes = malloc(sizeof(guac_ssh_ttymodes));
@@ -39,9 +39,6 @@ void guac_ssh_ttymodes_add(guac_ssh_ttymodes *tty_modes, char opcode, uint32_t v
     /* Increment number of opcodes */
     tty_modes->num_opcodes++;
 
-    /* Increase size of the pointer array */
-    tty_modes->ttymode_array = realloc(tty_modes->ttymode_array, sizeof(guac_ssh_ttymode) * tty_modes->num_opcodes);
-
     /* Add new values */
     tty_modes->ttymode_array[tty_modes->num_opcodes - 1].opcode = opcode;
     tty_modes->ttymode_array[tty_modes->num_opcodes - 1].value = value;
@@ -53,10 +50,8 @@ int guac_ssh_ttymodes_size(guac_ssh_ttymodes *tty_modes) {
     return (tty_modes->num_opcodes * 5) + 1;
 }
 
-char* guac_ssh_ttymodes_to_array(guac_ssh_ttymodes *tty_modes) {
-    /* Total data size should be number of tracked opcodes
-       plus one final byte for the TTY_OP_END code. */
-    int data_size = (tty_modes->num_opcodes * 5) + 1;
+char* guac_ssh_ttymodes_to_array(guac_ssh_ttymodes *tty_modes, int data_size) {
+ 
     char* temp = malloc(data_size);
 
     /* Loop through the array based on number of tracked

http://git-wip-us.apache.org/repos/asf/guacamole-server/blob/dd7522bd/src/protocols/ssh/ttymode.h
----------------------------------------------------------------------
diff --git a/src/protocols/ssh/ttymode.h b/src/protocols/ssh/ttymode.h
index ecc57de..30f36e6 100644
--- a/src/protocols/ssh/ttymode.h
+++ b/src/protocols/ssh/ttymode.h
@@ -61,20 +61,47 @@ typedef struct guac_ssh_ttymodes {
  * Initialize an empty guac_ssh_ttymodes data structure,
  * with a null array of guac_ssh_ttymode and opcodes
  * set to zero.
+ *
+ * @param max_opcodes
+ *     The maximum number of opcodes that will be allocated.
+ *
+ * @return
+ *     The pointer array for the gauc_ssh_ttymodes data
+ *     structure generated by the function.
  */
-guac_ssh_ttymodes* guac_ssh_ttymodes_init();
+guac_ssh_ttymodes* guac_ssh_ttymodes_init(int max_opcodes);
 
 /**
  * Add an item to the opcode array.  This resizes the
  * array, increments the number of opcodes, and adds
  * the specified opcode and value pair to the data
  * structure.
+ *
+ * @param tty_modes
+ *     The pointer to the guac_ssh_ttymodes data structure
+ *     to add the opcode to.
+ * 
+ * @param opcode
+ *     The single byte opcode as documented in section 8
+ *     of the SSH RFC.
+ *
+ * @param value
+ *     The four byte value of the opcodeto add to the
+ *     guac_ssh_ttymodes data structure.     
  */
 void guac_ssh_ttymodes_add(guac_ssh_ttymodes *tty_modes, char opcode, uint32_t value);
 
 /**
  * Retrieve the size, in bytes, of the ttymode_array
  * in the given guac_ssh_ttymodes data structure.
+ * 
+ * @param tty_modes
+ *     The pointer to the guac_ssh_ttymodes structure
+ *     whose size we are curious about.
+ *
+ * @return
+ *     The number of bytes of the opcodes and value paris
+ *     in the data structure.
  */
 int guac_ssh_ttymodes_size(guac_ssh_ttymodes *tty_modes);
 
@@ -82,7 +109,20 @@ int guac_ssh_ttymodes_size(guac_ssh_ttymodes *tty_modes);
  * Convert the ttymodes data structure into a char
  * pointer array suitable for passing into the
  * libssh2_channel_request_pty_ex() function.
+ *
+ * @param tty_modes
+ *     The pointer to the guac_ssh_ttymodes structure
+ *     that contains the opcode and value pairs
+ *     we want to convert to a character pointer array.
+ *
+ * @param data_size
+ *     The size of the resulting character pointer
+ *     array.
+ *
+ * @return
+ *     The character pointer array of opcode and
+ *     value pairs to be passed to a SSH session.
  */
-char* guac_ssh_ttymodes_to_array(guac_ssh_ttymodes *tty_modes);
+char* guac_ssh_ttymodes_to_array(guac_ssh_ttymodes *tty_modes, int data_size);
 
 #endif