// strstr requires null-terminated strings as arguments. // This sample calls strstr with a needle that matches the beginning // of an unterminated haystack string, to investigate what happens with // the libc on various operating systems. #include #include #include #include #include int main (int argc, char *argv[]) { char *buf, *p; char *ebuf; int i; int pid; const size_t bufsize = 4096; pid = getpid(); printf("My pid = %d\n", pid); buf = mmap(NULL, bufsize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); if (buf == NULL) err(1, "mmap"); printf("mmap buf=%p\n", buf); memset(buf, 0xff, bufsize); ebuf = buf + bufsize; printf("ebuf = %p\n", ebuf); buf = buf + bufsize - 16; strncpy(buf, "hello world", 11); printf("About to strstr\n"); p = strstr(buf, "hello"); printf("strstr p=%p\n", p); return 0; }