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

This commit is contained in:
Nick Couchman 2018-03-03 21:15:04 -05:00
parent c3e1b2afef
commit dd7522bd9f
3 changed files with 58 additions and 15 deletions

View File

@ -193,7 +193,7 @@ void* ssh_client_thread(void* data) {
} }
/* Initialize a ttymode array */ /* 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 */ /* Set up screen recording, if requested */
if (settings->recording_path != NULL) { 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 */ /* Request PTY */
if (libssh2_channel_request_pty_ex(ssh_client->term_channel, "linux", sizeof("linux")-1, 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), ttymode_array, ttymode_size, ssh_client->term->term_width,
ssh_client->term->term_width, ssh_client->term->term_height, 0, 0)) { ssh_client->term->term_height, 0, 0)) {
guac_client_abort(client, GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR, "Unable to allocate PTY."); guac_client_abort(client, GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR, "Unable to allocate PTY.");
return NULL; 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 a command is specified, run that instead of a shell */
if (settings->command != NULL) { if (settings->command != NULL) {
if (libssh2_channel_exec(ssh_client->term_channel, settings->command)) { if (libssh2_channel_exec(ssh_client->term_channel, settings->command)) {

View File

@ -23,9 +23,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
guac_ssh_ttymodes* guac_ssh_ttymodes_init() { guac_ssh_ttymodes* guac_ssh_ttymodes_init(int max_opcodes) {
/* Simple allocation for a placeholder */ /* Allocate enough space for the max opcodes */
guac_ssh_ttymode* ttymode_array = malloc(1); guac_ssh_ttymode* ttymode_array = malloc(sizeof(guac_ssh_ttymode) * max_opcodes);
/* Set up the initial data structure. */ /* Set up the initial data structure. */
guac_ssh_ttymodes* empty_modes = malloc(sizeof(guac_ssh_ttymodes)); 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 */ /* Increment number of opcodes */
tty_modes->num_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 */ /* Add new values */
tty_modes->ttymode_array[tty_modes->num_opcodes - 1].opcode = opcode; tty_modes->ttymode_array[tty_modes->num_opcodes - 1].opcode = opcode;
tty_modes->ttymode_array[tty_modes->num_opcodes - 1].value = value; 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; return (tty_modes->num_opcodes * 5) + 1;
} }
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) {
/* 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* temp = malloc(data_size); char* temp = malloc(data_size);
/* Loop through the array based on number of tracked /* Loop through the array based on number of tracked

View File

@ -61,20 +61,47 @@ typedef struct guac_ssh_ttymodes {
* Initialize an empty guac_ssh_ttymodes data structure, * Initialize an empty guac_ssh_ttymodes data structure,
* with a null array of guac_ssh_ttymode and opcodes * with a null array of guac_ssh_ttymode and opcodes
* set to zero. * 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 * Add an item to the opcode array. This resizes the
* array, increments the number of opcodes, and adds * array, increments the number of opcodes, and adds
* the specified opcode and value pair to the data * the specified opcode and value pair to the data
* structure. * 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); void guac_ssh_ttymodes_add(guac_ssh_ttymodes *tty_modes, char opcode, uint32_t value);
/** /**
* Retrieve the size, in bytes, of the ttymode_array * Retrieve the size, in bytes, of the ttymode_array
* in the given guac_ssh_ttymodes data structure. * 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); 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 * Convert the ttymodes data structure into a char
* pointer array suitable for passing into the * pointer array suitable for passing into the
* libssh2_channel_request_pty_ex() function. * 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 #endif