Skip to content

Instantly share code, notes, and snippets.

@iii-i
Last active August 4, 2023 23:31
Show Gist options
  • Save iii-i/c425800e75796eae65660491ac511356 to your computer and use it in GitHub Desktop.
Save iii-i/c425800e75796eae65660491ac511356 to your computer and use it in GitHub Desktop.
strstr_fuzz.c
#include <assert.h>
#include <dlfcn.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
static char *strstr_simple(const char *haystack, const char *needle) {
/*
* This function return a pointer to the beginning of the located substring,
* or NULL if the substring is not found. If needle is the empty string, the
* return value is always haystack itself.
*/
int i, j;
if (needle == NULL || haystack == NULL) {
return NULL;
}
if (needle[0] == 0) {
return (char *)haystack;
}
for (i = 0; haystack[i] != 0; i++) {
for (j = 0; haystack[i + j] != 0 && needle[j] != 0; j++) {
if (needle[j] != haystack[i + j]) {
break;
}
}
if (needle[j] == 0) {
return (char *)haystack + i;
}
}
return NULL;
}
static char *(*strstr_ptr)(const char *haystack, const char *needle);
__attribute__((constructor)) static void init(void) {
void *libc = dlopen("libc.so.6", RTLD_NOW);
strstr_ptr = dlsym(libc, "strstr");
}
int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) {
char *s = malloc(len + 2);
assert(s);
memcpy(s, buf, len);
s[len] = 0;
s[len + 1] = 0;
s[len / 2] = 0;
assert(strstr_simple(s, &s[len / 2 + 1]) == strstr_ptr(s, &s[len / 2 + 1]));
free(s);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment