dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 100.0% 3 / 0 / 3
Functions: 100.0% 1 / 0 / 1
Branches: -% 0 / 2 / 2

src/util/xmemmem.c
Line Branch Exec Source
1 #include "build-defs.h"
2 #include <string.h>
3 #include "xmemmem.h"
4 #include "debug.h"
5
6 54 void *xmemmem(const void *haystack, size_t hlen, const void *needle, size_t nlen)
7 {
8 54 BUG_ON(nlen == 0);
9
10 #if HAVE_MEMMEM
11 54 return memmem(haystack, hlen, needle, nlen);
12 #endif
13
14 // Note: this fallback implementation isn't well suited to general
15 // purpose use and can exhibit poor performance under certain inputs.
16 // A library-quality memmem(3) isn't a trivial thing to implement,
17 // but fortunately almost every modern platform already has one in
18 // libc and it was added to the POSIX base spec in issue 8. Therefore,
19 // this code isn't likely to be used, and even when it is, it should
20 // be acceptable for the uses in this codebase.
21
22 const char *start = haystack;
23 int first_char = ((const unsigned char*)needle)[0];
24 if (nlen == 1) {
25 // NOLINTNEXTLINE(readability-redundant-casting)
26 return (void*)memchr(start, first_char, hlen);
27 }
28
29 while (1) {
30 // NOLINTNEXTLINE(readability-redundant-casting)
31 char *ptr = (void*)memchr(start, first_char, hlen);
32 if (!ptr) {
33 return NULL;
34 }
35 size_t skip = (size_t)(ptr - start);
36 if (skip + nlen > hlen) {
37 return NULL;
38 }
39 if (memcmp(ptr, needle, nlen) == 0) {
40 return ptr;
41 }
42 start = ptr + 1;
43 hlen -= skip + 1;
44 }
45
46 BUG("unexpected loop break");
47 return NULL;
48 }
49