Prevent possible memmory leak in misc module
authorTomas Mlcoch <tmlcoch@redhat.com>
Thu, 12 Jan 2012 09:22:43 +0000 (10:22 +0100)
committerTomas Mlcoch <tmlcoch@redhat.com>
Thu, 12 Jan 2012 09:22:43 +0000 (10:22 +0100)
misc.c
misc.h

diff --git a/misc.c b/misc.c
index 732f388..b994465 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -122,28 +122,56 @@ struct VersionStruct string_to_version(const char *string, GStringChunk *chunk)
 }
 
 
-GRegex *pri_re_1 = NULL;
-GRegex *pri_re_2 = NULL;
-GRegex *pri_re_3 = NULL;
 
-int is_primary(const char *filename)
+struct PrimaryReStruct new_optimalized_primary_files_re()
 {
-    // Regex compilation
-    if (!pri_re_1) {
-        GRegexMatchFlags compile_flags = G_REGEX_OPTIMIZE|G_REGEX_MATCH_ANCHORED;
+    struct PrimaryReStruct res;
+    GRegexMatchFlags compile_flags = G_REGEX_OPTIMIZE|G_REGEX_MATCH_ANCHORED;
+    res.pri_re_1 = g_regex_new(".*bin/.*", compile_flags, 0, NULL);
+    res.pri_re_2 = g_regex_new("/etc/.*", compile_flags, 0, NULL);
+    res.pri_re_3 = g_regex_new("/usr/lib/sendmail$", compile_flags, 0, NULL);
+    return res;
+}
+
+void free_optimalized_primary_files_re(struct PrimaryReStruct in) {
+    g_regex_unref(in.pri_re_1);
+    g_regex_unref(in.pri_re_2);
+    g_regex_unref(in.pri_re_3);
+}
+
+int is_primary(const char *filename, struct PrimaryReStruct *user_re)
+{
+
+    GRegex *pri_re_1 = NULL;
+    GRegex *pri_re_2 = NULL;
+    GRegex *pri_re_3 = NULL;
+
+    if (!user_re) {
+        GRegexMatchFlags compile_flags = G_REGEX_MATCH_ANCHORED;
         pri_re_1 = g_regex_new(".*bin/.*", compile_flags, 0, NULL);
         pri_re_2 = g_regex_new("/etc/.*", compile_flags, 0, NULL);
         pri_re_3 = g_regex_new("/usr/lib/sendmail$", compile_flags, 0, NULL);
+    } else {
+        pri_re_1 = user_re->pri_re_1;
+        pri_re_2 = user_re->pri_re_2;
+        pri_re_3 = user_re->pri_re_3;
     }
 
+    int ret = 0;
     if (g_regex_match(pri_re_1, filename, 0, NULL)
         || g_regex_match(pri_re_2, filename, 0, NULL)
         || g_regex_match(pri_re_3, filename, 0, NULL))
     {
-        return 1;
+        ret = 1;
+    }
+
+    if (!user_re) {
+        g_regex_unref(pri_re_1);
+        g_regex_unref(pri_re_2);
+        g_regex_unref(pri_re_3);
     }
 
-    return 0;
+    return ret;
 }
 
 
diff --git a/misc.h b/misc.h
index 92aee65..9dced4c 100644 (file)
--- a/misc.h
+++ b/misc.h
@@ -17,7 +17,15 @@ struct VersionStruct {
  * Be so kind and don't forget use free() for all its element, before end of structure lifecycle.
  */
 struct VersionStruct string_to_version(const char *string, GStringChunk *chunk);
-int is_primary(const char *filename);
+
+struct PrimaryReStruct {
+    GRegex *pri_re_1;
+    GRegex *pri_re_2;
+    GRegex *pri_re_3;
+};
+struct PrimaryReStruct new_optimalized_primary_files_re();
+void free_optimalized_primary_files_re(struct PrimaryReStruct in);
+int is_primary(const char *filename, struct PrimaryReStruct *user_re);
 char *compute_file_checksum(const char *filename, ChecksumType type);
 
 struct HeaderRangeStruct {