[Forensics-changes] [yara] 54/192: Use a union instead of a int64_t for arguments to module functions. This makes the code clearer, reduce typecasting and solve warnings.
Hilko Bengen
bengen at moszumanska.debian.org
Sat Jul 1 10:31:45 UTC 2017
This is an automated email from the git hooks/post-receive script.
bengen pushed a commit to annotated tag v3.6.0
in repository yara.
commit fc36b064096233b959b958d7cd51a2edb2f563e7
Author: plusvic <plusvic at gmail.com>
Date: Mon Oct 24 11:47:00 2016 +0200
Use a union instead of a int64_t for arguments to module functions. This makes the code clearer, reduce typecasting and solve warnings.
---
libyara/exec.c | 31 ++++++++-----------------------
libyara/include/yara/modules.h | 10 +++++-----
libyara/include/yara/types.h | 15 +++++++++++++--
3 files changed, 26 insertions(+), 30 deletions(-)
diff --git a/libyara/exec.c b/libyara/exec.c
index 1a347c0..5266b9a 100644
--- a/libyara/exec.c
+++ b/libyara/exec.c
@@ -50,17 +50,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define MEM_SIZE MAX_LOOP_NESTING * LOOP_LOCAL_VARS
-typedef union _STACK_ITEM {
-
- int64_t i;
- double d;
- void* p;
- YR_OBJECT* o;
- YR_STRING* s;
- SIZED_STRING* ss;
-
-} STACK_ITEM;
-
#define push(x) \
if (sp < stack_size) \
@@ -172,14 +161,14 @@ int yr_execute_code(
time_t start_time)
{
int64_t mem[MEM_SIZE];
- int64_t args[MAX_FUNCTION_ARGS];
int32_t sp = 0;
uint8_t* ip = rules->code_start;
- STACK_ITEM *stack;
- STACK_ITEM r1;
- STACK_ITEM r2;
- STACK_ITEM r3;
+ YR_VALUE args[MAX_FUNCTION_ARGS];
+ YR_VALUE *stack;
+ YR_VALUE r1;
+ YR_VALUE r2;
+ YR_VALUE r3;
#ifdef PROFILING_ENABLED
YR_RULE* current_rule = NULL;
@@ -207,7 +196,7 @@ int yr_execute_code(
yr_get_configuration(YR_CONFIG_STACK_SIZE, (void*) &stack_size);
- stack = (STACK_ITEM*) yr_malloc(stack_size * sizeof(STACK_ITEM));
+ stack = (YR_VALUE*) yr_malloc(stack_size * sizeof(YR_VALUE));
if (stack == NULL)
return ERROR_INSUFICIENT_MEMORY;
@@ -553,7 +542,7 @@ int yr_execute_code(
if (is_undef(r1)) // count the number of undefined args
count++;
- args[i - 1] = r1.i;
+ args[i - 1] = r1;
i--;
}
@@ -580,11 +569,7 @@ int yr_execute_code(
if (strcmp(function->prototypes[i].arguments_fmt, args_fmt) == 0)
{
- result = function->prototypes[i].code(
- (void*) args,
- context,
- function);
-
+ result = function->prototypes[i].code(args, context, function);
break;
}
}
diff --git a/libyara/include/yara/modules.h b/libyara/include/yara/modules.h
index 38e1bcd..9e9a6e3 100644
--- a/libyara/include/yara/modules.h
+++ b/libyara/include/yara/modules.h
@@ -266,25 +266,25 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define define_function(func) \
int func ( \
- void* __args, \
+ YR_VALUE* __args, \
YR_SCAN_CONTEXT* __context, \
YR_OBJECT_FUNCTION* __function_obj)
#define sized_string_argument(n) \
- (*(SIZED_STRING**) &(((int64_t*) __args)[n-1]))
+ (__args[n-1].ss)
#define string_argument(n) \
(sized_string_argument(n)->c_string)
#define integer_argument(n) \
- (((int64_t*) __args)[n-1])
+ (__args[n-1].i)
#define float_argument(n) \
- (((double*) __args)[n-1])
+ (__args[n-1].d)
#define regexp_argument(n) \
- ((RE_CODE)((int64_t*) __args)[n-1])
+ ((RE_CODE)(__args[n-1].p))
#define module() yr_object_get_root((YR_OBJECT*) __function_obj)
diff --git a/libyara/include/yara/types.h b/libyara/include/yara/types.h
index a7c50cc..4cc84f6 100644
--- a/libyara/include/yara/types.h
+++ b/libyara/include/yara/types.h
@@ -380,7 +380,6 @@ typedef struct _YR_RULES {
} YR_RULES;
-
struct _YR_MEMORY_BLOCK;
struct _YR_MEMORY_BLOCK_ITERATOR;
@@ -514,11 +513,23 @@ typedef struct _YR_OBJECT_DICTIONARY
} YR_OBJECT_DICTIONARY;
+typedef union _YR_VALUE {
+
+ int64_t i;
+ double d;
+ void* p;
+ YR_OBJECT* o;
+ YR_STRING* s;
+ SIZED_STRING* ss;
+
+} YR_VALUE;
+
+
struct _YR_OBJECT_FUNCTION;
typedef int (*YR_MODULE_FUNC)(
- void* args,
+ YR_VALUE* args,
YR_SCAN_CONTEXT* context,
struct _YR_OBJECT_FUNCTION* function_obj);
--
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