[Forensics-changes] [yara] 127/415: Fix memory leaks

Hilko Bengen bengen at moszumanska.debian.org
Thu Apr 3 05:42:54 UTC 2014


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to branch debian
in repository yara.

commit 9b9753b874368dd068ea6d731e2c8b222f2b3e74
Author: Victor M. Alvarez <plusvic at gmail.com>
Date:   Wed Apr 25 10:20:27 2012 +0000

    Fix memory leaks
---
 libyara/libyara.c         | 17 +++++++++++++++++
 yara-python/yara-python.c | 19 ++++++++++++++++---
 2 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/libyara/libyara.c b/libyara/libyara.c
index 6e734d3..9e8d619 100644
--- a/libyara/libyara.c
+++ b/libyara/libyara.c
@@ -86,6 +86,10 @@ void yr_destroy_context(YARA_CONTEXT* context)
 	NAMESPACE* next_ns;
     VARIABLE* variable;
 	VARIABLE* next_variable;
+    RULE_LIST_ENTRY* rule_list_entry;
+    RULE_LIST_ENTRY* next_rule_list_entry;
+	
+    int i;
     
     rule = context->rule_list.head;
     
@@ -194,6 +198,19 @@ void yr_destroy_context(YARA_CONTEXT* context)
         yr_pop_file_name(context);
     }
     
+    for(i = 0; i < RULE_LIST_HASH_TABLE_SIZE; i++)
+    {
+        rule_list_entry = context->rule_list.hash_table[i].next;
+        
+        while(rule_list_entry != NULL)
+        {
+            next_rule_list_entry = rule_list_entry->next;
+            yr_free(rule_list_entry);
+            
+            rule_list_entry = next_rule_list_entry;
+        }
+    }
+    
     clear_hash_table(&context->hash_table);
 	yr_free(context);
 }
diff --git a/yara-python/yara-python.c b/yara-python/yara-python.c
index 4c5bc7e..ade75e5 100644
--- a/yara-python/yara-python.c
+++ b/yara-python/yara-python.c
@@ -156,6 +156,10 @@ static PyObject * Match_NEW(const char* rule, const char* ns, PyObject* tags, Py
         object->tags = tags;
         object->meta = meta;
         object->strings = strings;
+        
+        Py_INCREF(tags);
+        Py_INCREF(meta);
+        Py_INCREF(tags);
     } 
       
     return (PyObject *)object;
@@ -397,7 +401,7 @@ static PyObject * Rules_new_from_file(FILE* file, const char* filepath, PyObject
 
 static PyObject * Rules_new_from_string(const char* string, PyObject* rules, YARA_CONTEXT* context)
 { 
-  Rules* result;
+    Rules* result;
 
     int  errors;
     int  error_line;
@@ -456,6 +460,7 @@ int yara_callback(RULE* rule, void* data)
     PyObject* match;
     PyObject* callback_dict;
     PyObject* object;
+    PyObject* tuple;
     PyObject* matches = ((CALLBACK_DATA*) data)->matches;
     PyObject* callback = ((CALLBACK_DATA*) data)->callback;
     PyObject* callback_result;
@@ -522,9 +527,13 @@ int yara_callback(RULE* rule, void* data)
 
             while (m != NULL)
             {
-                object = Py_BuildValue("(i,s,O)", m->offset, string->identifier, PyBytes_FromStringAndSize((char*) m->data, m->length));
-                PyList_Append(string_list, object);
+                object = PyBytes_FromStringAndSize((char*) m->data, m->length);
+                tuple = Py_BuildValue("(i,s,O)", m->offset, string->identifier, object);
+                
+                PyList_Append(string_list, tuple);
+                
                 Py_DECREF(object);
+                Py_DECREF(tuple);
                    
                 m = m->next;
             }
@@ -600,6 +609,10 @@ int yara_callback(RULE* rule, void* data)
         Py_DECREF(callback); 
     }
     
+  Py_DECREF(tag_list);
+  Py_DECREF(string_list);
+  Py_DECREF(meta_list);  
+    
   return result;
 
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/forensics/yara.git



More information about the forensics-changes mailing list