diff options
Diffstat (limited to 'src/libical/icalmemory.c')
-rw-r--r-- | src/libical/icalmemory.c | 155 |
1 files changed, 120 insertions, 35 deletions
diff --git a/src/libical/icalmemory.c b/src/libical/icalmemory.c index 6c1bd9ca..2b5d0490 100644 --- a/src/libical/icalmemory.c +++ b/src/libical/icalmemory.c @@ -2,29 +2,9 @@ FILE: icalmemory.c CREATOR: eric 30 June 1999 - Copyright (C) 2000 Eric Busboom <eric@civicknowledge.com> + SPDX-FileCopyrightText: 2000 Eric Busboom <eric@civicknowledge.com> - This library is free software; you can redistribute it and/or modify - it under the terms of either: - - The LGPL as published by the Free Software Foundation, version - 2.1, available at: https://www.gnu.org/licenses/lgpl-2.1.html - - Or: - - The Mozilla Public License Version 2.0. You may obtain a copy of - the License at https://www.mozilla.org/MPL/ - - This library is free software; you can redistribute it and/or modify - it under the terms of either: - - The LGPL as published by the Free Software Foundation, version - 2.1, available at: https://www.gnu.org/licenses/lgpl-2.1.html - - Or: - - The Mozilla Public License Version 2.0. You may obtain a copy of - the License at https://www.mozilla.org/MPL/ + SPDX-License-Identifier: LGPL-2.1-only OR MPL-2.0 ======================================================================*/ #ifdef HAVE_CONFIG_H @@ -33,6 +13,9 @@ #include "icalmemory.h" #include "icalerror.h" +#if defined(MEMORY_CONSISTENCY) +#include "test-malloc.h" +#endif #include <stdlib.h> @@ -74,10 +57,10 @@ static void icalmemory_free_ring_byval(buffer_ring * br) for (i = 0; i < BUFFER_RING_SIZE; i++) { if (br->ring[i] != 0) { - free(br->ring[i]); + icalmemory_free_buffer(br->ring[i]); } } - free(br); + icalmemory_free_buffer(br); } #if defined(HAVE_PTHREAD) @@ -109,7 +92,7 @@ static void icalmemory_free_tmp_buffer(void *buf) return; } - free(buf); + icalmemory_free_buffer(buf); } #endif @@ -122,7 +105,9 @@ static buffer_ring *buffer_ring_new(void) buffer_ring *br; int i; - br = (buffer_ring *) malloc(sizeof(buffer_ring)); + br = (buffer_ring *) icalmemory_new_buffer(sizeof(buffer_ring)); + if (!br) + return NULL; for (i = 0; i < BUFFER_RING_SIZE; i++) { br->ring[i] = 0; @@ -182,6 +167,9 @@ static buffer_ring *get_buffer_ring(void) void icalmemory_add_tmp_buffer(void *buf) { buffer_ring *br = get_buffer_ring(); + if (!br) { + return; + } /* Wrap around the ring */ if (++(br->pos) == BUFFER_RING_SIZE) { @@ -190,7 +178,7 @@ void icalmemory_add_tmp_buffer(void *buf) /* Free buffers as their slots are overwritten */ if (br->ring[br->pos] != 0) { - free(br->ring[br->pos]); + icalmemory_free_buffer(br->ring[br->pos]); } /* Assign the buffer to a slot */ @@ -210,7 +198,7 @@ void *icalmemory_tmp_buffer(size_t size) size = MIN_BUFFER_SIZE; } - buf = (void *)malloc(size); + buf = (void *)icalmemory_new_buffer(size); if (buf == 0) { icalerror_set_errno(ICAL_NEWFAILED_ERROR); @@ -229,6 +217,8 @@ void icalmemory_free_ring() buffer_ring *br; br = get_buffer_ring(); + if (!br) + return; icalmemory_free_ring_byval(br); #if defined(HAVE_PTHREAD) @@ -241,7 +231,15 @@ void icalmemory_free_ring() /* Like strdup, but the buffer is on the ring. */ char *icalmemory_tmp_copy(const char *str) { - char *b = icalmemory_tmp_buffer(strlen(str) + 1); + char *b; + + if (!str) + return NULL; + + b = icalmemory_tmp_buffer(strlen(str) + 1); + + if (!b) + return NULL; strcpy(b, str); @@ -250,7 +248,67 @@ char *icalmemory_tmp_copy(const char *str) char *icalmemory_strdup(const char *s) { - return strdup(s); + size_t l; + char *res; + + if (!s) + return NULL; + + l = (strlen(s) + 1) * sizeof(char); + res = (char *) icalmemory_new_buffer(l); + if (res == NULL) + return NULL; + + memcpy(res, s, l); + + return res; +} + +#if defined(MEMORY_CONSISTENCY) +static icalmemory_malloc_f global_icalmem_malloc = &test_malloc; +#elif defined(ICALMEMORY_DEFAULT_MALLOC) && !defined(S_SPLINT_S) +static icalmemory_malloc_f global_icalmem_malloc = &ICALMEMORY_DEFAULT_MALLOC; +#else +static icalmemory_malloc_f global_icalmem_malloc = NULL; +#endif + +#if defined(MEMORY_CONSISTENCY) +static icalmemory_realloc_f global_icalmem_realloc = &test_realloc; +#elif defined(ICALMEMORY_DEFAULT_REALLOC) && !defined(S_SPLINT_S) +static icalmemory_realloc_f global_icalmem_realloc = &ICALMEMORY_DEFAULT_REALLOC; +#else +static icalmemory_realloc_f global_icalmem_realloc = NULL; +#endif + +#if defined(MEMORY_CONSISTENCY) +static icalmemory_free_f global_icalmem_free = &test_free; +#elif defined(ICALMEMORY_DEFAULT_FREE) && !defined(S_SPLINT_S) +static icalmemory_free_f global_icalmem_free = &ICALMEMORY_DEFAULT_FREE; +#else +static icalmemory_free_f global_icalmem_free = NULL; +#endif + +void icalmemory_set_mem_alloc_funcs(icalmemory_malloc_f f_malloc, + icalmemory_realloc_f f_realloc, + icalmemory_free_f f_free) +{ + global_icalmem_malloc = f_malloc; + global_icalmem_realloc = f_realloc; + global_icalmem_free = f_free; +} + +void icalmemory_get_mem_alloc_funcs(icalmemory_malloc_f *f_malloc, + icalmemory_realloc_f *f_realloc, + icalmemory_free_f *f_free) { + if (f_malloc) { + *f_malloc = global_icalmem_malloc; + } + if (f_realloc) { + *f_realloc = global_icalmem_realloc; + } + if (f_free) { + *f_free = global_icalmem_free; + } } /* @@ -260,7 +318,14 @@ char *icalmemory_strdup(const char *s) void *icalmemory_new_buffer(size_t size) { - void *b = malloc(size); + void *b; + + if (global_icalmem_malloc == NULL) { + icalerror_set_errno(ICAL_NEWFAILED_ERROR); + return 0; + } + + b = global_icalmem_malloc(size); if (b == 0) { icalerror_set_errno(ICAL_NEWFAILED_ERROR); @@ -274,7 +339,14 @@ void *icalmemory_new_buffer(size_t size) void *icalmemory_resize_buffer(void *buf, size_t size) { - void *b = realloc(buf, size); + void *b; + + if (global_icalmem_realloc == NULL) { + icalerror_set_errno(ICAL_NEWFAILED_ERROR); + return 0; + } + + b = global_icalmem_realloc(buf, size); if (b == 0) { icalerror_set_errno(ICAL_NEWFAILED_ERROR); @@ -286,7 +358,12 @@ void *icalmemory_resize_buffer(void *buf, size_t size) void icalmemory_free_buffer(void *buf) { - free(buf); + if (global_icalmem_free == NULL) { + icalerror_set_errno(ICAL_NEWFAILED_ERROR); + return; + } + + global_icalmem_free(buf); } void icalmemory_append_string(char **buf, char **pos, size_t *buf_size, const char *string) @@ -314,7 +391,11 @@ void icalmemory_append_string(char **buf, char **pos, size_t *buf_size, const ch *buf_size = (*buf_size) * 2 + final_length; - new_buf = realloc(*buf, *buf_size); + new_buf = icalmemory_resize_buffer(*buf, *buf_size); + if (!new_buf) { + // an error was set in the resize function, so we just return here. + return; + } new_pos = (void *)((size_t) new_buf + data_length); @@ -351,7 +432,11 @@ void icalmemory_append_char(char **buf, char **pos, size_t *buf_size, char ch) *buf_size = (*buf_size) * 2 + final_length + 1; - new_buf = realloc(*buf, *buf_size); + new_buf = icalmemory_resize_buffer(*buf, *buf_size); + if (!new_buf) { + // an error was set in the resize function, so we just return here. + return; + } new_pos = (void *)((size_t) new_buf + data_length); |