Skip to content

tests(strnstr): add test 15. test finding the string when it exists i…#68

Open
douglasfanucchi wants to merge 2 commits into
Tripouille:masterfrom
douglasfanucchi:master
Open

tests(strnstr): add test 15. test finding the string when it exists i…#68
douglasfanucchi wants to merge 2 commits into
Tripouille:masterfrom
douglasfanucchi:master

Conversation

@douglasfanucchi

Copy link
Copy Markdown

The following ft_strnstr implementation passes in those 14 tests but it fails when trying to find a string that is present in the haystack and the n bytes to lookup is a number greater than the length of the haystack.

#include <libft.h>

char *ft_strnstr(const char *haystack, const char *search, size_t n)
{
	size_t	i;
	size_t	j;
	size_t	len;

	i = 0;
	len = ft_strlen(search);
	if (!len && !haystack[0])
		return ((char *)haystack);
	while (i < n && haystack[i])
	{
		j = 0;
		while (i + j < n && haystack[i + j] == search[j])
			j++;
		if (j == len)
			return ((char *)haystack + i);
		i++;
	}
	return ((void *)0);
}

The test added in the pull request detects this problem in the above implementation.

…n haystack but the n bytes to lookup is a number greater than haystack length
@douglasfanucchi douglasfanucchi marked this pull request as draft June 9, 2026 14:56
@douglasfanucchi

Copy link
Copy Markdown
Author

I just found other test cases that detects problems in that implementation, I'll be adding them to this PR later.

@douglasfanucchi douglasfanucchi marked this pull request as ready for review June 10, 2026 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant