diff --git a/src/common/clipboard.c b/src/common/clipboard.c index f9bf1acb..72ec263d 100644 --- a/src/common/clipboard.c +++ b/src/common/clipboard.c @@ -21,6 +21,7 @@ #include "common/clipboard.h" #include +#include #include #include #include @@ -31,11 +32,11 @@ guac_common_clipboard* guac_common_clipboard_alloc() { - guac_common_clipboard* clipboard = malloc(sizeof(guac_common_clipboard)); + guac_common_clipboard* clipboard = guac_mem_alloc(sizeof(guac_common_clipboard)); /* Init clipboard */ clipboard->mimetype[0] = '\0'; - clipboard->buffer = malloc(GUAC_COMMON_CLIPBOARD_MAX_LENGTH); + clipboard->buffer = guac_mem_alloc(GUAC_COMMON_CLIPBOARD_MAX_LENGTH); clipboard->available = GUAC_COMMON_CLIPBOARD_MAX_LENGTH; clipboard->length = 0; @@ -51,10 +52,11 @@ void guac_common_clipboard_free(guac_common_clipboard* clipboard) { pthread_mutex_destroy(&(clipboard->lock)); /* Free buffer */ - free(clipboard->buffer); + guac_mem_free(clipboard->buffer); /* Free base structure */ - free(clipboard); + guac_mem_free(clipboard); + } /** diff --git a/src/common/common/cursor.h b/src/common/common/cursor.h index 133176a7..aca24faa 100644 --- a/src/common/common/cursor.h +++ b/src/common/common/cursor.h @@ -68,7 +68,7 @@ typedef struct guac_common_cursor { /** * The size of the image data buffer, in bytes. */ - int image_buffer_size; + size_t image_buffer_size; /** * The current cursor image, if any. If the mouse cursor has not yet been diff --git a/src/common/common/string.h b/src/common/common/string.h index 442d7f49..8498cddc 100644 --- a/src/common/common/string.h +++ b/src/common/common/string.h @@ -22,24 +22,36 @@ #include "config.h" +#include + /** * Counts the number of occurrences of a given character in a string. * - * @param string The string to count occurrences within. - * @param c The character to count occurrences of. - * @return The number of occurrences. + * @param string + * The string to count occurrences within. + * + * @param c + * The character to count occurrences of. + * + * @return + * The number of occurrences. */ -int guac_count_occurrences(const char* string, char c); +size_t guac_count_occurrences(const char* string, char c); /** * Splits a string into a newly-allocated array of strings. The array itself - * and each string within the array will eventually need to be freed. The array - * is NULL-terminated. + * and each string within the array will eventually need to be freed through + * calls to guac_mem_free(). The array is NULL-terminated. * - * @param string The string to split. - * @param delim The character which separates individual substrings within the - * given string. - * @return A newly-allocated, NULL-terminated array of strings. + * @param string + * The string to split. + * + * @param delim + * The character which separates individual substrings within the + * given string. + * + * @return + * A newly-allocated, NULL-terminated array of strings. */ char** guac_split(const char* string, char delim); diff --git a/src/common/cursor.c b/src/common/cursor.c index 881ea15b..2a1650fd 100644 --- a/src/common/cursor.c +++ b/src/common/cursor.c @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -48,7 +49,7 @@ */ guac_common_cursor* guac_common_cursor_alloc(guac_client* client) { - guac_common_cursor* cursor = malloc(sizeof(guac_common_cursor)); + guac_common_cursor* cursor = guac_mem_alloc(sizeof(guac_common_cursor)); if (cursor == NULL) return NULL; @@ -58,7 +59,7 @@ guac_common_cursor* guac_common_cursor_alloc(guac_client* client) { /* Allocate initial image buffer */ cursor->image_buffer_size = GUAC_COMMON_CURSOR_DEFAULT_SIZE; - cursor->image_buffer = malloc(cursor->image_buffer_size); + cursor->image_buffer = guac_mem_alloc(cursor->image_buffer_size); /* No cursor image yet */ cursor->width = 0; @@ -90,7 +91,7 @@ void guac_common_cursor_free(guac_common_cursor* cursor) { cairo_surface_t* surface = cursor->surface; /* Free image buffer and surface */ - free(cursor->image_buffer); + guac_mem_free(cursor->image_buffer); if (surface != NULL) cairo_surface_destroy(surface); @@ -100,7 +101,7 @@ void guac_common_cursor_free(guac_common_cursor* cursor) { /* Return buffer to pool */ guac_client_free_buffer(client, buffer); - free(cursor); + guac_mem_free(cursor); } @@ -206,17 +207,17 @@ void guac_common_cursor_update(guac_common_cursor* cursor, guac_user* user, static void guac_common_cursor_resize(guac_common_cursor* cursor, int width, int height, int stride) { - int minimum_size = height * stride; + size_t minimum_size = guac_mem_ckd_mul_or_die(height, stride); /* Grow image buffer if necessary */ if (cursor->image_buffer_size < minimum_size) { /* Calculate new size */ - cursor->image_buffer_size = minimum_size*2; + cursor->image_buffer_size = guac_mem_ckd_mul_or_die(minimum_size, 2); /* Destructively reallocate image buffer */ - free(cursor->image_buffer); - cursor->image_buffer = malloc(cursor->image_buffer_size); + guac_mem_free(cursor->image_buffer); + cursor->image_buffer = guac_mem_alloc(cursor->image_buffer_size); } diff --git a/src/common/display.c b/src/common/display.c index 0fddb9b0..9e884430 100644 --- a/src/common/display.c +++ b/src/common/display.c @@ -22,6 +22,7 @@ #include "common/surface.h" #include +#include #include #include @@ -92,7 +93,7 @@ static void guac_common_display_free_layers(guac_common_display_layer* layers, guac_client_free_layer(client, layer); /* Free current element and advance to next */ - free(current); + guac_mem_free(current); current = next; } @@ -119,14 +120,14 @@ guac_common_display* guac_common_display_alloc(guac_client* client, int width, int height) { /* Allocate display */ - guac_common_display* display = malloc(sizeof(guac_common_display)); + guac_common_display* display = guac_mem_alloc(sizeof(guac_common_display)); if (display == NULL) return NULL; /* Allocate shared cursor */ display->cursor = guac_common_cursor_alloc(client); if (display->cursor == NULL) { - free(display); + guac_mem_free(display); return NULL; } @@ -159,7 +160,7 @@ void guac_common_display_free(guac_common_display* display) { guac_common_display_free_layers(display->layers, display->client); pthread_mutex_destroy(&display->_lock); - free(display); + guac_mem_free(display); } @@ -252,7 +253,7 @@ static guac_common_display_layer* guac_common_display_add_layer( guac_common_display_layer* old_head = *head; guac_common_display_layer* display_layer = - malloc(sizeof(guac_common_display_layer)); + guac_mem_alloc(sizeof(guac_common_display_layer)); /* Init layer/surface pair */ display_layer->layer = layer; @@ -361,7 +362,7 @@ void guac_common_display_free_layer(guac_common_display* display, guac_client_free_layer(display->client, display_layer->layer); /* Free list element */ - free(display_layer); + guac_mem_free(display_layer); pthread_mutex_unlock(&display->_lock); @@ -380,7 +381,7 @@ void guac_common_display_free_buffer(guac_common_display* display, guac_client_free_buffer(display->client, display_buffer->layer); /* Free list element */ - free(display_buffer); + guac_mem_free(display_buffer); pthread_mutex_unlock(&display->_lock); diff --git a/src/common/list.c b/src/common/list.c index 4138d3cc..f1012437 100644 --- a/src/common/list.c +++ b/src/common/list.c @@ -20,12 +20,14 @@ #include "config.h" #include "common/list.h" +#include + #include #include guac_common_list* guac_common_list_alloc() { - guac_common_list* list = malloc(sizeof(guac_common_list)); + guac_common_list* list = guac_mem_alloc(sizeof(guac_common_list)); pthread_mutex_init(&list->_lock, NULL); list->head = NULL; @@ -47,12 +49,12 @@ void guac_common_list_free( if (free_element_handler != NULL) free_element_handler(element->data); - free(element); + guac_mem_free(element); element = next; } /* Free the list itself */ - free(list); + guac_mem_free(list); } @@ -61,7 +63,7 @@ guac_common_list_element* guac_common_list_add(guac_common_list* list, /* Allocate element, initialize as new head */ guac_common_list_element* element = - malloc(sizeof(guac_common_list_element)); + guac_mem_alloc(sizeof(guac_common_list_element)); element->data = data; element->next = list->head; element->_ptr = &(list->head); @@ -85,7 +87,7 @@ void guac_common_list_remove(guac_common_list* list, if (element->next != NULL) element->next->_ptr = element->_ptr; - free(element); + guac_mem_free(element); } diff --git a/src/common/string.c b/src/common/string.c index b2479998..87bfff5c 100644 --- a/src/common/string.c +++ b/src/common/string.c @@ -21,18 +21,20 @@ #include "common/string.h" +#include + #include #include -int guac_count_occurrences(const char* string, char c) { +size_t guac_count_occurrences(const char* string, char c) { - int count = 0; + size_t count = 0; while (*string != 0) { /* Count each occurrence */ if (*string == c) - count++; + count = guac_mem_ckd_add_or_die(count, 1); /* Next character */ string++; @@ -45,17 +47,18 @@ int guac_count_occurrences(const char* string, char c) { char** guac_split(const char* string, char delim) { - int i = 0; + size_t i = 0; - int token_count = guac_count_occurrences(string, delim) + 1; + /* Calculate number of tokens present based on number of delimiters */ + size_t token_count = guac_mem_ckd_add_or_die(guac_count_occurrences(string, delim), 1); const char* token_start = string; - /* Allocate space for tokens */ - char** tokens = malloc(sizeof(char*) * (token_count+1)); + /* Allocate space for tokens, including NULL terminator */ + char** tokens = guac_mem_alloc(sizeof(char*), guac_mem_ckd_add_or_die(token_count, 1)); do { - int length; + size_t length; char* token; /* Find end of token */ @@ -66,7 +69,7 @@ char** guac_split(const char* string, char delim) { length = string - token_start; /* Allocate space for token and NULL terminator */ - tokens[i++] = token = malloc(length + 1); + tokens[i++] = token = guac_mem_alloc(guac_mem_ckd_add_or_die(length, 1)); /* Copy token, store null */ memcpy(token, token_start, length); diff --git a/src/common/surface.c b/src/common/surface.c index e5d1ca9f..9334f0ce 100644 --- a/src/common/surface.c +++ b/src/common/surface.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -396,7 +397,7 @@ static unsigned int __guac_common_surface_calculate_framerate( int x, y; /* Calculate heat map dimensions */ - int heat_width = GUAC_COMMON_SURFACE_HEAT_DIMENSION(surface->width); + size_t heat_width = GUAC_COMMON_SURFACE_HEAT_DIMENSION(surface->width); /* Calculate minimum X/Y coordinates intersecting given rect */ int min_x = rect->x / GUAC_COMMON_SURFACE_HEAT_CELL_SIZE; @@ -615,7 +616,7 @@ static void __guac_common_surface_touch_rect(guac_common_surface* surface, int x, y; /* Calculate heat map dimensions */ - int heat_width = GUAC_COMMON_SURFACE_HEAT_DIMENSION(surface->width); + size_t heat_width = GUAC_COMMON_SURFACE_HEAT_DIMENSION(surface->width); /* Calculate minimum X/Y coordinates intersecting given rect */ int min_x = rect->x / GUAC_COMMON_SURFACE_HEAT_CELL_SIZE; @@ -1227,11 +1228,11 @@ guac_common_surface* guac_common_surface_alloc(guac_client* client, guac_socket* socket, const guac_layer* layer, int w, int h) { /* Calculate heat map dimensions */ - int heat_width = GUAC_COMMON_SURFACE_HEAT_DIMENSION(w); - int heat_height = GUAC_COMMON_SURFACE_HEAT_DIMENSION(h); + size_t heat_width = GUAC_COMMON_SURFACE_HEAT_DIMENSION(w); + size_t heat_height = GUAC_COMMON_SURFACE_HEAT_DIMENSION(h); /* Init surface */ - guac_common_surface* surface = calloc(1, sizeof(guac_common_surface)); + guac_common_surface* surface = guac_mem_zalloc(sizeof(guac_common_surface)); surface->client = client; surface->socket = socket; surface->layer = layer; @@ -1244,10 +1245,10 @@ guac_common_surface* guac_common_surface_alloc(guac_client* client, /* Create corresponding Cairo surface */ surface->stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, w); - surface->buffer = calloc(h, surface->stride); + surface->buffer = guac_mem_zalloc(h, surface->stride); /* Create corresponding heat map */ - surface->heat_map = calloc(heat_width * heat_height, + surface->heat_map = guac_mem_zalloc(heat_width, heat_height, sizeof(guac_common_surface_heat_cell)); /* Reset clipping rect */ @@ -1274,9 +1275,9 @@ void guac_common_surface_free(guac_common_surface* surface) { pthread_mutex_destroy(&surface->_lock); - free(surface->heat_map); - free(surface->buffer); - free(surface); + guac_mem_free(surface->heat_map); + guac_mem_free(surface->buffer); + guac_mem_free(surface); } @@ -1299,8 +1300,8 @@ void guac_common_surface_resize(guac_common_surface* surface, int w, int h) { int sy = 0; /* Calculate heat map dimensions */ - int heat_width = GUAC_COMMON_SURFACE_HEAT_DIMENSION(w); - int heat_height = GUAC_COMMON_SURFACE_HEAT_DIMENSION(h); + size_t heat_width = GUAC_COMMON_SURFACE_HEAT_DIMENSION(w); + size_t heat_height = GUAC_COMMON_SURFACE_HEAT_DIMENSION(h); /* Copy old surface data */ old_buffer = surface->buffer; @@ -1311,7 +1312,7 @@ void guac_common_surface_resize(guac_common_surface* surface, int w, int h) { surface->width = w; surface->height = h; surface->stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, w); - surface->buffer = calloc(h, surface->stride); + surface->buffer = guac_mem_zalloc(h, surface->stride); __guac_common_bound_rect(surface, &surface->clip_rect, NULL, NULL); /* Copy relevant old data */ @@ -1319,11 +1320,11 @@ void guac_common_surface_resize(guac_common_surface* surface, int w, int h) { __guac_common_surface_put(old_buffer, old_stride, &sx, &sy, surface, &old_rect, 1); /* Free old data */ - free(old_buffer); + guac_mem_free(old_buffer); /* Allocate completely new heat map (can safely discard old stats) */ - free(surface->heat_map); - surface->heat_map = calloc(heat_width * heat_height, + guac_mem_free(surface->heat_map); + surface->heat_map = guac_mem_alloc(heat_width, heat_height, sizeof(guac_common_surface_heat_cell)); /* Resize dirty rect to fit new surface dimensions */ diff --git a/src/common/tests/Makefile.am b/src/common/tests/Makefile.am index 526577b2..4c26f1cf 100644 --- a/src/common/tests/Makefile.am +++ b/src/common/tests/Makefile.am @@ -50,7 +50,8 @@ test_common_SOURCES = \ test_common_CFLAGS = \ -Werror -Wall -pedantic \ - @COMMON_INCLUDE@ + @COMMON_INCLUDE@ \ + @LIBGUAC_INCLUDE@ test_common_LDADD = \ @COMMON_LTLIB@ \ diff --git a/src/common/tests/string/split.c b/src/common/tests/string/split.c index 36f8f191..d7740188 100644 --- a/src/common/tests/string/split.c +++ b/src/common/tests/string/split.c @@ -20,6 +20,7 @@ #include "common/string.h" #include +#include #include @@ -52,12 +53,12 @@ void test_string__split() { CU_ASSERT_PTR_NULL(tokens[5]); /* Clean up */ - free(tokens[0]); - free(tokens[1]); - free(tokens[2]); - free(tokens[3]); - free(tokens[4]); - free(tokens); + guac_mem_free(tokens[0]); + guac_mem_free(tokens[1]); + guac_mem_free(tokens[2]); + guac_mem_free(tokens[3]); + guac_mem_free(tokens[4]); + guac_mem_free(tokens); }