I have the following function:
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *substr;
if (!s)
return (NULL);
if (start > ft_strlen(s))
len = 0;
if (start <= ft_strlen(s) && s[start] != '\0')
{
if (ft_strlen(s + start) < len)
len = ft_strlen(s + start);
}
substr = (char *)malloc(sizeof(*s) * (len + 1));
if (!substr)
return (NULL);
if (len == 0)
return (substr);
ft_strlcpy(substr, s + start, len + 1);
return (substr);
}
Some of the functions are self-made (strlen = ft_strlen etc).
The problem: if I pass in -1 for "start", my program segfaults because it is converted to 4.294.967.295. So it crashes when passed in that is my function body does not even execute making all protective measures useless. Even stranger, if I execute this on my Mac everything works fine, on Ubuntu 22.04 it does not.
Any suggestions? I am not allowed to change the function prototype.
Edit: I have to pass a series of tests and the following one I am failing constantly:
Error in test 4: ft_substr("hola", 4294967295, 0): Segmentation fault!
(The test actually inputs -1).
Also, I am not allowed to include a main that tests user input.
Edit 2: My main is simple and just calls the function with input "hola", -1, 3