Skip to content

Instantly share code, notes, and snippets.

@dequis dequis/gregex.c
Created Feb 11, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.