From bf690a960c7d8dacba9d93587dd4435d73a4fb0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 22 Oct 2024 12:15:48 +0200 Subject: [PATCH] ft_memmove: do not try to copy to or from null --- ft_memmove.c | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/ft_memmove.c b/ft_memmove.c index 2ff700e..8be0b60 100644 --- a/ft_memmove.c +++ b/ft_memmove.c @@ -6,11 +6,12 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/15 13:57:12 by kcolin #+# #+# */ -/* Updated: 2024/10/16 15:18:46 by kcolin ### ########.fr */ +/* Updated: 2024/10/22 12:15:04 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" +#include /* * case 1: dest overlaps at start of src. @@ -29,6 +30,8 @@ void *ft_memmove(void *dest, const void *src, size_t n) { size_t i; + if ((dest == NULL || src == NULL) && n > 0) + return (dest); if (dest >= src && dest <= (src + n)) { i = n; @@ -39,8 +42,32 @@ void *ft_memmove(void *dest, const void *src, size_t n) } } else - { ft_memcpy(dest, src, n); - } return (dest); } + +/* +#include +#include +#include "libft.h" + +int main(void) +{ + char buf[27] = "abcdefghijklmnopqrstuvwxyz"; + char buf2[27] = "abcdefghijklmnopqrstuvwxyz"; + + printf("%p\n", ft_memmove(buf, buf+3, 6)); + printf("%s\n", buf); + printf("%p\n", ft_memmove(buf+10, buf+8, 6)); + printf("%s\n", buf); + printf("%p\n", ft_memmove(buf, buf+6, 6)); + printf("%s\n", buf); + printf("%p\n", memmove(buf2, buf2+3, 6)); + printf("%s\n", buf2); + printf("%p\n", memmove(buf2+10, buf2+8, 6)); + printf("%s\n", buf2); + printf("%p\n", memmove(buf2, buf2+6, 6)); + printf("%s\n", buf2); + printf("%p\n", ft_memmove(NULL, NULL, 6)); +} +*/