MCPcopy Create free account
hub / github.com/cgsecurity/testdisk / td_memmem

Function td_memmem

src/memmem.h:35–68  ·  view source on GitHub ↗

@ @ requires \valid_read((const char *)haystack+(0..haystack_len-1)); @ requires \valid_read((const char *)needle+(0..needle_len-1)); @ assigns \nothing; @ ensures result_null_or_in_haystack: @ \result == \null @ || (\subset((char *)\result, (char *)haystack+(0..haystack_len-needle_len)) && \valid_read((char *)\result)); @*/

Source from the content-addressed store, hash-verified

33 @ || (\subset((char *)\result, (char *)haystack+(0..haystack_len-needle_len)) && \valid_read((char *)\result));
34 @*/
35static inline const void *td_memmem(const void *haystack, const unsigned int haystack_len, const void *needle, const unsigned int needle_len)
36{
37 const char *begin;
38 const char *const last_possible = (const char *) haystack + haystack_len - needle_len;
39
40 if (needle_len == 0)
41 /* The first occurrence of the empty string is deemed to occur at
42 the beginning of the string. */
43 /*@ assert (\subset((char *)haystack, (char *)haystack+(0..haystack_len-needle_len)) && \valid_read((char *)haystack)); */
44 return (const void *) haystack;
45
46 /* Sanity check, otherwise the loop might search through the whole
47 memory. */
48 if (haystack_len < needle_len)
49 return NULL;
50
51 /*@
52 @ loop invariant \valid_read(begin);
53 @ loop invariant \subset(begin, (char *)haystack+(0..haystack_len-needle_len+1));
54 @ loop assigns begin;
55 @*/
56 for (begin = (const char *) haystack; begin <= last_possible; ++begin)
57 {
58 if (begin[0] == ((const char *) needle)[0] &&
59 !memcmp ((const void *) &begin[1],
60 (const void *) ((const char *) needle + 1),
61 needle_len - 1))
62 {
63 /*@ assert (\subset(begin, (char *)haystack+(0..haystack_len-needle_len)) && \valid_read(begin)); */
64 return (const void *) begin;
65 }
66 }
67 return NULL;
68}
69#endif

Callers 10

header_check_fobFunction · 0.85
header_check_dbnFunction · 0.85
read_pdf_fileFunction · 0.85
header_check_pdfFunction · 0.85
header_check_docFunction · 0.85
header_check_perlmFunction · 0.85
header_check_snzFunction · 0.85
header_check_stlFunction · 0.85
header_check_txtFunction · 0.85
ifFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected