[Forensics-changes] [yara] 352/415: Improve error handling
Hilko Bengen
bengen at moszumanska.debian.org
Thu Apr 3 05:43:23 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 ce480357eded31db348c7ba52fa889d0d2aa21d8
Author: Victor Manuel Alvarez <vmalvarez at virustotal.com>
Date: Tue Dec 31 10:54:58 2013 +0100
Improve error handling
---
libyara/rules.c | 8 +++++++-
threading.c | 25 +++++++++++++++++++++----
threading.h | 8 ++++----
yara.c | 25 ++++++++++++++++++-------
4 files changed, 50 insertions(+), 16 deletions(-)
diff --git a/libyara/rules.c b/libyara/rules.c
index d649a60..2a16573 100644
--- a/libyara/rules.c
+++ b/libyara/rules.c
@@ -1358,8 +1358,14 @@ int yr_rules_load(
#if WIN32
new_rules->mutex = CreateMutex(NULL, FALSE, NULL);
+
+ if (new_rules->mutex == NULL)
+ return ERROR_INTERNAL_FATAL_ERROR;
#else
- pthread_mutex_init(&new_rules->mutex, NULL);
+ result = pthread_mutex_init(&new_rules->mutex, NULL);
+
+ if (result != 0)
+ return ERROR_INTERNAL_FATAL_ERROR;
#endif
rule = new_rules->rules_list_head;
diff --git a/threading.c b/threading.c
index 5dded71..2f394b7 100644
--- a/threading.c
+++ b/threading.c
@@ -16,16 +16,24 @@ limitations under the License.
#include <fcntl.h>
+#ifndef WIN32
+#include <errno.h>
+#endif
+
#include "threading.h"
-void mutex_init(
+int mutex_init(
MUTEX* mutex)
{
#ifdef WIN32
*mutex = CreateMutex(NULL, FALSE, NULL);
+ if (*mutex == NULL)
+ return GetLastError();
+ else
+ return 0;
#else
- pthread_mutex_init(mutex, NULL);
+ return pthread_mutex_init(mutex, NULL);
#endif
}
@@ -62,12 +70,16 @@ void mutex_unlock(
}
-void semaphore_init(
+int semaphore_init(
SEMAPHORE* semaphore,
int value)
{
#ifdef WIN32
*semaphore = CreateSemaphore(NULL, value, 65535, NULL);
+ if (*semaphore == NULL)
+ return GetLastError();
+ else
+ return 0;
#else
// 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
@@ -75,7 +87,12 @@ void semaphore_init(
//
// http://stackoverflow.com/questions/1413785/sem-init-on-os-x
*semaphore = sem_open("/semaphore", O_CREAT, S_IRUSR, value);
- sem_unlink("/semaphore");
+ if (*semaphore == SEM_FAILED)
+ return errno;
+ else
+ return 0;
+ if (sem_unlink("/semaphore") != 0)
+ return errno;
#endif
}
diff --git a/threading.h b/threading.h
index 1c88b70..19d4432 100644
--- a/threading.h
+++ b/threading.h
@@ -43,7 +43,7 @@ typedef void *(*THREAD_START_ROUTINE) (void *);
#endif
-void mutex_init(
+int mutex_init(
MUTEX* mutex);
void mutex_destroy(
@@ -55,8 +55,8 @@ void mutex_lock(
void mutex_unlock(
MUTEX* mutex);
-void semaphore_init(
- SEMAPHORE* semaphore,
+int semaphore_init(
+ SEMAPHORE* semaphore,
int value);
void semaphore_destroy(
@@ -69,7 +69,7 @@ void semaphore_release(
SEMAPHORE* semaphore);
int create_thread(
- THREAD* thread,
+ THREAD* thread,
THREAD_START_ROUTINE start_routine,
void* param);
diff --git a/yara.c b/yara.c
index f2d78ce..62842d6 100644
--- a/yara.c
+++ b/yara.c
@@ -159,14 +159,24 @@ MUTEX queue_mutex;
MUTEX output_mutex;
-void file_queue_init()
+int file_queue_init()
{
+ int result;
+
queue_tail = 0;
queue_head = 0;
- mutex_init(&queue_mutex);
- semaphore_init(&used_slots, 0);
- semaphore_init(&unused_slots, MAX_QUEUED_FILES);
+ result = mutex_init(&queue_mutex);
+
+ if (result != 0)
+ return result;
+
+ result = semaphore_init(&used_slots, 0);
+
+ if (result != 0)
+ return result;
+
+ return semaphore_init(&unused_slots, MAX_QUEUED_FILES);
}
@@ -880,8 +890,8 @@ int main(
result = yr_rules_load(argv[optind], &rules);
- if (result == ERROR_UNSUPPORTED_FILE_VERSION ||
- result == ERROR_CORRUPT_FILE)
+ if (result != ERROR_SUCCESS &&
+ result != ERROR_INVALID_FILE)
{
print_scanner_error(result);
yr_finalize();
@@ -1016,7 +1026,8 @@ int main(
}
else if (is_directory(argv[argc - 1]))
{
- file_queue_init();
+ if (file_queue_init() != 0)
+ print_scanner_error(ERROR_INTERNAL_FATAL_ERROR);
for (i = 0; i < threads; i++)
{
--
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