GUACAMOLE-25: Avoid unnecessary allocation of temporary string for guac_rdp_ptr_to_string().

This commit is contained in:
Michael Jumper 2016-06-03 11:30:05 -07:00
parent 995b6d669a
commit c1fdbca62b
3 changed files with 17 additions and 19 deletions

View File

@ -185,9 +185,11 @@ int guac_rdp_audio_end_handler(guac_user* user, guac_stream* stream) {
void guac_rdp_audio_load_plugin(rdpContext* context, guac_rdp_dvc_list* list) { void guac_rdp_audio_load_plugin(rdpContext* context, guac_rdp_dvc_list* list) {
guac_client* client = ((rdp_freerdp_context*) context)->client; guac_client* client = ((rdp_freerdp_context*) context)->client;
char client_ref[GUAC_RDP_PTR_STRING_LENGTH];
/* Add "AUDIO_INPUT" channel */ /* Add "AUDIO_INPUT" channel */
guac_rdp_dvc_list_add(list, "guacai", guac_rdp_ptr_to_string(client), NULL); guac_rdp_ptr_to_string(client, client_ref);
guac_rdp_dvc_list_add(list, "guacai", client_ref, NULL);
} }

View File

@ -25,21 +25,11 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
/** void guac_rdp_ptr_to_string(void* data, char* str) {
* The maximum number of bytes required to represent a pointer printed using
* printf()'s "%p". This will be the size of the hex prefix ("0x"), null
* terminator, plus two bytes for every byte required by a pointer.
*/
#define GUAC_RDP_PTR_STRING_LENGTH (sizeof("0x") + (sizeof(void*) * 2))
char* guac_rdp_ptr_to_string(void* data) {
/* Convert pointer to string */ /* Convert pointer to string */
char* str = malloc(GUAC_RDP_PTR_STRING_LENGTH);
sprintf(str, "%p", data); sprintf(str, "%p", data);
return str;
} }
void* guac_rdp_string_to_ptr(const char* str) { void* guac_rdp_string_to_ptr(const char* str) {

View File

@ -24,6 +24,13 @@
#include <guacamole/client.h> #include <guacamole/client.h>
/**
* The maximum number of bytes required to represent a pointer printed using
* printf()'s "%p". This will be the size of the hex prefix ("0x"), null
* terminator, plus two bytes for every byte required by a pointer.
*/
#define GUAC_RDP_PTR_STRING_LENGTH (sizeof("0x") + (sizeof(void*) * 2))
/** /**
* Converts the given string back into a void pointer. The string MUST have * Converts the given string back into a void pointer. The string MUST have
* been produced via guac_rdp_ptr_to_string(). * been produced via guac_rdp_ptr_to_string().
@ -40,18 +47,17 @@ void* guac_rdp_string_to_ptr(const char* str);
/** /**
* Converts a void pointer into a string representation, safe for use with * Converts a void pointer into a string representation, safe for use with
* parts of the FreeRDP API which provide only for passing arbitrary strings, * parts of the FreeRDP API which provide only for passing arbitrary strings,
* despite being within the same memory area. The returned string must * despite being within the same memory area.
* eventually be freed with a call to free().
* *
* @param data * @param data
* The void pointer to convert to a string. * The void pointer to convert to a string.
* *
* @return * @param str
* A newly-allocated string containing the string representation of the * The buffer in which the string representation of the given void pointer
* given void pointer. This string must eventually be freed with a call to * should be stored. This buffer must have at least
* free(). * GUAC_RDP_PTR_STRING_LENGTH bytes available.
*/ */
char* guac_rdp_ptr_to_string(void* data); void guac_rdp_ptr_to_string(void* data, char* str);
#endif #endif