fix(ft_substr): size_t overflow + logic error
This commit is contained in:
parent
d068a9444b
commit
b2a11a2283
1 changed files with 5 additions and 3 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/17 11:45:52 by kcolin #+# #+# */
|
||||
/* Updated: 2024/10/19 18:34:36 by kcolin ### ########.fr */
|
||||
/* Updated: 2024/10/23 10:46:53 by kcolin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -38,21 +38,23 @@ char *ft_substr(char const *s, unsigned int start, size_t len)
|
|||
}
|
||||
if (i < start)
|
||||
return (ft_calloc(1, sizeof(char)));
|
||||
out = ft_calloc(min(substr_len + 1, len + 1), sizeof(char));
|
||||
out = ft_calloc(min(substr_len, len) + 1, sizeof(char));
|
||||
if (out == NULL)
|
||||
return (NULL);
|
||||
ft_strlcpy(out, s + start, len + 1);
|
||||
ft_strlcpy(out, s + start, min(substr_len, len) + 1);
|
||||
return (out);
|
||||
}
|
||||
|
||||
/*
|
||||
#include <stdio.h> // BAD
|
||||
#include <stdint.h> // BAD
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char *data = "This is a long test string.";
|
||||
|
||||
printf("'%s'\n", ft_substr(data, 0, 4));
|
||||
printf("'%s'\n", ft_substr(data, 0, SIZE_MAX));
|
||||
printf("'%s'\n", ft_substr(data, 15, 4));
|
||||
printf("'%s'\n", ft_substr(data, 15, 0));
|
||||
printf("'%s'\n", ft_substr(data, 20, 20));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue