From 41a06d833c7691d7034a08406219301c7ff62097 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Sat, 6 Jul 2024 22:09:41 -0500 Subject: [PATCH] Update: Use memmove() instead of memcpy() for memory append and memory append all functions. These memory append functions do not state that the memory cannot overlap. The use of memmove() is safer in that it allows for overlapping memory. --- level_0/f_memory/c/memory/array.c | 4 ++-- level_0/f_memory/c/memory/array.h | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/level_0/f_memory/c/memory/array.c b/level_0/f_memory/c/memory/array.c index d80db1d..d513835 100644 --- a/level_0/f_memory/c/memory/array.c +++ b/level_0/f_memory/c/memory/array.c @@ -44,7 +44,7 @@ extern "C" { } // Casting (void *) to (uint8_t *) should result in an increment of size 1 and avoids problems with (void *) having arithmetic issues. - memcpy((void *) (((uint8_t *) (*array)) + (*used * width)), source, width); + memmove((void *) (((uint8_t *) (*array)) + (*used * width)), source, width); ++(*used); @@ -79,7 +79,7 @@ extern "C" { } // Casting (void *) to (uint8_t *) should result in an increment of size 1 and avoids problems with (void *) having arithmetic issues. - memcpy((void *) (((uint8_t *) (*array)) + (*used * width)), sources, width * amount); + memmove((void *) (((uint8_t *) (*array)) + (*used * width)), sources, width * amount); *used += amount; diff --git a/level_0/f_memory/c/memory/array.h b/level_0/f_memory/c/memory/array.h index 3e68c3d..d8037be 100644 --- a/level_0/f_memory/c/memory/array.h +++ b/level_0/f_memory/c/memory/array.h @@ -103,6 +103,8 @@ extern "C" { * F_array_too_large (with error bit) if the new array length is too large. * F_memory_not (with error bit) on out of memory. * F_parameter (with error bit) if a parameter is invalid. + * + * @see memmove() */ #ifndef _di_f_memory_array_append_ extern f_status_t f_memory_array_append(const void * const source, const size_t width, void ** const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size); @@ -164,6 +166,8 @@ extern "C" { * F_array_too_large (with error bit) if the new array length is too large. * F_memory_not (with error bit) on out of memory. * F_parameter (with error bit) if a parameter is invalid. + * + * @see memmove() */ #ifndef _di_f_memory_array_append_all_ extern f_status_t f_memory_array_append_all(const void * const sources, const f_number_unsigned_t amount, const size_t width, void ** const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size); -- 1.8.3.1