[Forensics-changes] [yara] 337/415: Improve error handling

Hilko Bengen bengen at moszumanska.debian.org
Thu Apr 3 05:43:21 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 e48fecdac168474d31beaa129d431a694302dab2
Author: Victor M. Alvarez <plusvic at gmail.com>
Date:   Mon Dec 23 11:56:11 2013 +0100

    Improve error handling
---
 threading.c | 14 ++++++++------
 yara.c      | 55 ++++++++++++++++++++++++++++++-------------------------
 yarac.c     | 18 +++++++++++++++---
 3 files changed, 53 insertions(+), 34 deletions(-)

diff --git a/threading.c b/threading.c
index 24430a6..5dded71 100644
--- a/threading.c
+++ b/threading.c
@@ -63,7 +63,7 @@ void mutex_unlock(
 
 
 void semaphore_init(
-    SEMAPHORE* semaphore, 
+    SEMAPHORE* semaphore,
     int value)
 {
   #ifdef WIN32
@@ -72,7 +72,7 @@ void semaphore_init(
   // Mac OS X doesn't support unnamed semaphores via sem_init, that's why
   // we use sem_open instead sem_init and immediately unlink the semaphore
   // from the name. More info at:
-  // 
+  //
   // http://stackoverflow.com/questions/1413785/sem-init-on-os-x
   *semaphore = sem_open("/semaphore", O_CREAT, S_IRUSR, value);
   sem_unlink("/semaphore");
@@ -114,17 +114,19 @@ void semaphore_release(
 
 
 int create_thread(
-    THREAD* thread, 
+    THREAD* thread,
     THREAD_START_ROUTINE start_routine,
     void* param)
 {
   #ifdef WIN32
   *thread = CreateThread(NULL, 0, start_routine, param, 0, NULL);
+  if (*thread == NULL)
+    return GetLastError();
+  else
+    return 0;
   #else
-  pthread_create(thread, NULL, start_routine, param);
+  return pthread_create(thread, NULL, start_routine, param);
   #endif
-
-  return 0;
 }
 
 
diff --git a/yara.c b/yara.c
index 6a67bc5..8c85079 100644
--- a/yara.c
+++ b/yara.c
@@ -363,7 +363,7 @@ void print_hex_string(
 }
 
 
-void print_scanning_error(int error)
+void print_scanner_error(int error)
 {
   switch (error)
   {
@@ -548,13 +548,9 @@ int handle_message(int message, YR_RULE* rule, void* data)
             printf("0x%" PRIx64 ":%s: ", match->offset, string->identifier);
 
             if (STRING_IS_HEX(string))
-            {
               print_hex_string(match->data, match->length);
-            }
             else
-            {
               print_string(match->data, match->length);
-            }
 
             match = match->next;
           }
@@ -615,7 +611,7 @@ void* scanning_thread(void* param)
     {
       mutex_lock(&output_mutex);
       fprintf(stderr, "Error scanning %s: ", file_path);
-      print_scanning_error(result);
+      print_scanner_error(result);
       mutex_unlock(&output_mutex);
     }
 
@@ -880,7 +876,7 @@ int main(
   if (result == ERROR_UNSUPPORTED_FILE_VERSION ||
       result == ERROR_CORRUPT_FILE)
   {
-    print_scanning_error(result);
+    print_scanner_error(result);
     yr_finalize();
     cleanup();
     return EXIT_FAILURE;
@@ -960,29 +956,35 @@ int main(
     compiler->error_report_function = print_compiler_error;
     rule_file = fopen(argv[optind], "r");
 
-    if (rule_file != NULL)
+    if (rule_file == NULL)
     {
-      yr_compiler_push_file_name(compiler, argv[optind]);
+      fprintf(stderr, "could not open file: %s\n", argv[optind]);
+      yr_compiler_destroy(compiler);
+      yr_finalize();
+      cleanup();
+      return EXIT_FAILURE;
+    }
 
-      errors = yr_compiler_add_file(compiler, rule_file, NULL);
+    yr_compiler_push_file_name(compiler, argv[optind]);
 
-      fclose(rule_file);
+    errors = yr_compiler_add_file(compiler, rule_file, NULL);
 
-      if (errors == 0)
-        yr_compiler_get_rules(compiler, &rules);
+    fclose(rule_file);
 
+    if (errors > 0)
+    {
       yr_compiler_destroy(compiler);
-
-      if (errors > 0)
-      {
-        yr_finalize();
-        cleanup();
-        return EXIT_FAILURE;
-      }
+      yr_finalize();
+      cleanup();
+      return EXIT_FAILURE;
     }
-    else
+
+    result = yr_compiler_get_rules(compiler, &rules);
+
+    yr_compiler_destroy(compiler);
+
+    if (result != ERROR_SUCCESS)
     {
-      fprintf(stderr, "could not open file: %s\n", argv[optind]);
       yr_finalize();
       cleanup();
       return EXIT_FAILURE;
@@ -1003,7 +1005,7 @@ int main(
         timeout);
 
     if (result != ERROR_SUCCESS)
-      print_scanning_error(result);
+      print_scanner_error(result);
   }
   else if (is_directory(argv[argc - 1]))
   {
@@ -1012,7 +1014,10 @@ int main(
     for (i = 0; i < threads; i++)
     {
       if (create_thread(&thread[i], scanning_thread, (void*) rules) != 0)
-        return ERROR_COULD_NOT_CREATE_THREAD;
+      {
+        print_scanner_error(ERROR_COULD_NOT_CREATE_THREAD);
+        return EXIT_FAILURE;
+      }
     }
 
     scan_dir(
@@ -1042,7 +1047,7 @@ int main(
     if (result != ERROR_SUCCESS)
     {
       fprintf(stderr, "Error scanning %s: ", argv[argc - 1]);
-      print_scanning_error(result);
+      print_scanner_error(result);
     }
   }
 
diff --git a/yarac.c b/yarac.c
index 4256d97..845d8b0 100644
--- a/yarac.c
+++ b/yarac.c
@@ -165,7 +165,7 @@ int main(
     int argc,
     char const* argv[])
 {
-  int i, errors;
+  int i, result, errors;
 
   YR_COMPILER* compiler;
   YR_RULES* rules;
@@ -221,9 +221,21 @@ int main(
     }
   }
 
-  yr_compiler_get_rules(compiler, &rules);
+  result = yr_compiler_get_rules(compiler, &rules);
 
-  yr_rules_save(rules, argv[argc - 1]);
+  if (result != ERROR_SUCCESS)
+  {
+    fprintf(stderr, "error: %d\n", result);
+    return EXIT_FAILURE;
+  }
+
+  result = yr_rules_save(rules, argv[argc - 1]);
+
+  if (result != ERROR_SUCCESS)
+  {
+    fprintf(stderr, "error: %d\n", result);
+    return EXIT_FAILURE;
+  }
 
   yr_rules_destroy(rules);
   yr_compiler_destroy(compiler);

-- 
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