Skip to content

Instantly share code, notes, and snippets.

@dequis
Created February 11, 2017 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dequis/14fbf3600ebaa3d40b11175d701d48a3 to your computer and use it in GitHub Desktop.
Save dequis/14fbf3600ebaa3d40b11175d701d48a3 to your computer and use it in GitHub Desktop.
Fuzzing test cases for GRegex/posix regexps
/*
$ gcc $(pkg-config --cflags glib-2.0) -lpthread gregex.c libglib-2.0.a libpcre.a -o gregex
$ echo -n 'pattern\ntext' | ./gregex
*/
#include <glib.h>
#include <string.h>
int main() {
GRegex *preg;
GMatchInfo *match_info;
char *str;
char *p;
if (!g_file_get_contents("/dev/stdin", &str, NULL, NULL)) {
return 1;
}
if (!(p = strchr(str, '\n')) || p == str || !p[1]) {
return 2;
}
*p = 0;
GRegexCompileFlags flags = 0; //G_REGEX_OPTIMIZE;
if (!g_utf8_validate(str, -1, NULL)) {
g_print("invalid utf8\n");
flags |= G_REGEX_RAW;
}
if (!g_utf8_validate(p + 1, -1, NULL)) {
g_print("invalid utf8 in text\n");
flags |= G_REGEX_RAW;
}
if (!(preg = g_regex_new(str, flags, 0, NULL))) {
return 3;
}
if (!g_regex_match(preg, p + 1, 0, &match_info)) {
return 4;
}
while (g_match_info_matches(match_info)) {
gchar *word = g_match_info_fetch(match_info, 0);
g_print("Found: %s\n", word);
g_free(word);
g_match_info_next(match_info, NULL);
}
g_match_info_free(match_info);
g_regex_unref(preg);
g_free(str);
return 0;
}
/*
$ gcc $(pkg-config --cflags glib-2.0) -lpthread posix.c libglib-2.0.a -o posix
$ echo -n 'pattern\ntext' | ./posix
*/
#include <glib.h>
#include <regex.h>
#include <string.h>
#include <locale.h>
int main() {
regex_t preg;
char *str;
char *p;
setlocale(LC_ALL, "");
if (!g_file_get_contents("/dev/stdin", &str, NULL, NULL)) {
return 1;
}
if (!(p = strchr(str, '\n')) || p == str || !p[1]) {
return 2;
}
*p = 0;
int flags = REG_EXTENDED | REG_NOSUB;
if (regcomp(&preg, str, flags) != 0) {
return 3;
}
if (regexec(&preg, p + 1, 0, NULL, 0) != 0) {
return 4;
}
regfree(&preg);
g_free(str);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment