Code

push: fix segfault for odd config
[git.git] / compat / memmem.c
1 #include "../git-compat-util.h"
3 void *gitmemmem(const void *haystack, size_t haystack_len,
4                 const void *needle, size_t needle_len)
5 {
6         const char *begin = haystack;
7         const char *last_possible = begin + haystack_len - needle_len;
9         /*
10          * The first occurrence of the empty string is deemed to occur at
11          * the beginning of the string.
12          */
13         if (needle_len == 0)
14                 return (void *)begin;
16         /*
17          * Sanity check, otherwise the loop might search through the whole
18          * memory.
19          */
20         if (haystack_len < needle_len)
21                 return NULL;
23         for (; begin <= last_possible; begin++) {
24                 if (!memcmp(begin, needle, needle_len))
25                         return (void *)begin;
26         }
28         return NULL;
29 }