From 0cbb3a1da6038fb85810be7f334c9987c323eece Mon Sep 17 00:00:00 2001 From: Theron Spiegl Date: Wed, 30 Sep 2020 21:25:39 -0500 Subject: don't compile regex in loop --- src/attach.c | 4 ---- src/whatfiles.c | 7 +++++++ src/whatfiles.h | 2 ++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/attach.c b/src/attach.c index 7aad391..1846288 100644 --- a/src/attach.c +++ b/src/attach.c @@ -37,13 +37,9 @@ char read_status(pid_t pid) if (!h_status) return 0; read_file(str, 4096, h_status); - regex_t regex; int err; regmatch_t pmatch[2]; - if (regcomp(®ex, "State:\\W+([A-Za-z])", REG_EXTENDED) != 0) - SYS_ERR("regex compilation error"); err = regexec(®ex, str->data, 2, pmatch, 0); - regfree(®ex); if (err) { DEBUG("failed to find regex match in /proc/%d/status file\n", pid); } else { diff --git a/src/whatfiles.c b/src/whatfiles.c index fb9f291..0004308 100644 --- a/src/whatfiles.c +++ b/src/whatfiles.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -14,6 +15,7 @@ FILE *Handle = (FILE*)NULL; int Debug = 0; +regex_t regex; LastSyscall_t LastSyscall; DebugStats_t DebugStats; @@ -92,6 +94,10 @@ int main(int argc, char* argv[]) HashMap hashmap = &hm; init_hashmap(hashmap); + if (regcomp(®ex, "State:\\W+([A-Za-z])", REG_EXTENDED) != 0) { + SYS_ERR("regex compilation error"); + } + int start_of_user_command = discover_flags(argc, argv); char *user_filename = parse_flags(start_of_user_command, argv, &pid, &stdout_override, &attach); if (start_of_user_command == argc && !attach) { @@ -210,6 +216,7 @@ int main(int argc, char* argv[]) } } + regfree(®ex); err = destroy(hashmap); HASH_ERR_CHECK(err, "tried to free null pointers in hashmap.\n") fclose(Handle); diff --git a/src/whatfiles.h b/src/whatfiles.h index 4561c11..a4ea9cd 100644 --- a/src/whatfiles.h +++ b/src/whatfiles.h @@ -2,6 +2,7 @@ #define WHATFILES_H #include +#include #include #include @@ -9,6 +10,7 @@ extern int Debug; extern FILE *Handle; +extern regex_t regex; #define MODE_LEN 32 #define OUTPUT(...) fprintf(Handle, __VA_ARGS__) -- cgit v1.2.3