ft_memmove: do not try to copy to or from null

This commit is contained in:
Khaïs COLIN 2024-10-22 12:15:48 +02:00
parent 34a548bd78
commit bf690a960c

View file

@ -6,11 +6,12 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdlib.h>
/*
* 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 <string.h>
#include <stdio.h>
#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));
}
*/