[Forensics-changes] [yara] 160/415: Fix some problems and remove compilation warnings

Hilko Bengen bengen at moszumanska.debian.org
Thu Apr 3 05:43:00 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 e07cb4988767d9de67a5f8cf80dfdc2d84b1624c
Author: Victor M. Alvarez <plusvic at gmail.com>
Date:   Mon May 27 09:04:09 2013 +0000

    Fix some problems and remove compilation warnings
---
 libyara/arena.c           | 49 ++++++++++++++++++++++---------------------
 libyara/compiler.c        | 12 +++++++++++
 libyara/exec.c            | 26 +++++++++++------------
 libyara/grammar.c         | 12 +++++------
 libyara/grammar.y         | 12 +++++------
 libyara/parser.c          | 18 ++++++++--------
 libyara/proc.c            |  2 +-
 libyara/yara.h            | 53 ++++++++++++++++++++++++++++++++---------------
 yara-python/yara-python.c |  2 +-
 yara.c                    |  2 +-
 10 files changed, 110 insertions(+), 78 deletions(-)

diff --git a/libyara/arena.c b/libyara/arena.c
index 4e2401f..86440b7 100644
--- a/libyara/arena.c
+++ b/libyara/arena.c
@@ -236,7 +236,7 @@ int yr_arena_coalesce(
   ARENA_PAGE* next_page;
   RELOC* reloc;
 
-  uint64_t* reloc_address;
+  void** reloc_address;
   void* reloc_target;
   int total_size = 0;
 
@@ -288,15 +288,14 @@ int yr_arena_coalesce(
 
   while (reloc != NULL)
   {
-    reloc_address = (uint64_t*) (big_page->address + reloc->offset);
-    reloc_target = (void*) *reloc_address;
+    reloc_address = big_page->address + reloc->offset;
+    reloc_target = *reloc_address;
 
     if (reloc_target != NULL)
     {
       page = _yr_arena_page_for_address(arena, reloc_target);
       assert(page != NULL);
-      *reloc_address = (uint64_t) (page->new_address + \
-                                  (reloc_target - page->address));
+      *reloc_address = page->new_address + (reloc_target - page->address);
     }
 
     reloc = reloc->next;
@@ -395,6 +394,8 @@ int yr_arena_allocate_struct(
 
   va_end(offsets);
 
+  memset(*allocated_memory, 0, size);
+
   return result;
 }
 
@@ -483,7 +484,7 @@ int yr_arena_duplicate(
   ARENA_PAGE* page;
   ARENA_PAGE* new_page;
   ARENA* new_arena;
-  uint64_t* reloc_address;
+  void** reloc_address;
   void* reloc_target;
 
   // Only coalesced arenas can be duplicated.
@@ -527,17 +528,17 @@ int yr_arena_duplicate(
 
     new_page->reloc_list_tail = new_reloc;
 
-    reloc_address = (uint64_t*) (new_page->address + new_reloc->offset);
-    reloc_target = (void*) *reloc_address;
+    reloc_address = new_page->address + new_reloc->offset;
+    reloc_target = *reloc_address;
 
     if (reloc_target != NULL)
     {
       assert(reloc_target >= page->address);
       assert(reloc_target < page->address + page->used);
 
-      *reloc_address = (uint64_t) (reloc_target - \
-                                   page->address + \
-                                   new_page->address);
+      *reloc_address = reloc_target - \
+                       page->address + \
+                       new_page->address;
     }
 
     reloc = reloc->next;
@@ -563,7 +564,7 @@ int yr_arena_save(
   ARENA_FILE_HEADER header;
 
   int32_t end_marker = -1;
-  uint64_t* reloc_address;
+  void** reloc_address;
   void* reloc_target;
 
   // Only coalesced arenas can be saved.
@@ -580,18 +581,18 @@ int yr_arena_save(
   // Convert pointers to offsets before saving.
   while (reloc != NULL)
   {
-    reloc_address = (uint64_t*) (page->address + reloc->offset);
-    reloc_target = (void*) *reloc_address;
+    reloc_address = page->address + reloc->offset;
+    reloc_target = *reloc_address;
 
     if (reloc_target != NULL)
     {
       assert(reloc_target >= page->address);
       assert(reloc_target < page->address + page->used);
-      *reloc_address -= (uint64_t) page->address;
+      *reloc_address = (void*) (*reloc_address - page->address);
     }
     else
     {
-      *reloc_address = 0xFFFFFFFFFFFFFFFFL;
+      *reloc_address = (void*) (size_t) 0xFFFABADA;
     }
 
     reloc = reloc->next;
@@ -613,11 +614,11 @@ int yr_arena_save(
   {
     fwrite(&reloc->offset, sizeof(reloc->offset), 1, fh);
 
-    reloc_address = (uint64_t*) (page->address + reloc->offset);
-    reloc_target = (void*) *reloc_address;
+    reloc_address = page->address + reloc->offset;
+    reloc_target = *reloc_address;
 
-    if (reloc_target != (void*) 0xFFFFFFFFFFFFFFFFL)
-      *reloc_address += (uint64_t) page->address;
+    if (reloc_target != (void*) (size_t) 0xFFFABADA)
+      *reloc_address += (size_t) page->address;
     else
       *reloc_address = 0;
 
@@ -643,7 +644,7 @@ int yr_arena_load(
   void* new_address;
   int result;
   int32_t reloc_offset;
-  uint64_t* reloc_address;
+  void** reloc_address;
   void* reloc_target;
 
   fh = fopen(filename, "r");
@@ -701,11 +702,11 @@ int yr_arena_load(
   {
     yr_arena_make_relocatable(new_arena, page->address, reloc_offset, EOL);
 
-    reloc_address = (uint64_t*) (page->address + reloc_offset);
+    reloc_address = page->address + reloc_offset;
     reloc_target = (void*) *reloc_address;
 
-    if (reloc_target != (void*) 0xFFFFFFFFFFFFFFFFL)
-      *reloc_address += (uint64_t) page->address;
+    if (reloc_target != (void*) (size_t) 0xFFFABADA)
+      *reloc_address += (size_t) page->address;
     else
       *reloc_address = 0;
 
diff --git a/libyara/compiler.c b/libyara/compiler.c
index 8bad5c2..08d2cc9 100644
--- a/libyara/compiler.c
+++ b/libyara/compiler.c
@@ -321,6 +321,7 @@ int _yr_compiler_compile_rules(
   YARA_RULES_FILE_HEADER* rules_file_header;
   ARENA* arena;
   RULE null_rule;
+  EXTERNAL_VARIABLE null_external;
 
   int8_t halt = HALT;
   int result;
@@ -342,6 +343,16 @@ int _yr_compiler_compile_rules(
       sizeof(RULE),
       NULL);
 
+  // Write a null external the end.
+  memset(&null_external, 0xFA, sizeof(EXTERNAL_VARIABLE));
+  null_external.type = EXTERNAL_VARIABLE_TYPE_NULL;
+
+  yr_arena_write_data(
+      compiler->externals_arena,
+      &null_external,
+      sizeof(EXTERNAL_VARIABLE),
+      NULL);
+
   // Create Aho-Corasick automaton's failure links.
   yr_ac_create_failure_links(
       compiler->automaton_arena,
@@ -355,6 +366,7 @@ int _yr_compiler_compile_rules(
         sizeof(YARA_RULES_FILE_HEADER),
         (void**) &rules_file_header,
         offsetof(YARA_RULES_FILE_HEADER, rules_list_head),
+        offsetof(YARA_RULES_FILE_HEADER, externals_list_head),
         offsetof(YARA_RULES_FILE_HEADER, code_start),
         offsetof(YARA_RULES_FILE_HEADER, automaton),
         EOL);
diff --git a/libyara/exec.c b/libyara/exec.c
index 9fb583d..4a160ac 100644
--- a/libyara/exec.c
+++ b/libyara/exec.c
@@ -165,7 +165,7 @@ int yr_execute_code(
         //printf("JLE_A_B A:%d  B:%d\n", rA, rB);
         if (rA <= rB)
         {
-          ip = (uint8_t*)(*(uint64_t*)(ip + 1));
+          ip = *(uint8_t**)(ip + 1);
           // ip will be incremented at the end of the loop,
           // decrement it here to compensate.
           ip--;
@@ -179,7 +179,7 @@ int yr_execute_code(
       case JNUNDEF_A:
         if (rA != UNDEFINED)
         {
-          ip = (uint8_t*)(*(uint64_t*)(ip + 1));
+          ip = *(uint8_t**)(ip + 1);
           // ip will be incremented at the end of the loop,
           // decrement it here to compensate.
           ip--;
@@ -337,7 +337,7 @@ int yr_execute_code(
       case EXT_STR:
         external = *(EXTERNAL_VARIABLE**)(ip + 1);
         ip += sizeof(uint64_t);
-        push((uint64_t) external->string);
+        push(PTR_TO_UINT64(external->string));
         break;
 
       case EXT_BOOL:
@@ -351,7 +351,7 @@ int yr_execute_code(
 
       case SFOUND:
         pop(r1);
-        string = (STRING*) r1;
+        string = UINT64_TO_PTR(STRING*, r1);
         push(string->flags & STRING_FLAGS_FOUND ? 1 : 0);
         //printf("SFOUND %s %d\n", string->identifier, string->flags & STRING_FLAGS_FOUND? 1 : 0);
         break;
@@ -366,7 +366,7 @@ int yr_execute_code(
           break;
         }
 
-        string = (STRING*) r2;
+        string = UINT64_TO_PTR(STRING*, r2);
         match = string->matches_list_head;
         found = 0;
 
@@ -401,7 +401,7 @@ int yr_execute_code(
           break;
         }
 
-        string = (STRING*) r3;
+        string = UINT64_TO_PTR(STRING*, r3);
         match = string->matches_list_head;
         found = 0;
 
@@ -429,7 +429,7 @@ int yr_execute_code(
 
       case SCOUNT:
         pop(r1);
-        string = (STRING*) r1;
+        string = UINT64_TO_PTR(STRING*, r1);
         match = string->matches_list_head;
         found = 0;
         while (match != NULL)
@@ -450,7 +450,7 @@ int yr_execute_code(
           break;
         }
 
-        string = (STRING*) r2;
+        string = UINT64_TO_PTR(STRING*, r2);
         match = string->matches_list_head;
         i = 1;
         found = 0;
@@ -484,7 +484,7 @@ int yr_execute_code(
         pop(r1);
         while (r1 != UNDEFINED)
         {
-          string = (STRING*) r1;
+          string = UINT64_TO_PTR(STRING*, r1);
           if (string->flags & STRING_FLAGS_FOUND)
             found++;
           count++;
@@ -538,7 +538,7 @@ int yr_execute_code(
       case CONTAINS:
         pop(r2);
         pop(r1);
-        push(strstr((char*) r1, (char*) r2) != NULL);
+        push(strstr(UINT64_TO_PTR(char*, r1), UINT64_TO_PTR(char*, r2)) != NULL);
         break;
 
       case MATCHES:
@@ -546,7 +546,7 @@ int yr_execute_code(
         pop(r1);
 
         result = regex_compile(&re,
-            (char*) r2,
+            UINT64_TO_PTR(char*, r2),
             FALSE,
             NULL,
             0,
@@ -558,8 +558,8 @@ int yr_execute_code(
 
         result = regex_exec(&re,
             FALSE,
-            (char*) r1,
-            strlen((char*) r1));
+            UINT64_TO_PTR(char*, r1),
+            strlen(UINT64_TO_PTR(char*, r1)));
 
         push(result >= 0);
         break;
diff --git a/libyara/grammar.c b/libyara/grammar.c
index ac9c451..0129670 100644
--- a/libyara/grammar.c
+++ b/libyara/grammar.c
@@ -2130,7 +2130,7 @@ yyreduce:
                           compiler->last_result = emit_with_arg_reloc(
                               yyscanner,
                               RULE_PUSH,
-                              (int64_t) rule,
+                              PTR_TO_UINT64(rule),
                               NULL);
                         }
                         else
@@ -2175,7 +2175,7 @@ yyreduce:
                           emit_with_arg_reloc(
                               yyscanner,
                               PUSH,
-                              (int64_t) string,
+                              PTR_TO_UINT64(string),
                               NULL);
 
                           emit(yyscanner, MATCHES, NULL);
@@ -2305,7 +2305,7 @@ yyreduce:
                           emit_with_arg_reloc(
                               yyscanner,
                               JNUNDEF_A,
-                              (int64_t) compiler->loop_address,
+                              PTR_TO_UINT64(compiler->loop_address),
                               NULL);
                         }
                         else // INTEGER_SET_RANGE
@@ -2317,7 +2317,7 @@ yyreduce:
                           emit_with_arg_reloc(
                               yyscanner,
                               JLE_A_B,
-                              (int64_t) compiler->loop_address,
+                              PTR_TO_UINT64(compiler->loop_address),
                               NULL);
 
                           emit(yyscanner, POP_B, NULL);
@@ -2361,7 +2361,7 @@ yyreduce:
                         emit_with_arg_reloc(
                             yyscanner,
                             JNUNDEF_A,
-                            (int64_t) compiler->loop_address,
+                            PTR_TO_UINT64(compiler->loop_address),
                             NULL);
 
                         emit(yyscanner, POP_A, NULL);
@@ -2471,7 +2471,7 @@ yyreduce:
           emit_with_arg_reloc(
               yyscanner,
               PUSH,
-              (int64_t) string,
+              PTR_TO_UINT64(string),
               NULL);
 
           yr_free((yyvsp[(1) - (1)].sized_string));
diff --git a/libyara/grammar.y b/libyara/grammar.y
index 3354680..b190277 100644
--- a/libyara/grammar.y
+++ b/libyara/grammar.y
@@ -462,7 +462,7 @@ boolean_expression  : '(' boolean_expression ')'
                           compiler->last_result = emit_with_arg_reloc(
                               yyscanner,
                               RULE_PUSH,
-                              (int64_t) rule,
+                              PTR_TO_UINT64(rule),
                               NULL);
                         }
                         else
@@ -504,7 +504,7 @@ boolean_expression  : '(' boolean_expression ')'
                           emit_with_arg_reloc(
                               yyscanner,
                               PUSH,
-                              (int64_t) string,
+                              PTR_TO_UINT64(string),
                               NULL);
 
                           emit(yyscanner, MATCHES, NULL);
@@ -607,7 +607,7 @@ boolean_expression  : '(' boolean_expression ')'
                           emit_with_arg_reloc(
                               yyscanner,
                               JNUNDEF_A,
-                              (int64_t) compiler->loop_address,
+                              PTR_TO_UINT64(compiler->loop_address),
                               NULL);
                         }
                         else // INTEGER_SET_RANGE
@@ -619,7 +619,7 @@ boolean_expression  : '(' boolean_expression ')'
                           emit_with_arg_reloc(
                               yyscanner,
                               JLE_A_B,
-                              (int64_t) compiler->loop_address,
+                              PTR_TO_UINT64(compiler->loop_address),
                               NULL);
 
                           emit(yyscanner, POP_B, NULL);
@@ -657,7 +657,7 @@ boolean_expression  : '(' boolean_expression ')'
                         emit_with_arg_reloc(
                             yyscanner,
                             JNUNDEF_A,
-                            (int64_t) compiler->loop_address,
+                            PTR_TO_UINT64(compiler->loop_address),
                             NULL);
 
                         emit(yyscanner, POP_A, NULL);
@@ -731,7 +731,7 @@ text  : _TEXTSTRING_
           emit_with_arg_reloc(
               yyscanner,
               PUSH,
-              (int64_t) string,
+              PTR_TO_UINT64(string),
               NULL);
 
           yr_free($1);
diff --git a/libyara/parser.c b/libyara/parser.c
index 6dd6afd..148b5a0 100644
--- a/libyara/parser.c
+++ b/libyara/parser.c
@@ -122,7 +122,8 @@ void emit_pushes_for_strings(
     if ((*target_identifier == '\0' && *string_identifier == '\0') ||
          *target_identifier == '*')
     {
-      emit_with_arg_reloc(yyscanner, PUSH, (int64_t) string, NULL);
+      emit_with_arg_reloc(yyscanner, PUSH, PTR_TO_UINT64(string), NULL);
+      string->flags |= STRING_FLAGS_REFERENCED;
     }
 
     string = yr_arena_next_address(
@@ -644,7 +645,7 @@ int reduce_rule_declaration(
   compiler->last_result = emit_with_arg_reloc(
       yyscanner,
       RULE_POP,
-      (uint64_t) rule,
+      PTR_TO_UINT64(rule),
       NULL);
 
   if (compiler->last_result != ERROR_SUCCESS)
@@ -698,10 +699,12 @@ int reduce_string_identifier(
       emit_with_arg_reloc(
           yyscanner,
           PUSH,
-          (int64_t) string,
+          PTR_TO_UINT64(string),
           NULL);
 
       emit(yyscanner, instruction, NULL);
+
+      string->flags |= STRING_FLAGS_REFERENCED;
     }
   }
 
@@ -726,7 +729,7 @@ int reduce_external(
       compiler->last_result = emit_with_arg_reloc(
           yyscanner,
           EXT_BOOL,
-          (int64_t) external,
+          PTR_TO_UINT64(external),
           NULL);
     }
     else if (instruction == EXT_INT &&
@@ -735,7 +738,7 @@ int reduce_external(
       compiler->last_result = emit_with_arg_reloc(
           yyscanner,
           EXT_INT,
-          (int64_t) external,
+          PTR_TO_UINT64(external),
           NULL);
     }
     else if (instruction == EXT_STR &&
@@ -744,7 +747,7 @@ int reduce_external(
       compiler->last_result = emit_with_arg_reloc(
           yyscanner,
           EXT_STR,
-          (int64_t) external,
+          PTR_TO_UINT64(external),
           NULL);
     }
     else
@@ -805,6 +808,3 @@ META* reduce_meta_declaration(
 }
 
 
-
-
-
diff --git a/libyara/proc.c b/libyara/proc.c
index 3b7f2e8..e16c370 100644
--- a/libyara/proc.c
+++ b/libyara/proc.c
@@ -271,7 +271,7 @@ int get_process_memory(
 
   while (fgets(buffer, sizeof(buffer), maps) != NULL)
   {
-    sscanf(buffer, "%lx-%lx", &begin, &end);
+    sscanf(buffer, "%zx-%zx", &begin, &end);
 
     length = end - begin;
 
diff --git a/libyara/yara.h b/libyara/yara.h
index 186f6fb..f3a3b93 100644
--- a/libyara/yara.h
+++ b/libyara/yara.h
@@ -105,12 +105,17 @@ limitations under the License.
 #define META_TYPE_STRING                2
 #define META_TYPE_BOOLEAN               3
 
-#define META_IS_NULL(x)  ((x) == NULL || ((x)->type) == META_TYPE_NULL)
+#define META_IS_NULL(x) \
+    ((x) == NULL || ((x)->type) == META_TYPE_NULL)
 
-#define EXTERNAL_VARIABLE_TYPE_ANY      0
-#define EXTERNAL_VARIABLE_TYPE_INTEGER  1
-#define EXTERNAL_VARIABLE_TYPE_STRING   2
-#define EXTERNAL_VARIABLE_TYPE_BOOLEAN  3
+#define EXTERNAL_VARIABLE_TYPE_NULL     0
+#define EXTERNAL_VARIABLE_TYPE_ANY      1
+#define EXTERNAL_VARIABLE_TYPE_INTEGER  2
+#define EXTERNAL_VARIABLE_TYPE_STRING   3
+#define EXTERNAL_VARIABLE_TYPE_BOOLEAN  4
+
+#define EXTERNAL_VARIABLE_IS_NULL(x) \
+    ((x) == NULL || ((x)->type) == EXTERNAL_VARIABLE_TYPE_NULL)
 
 #define CALLBACK_CONTINUE  0
 #define CALLBACK_ABORT     1
@@ -128,15 +133,24 @@ limitations under the License.
 #define STRING_FLAGS_FAST_MATCH   0x200
 #define STRING_FLAGS_NULL         0x1000
 
-#define STRING_IS_HEX(x)       (((x)->flags) & STRING_FLAGS_HEXADECIMAL)
-#define STRING_IS_NO_CASE(x)   (((x)->flags) & STRING_FLAGS_NO_CASE)
-#define STRING_IS_ASCII(x)     (((x)->flags) & STRING_FLAGS_ASCII)
-#define STRING_IS_WIDE(x)      (((x)->flags) & STRING_FLAGS_WIDE)
-#define STRING_IS_REGEXP(x)    (((x)->flags) & STRING_FLAGS_REGEXP)
-#define STRING_IS_FULL_WORD(x) (((x)->flags) & STRING_FLAGS_FULL_WORD)
-#define STRING_IS_ANONYMOUS(x) (((x)->flags) & STRING_FLAGS_ANONYMOUS)
-#define STRING_IS_REFERENCED(x) (((x)->flags) & STRING_FLAGS_REFERENCED)
-#define STRING_IS_NULL(x)      ((x) == NULL || ((x)->flags) & STRING_FLAGS_NULL)
+#define STRING_IS_HEX(x) \
+    (((x)->flags) & STRING_FLAGS_HEXADECIMAL)
+#define STRING_IS_NO_CASE(x) \
+    (((x)->flags) & STRING_FLAGS_NO_CASE)
+#define STRING_IS_ASCII(x) \
+    (((x)->flags) & STRING_FLAGS_ASCII)
+#define STRING_IS_WIDE(x) \
+    (((x)->flags) & STRING_FLAGS_WIDE)
+#define STRING_IS_REGEXP(x) \
+    (((x)->flags) & STRING_FLAGS_REGEXP)
+#define STRING_IS_FULL_WORD(x) \
+    (((x)->flags) & STRING_FLAGS_FULL_WORD)
+#define STRING_IS_ANONYMOUS(x) \
+    (((x)->flags) & STRING_FLAGS_ANONYMOUS)
+#define STRING_IS_REFERENCED(x) \
+    (((x)->flags) & STRING_FLAGS_REFERENCED)
+#define STRING_IS_NULL(x) \
+    ((x) == NULL || ((x)->flags) & STRING_FLAGS_NULL)
 
 #define RULE_FLAGS_MATCH                0x01
 #define RULE_FLAGS_PRIVATE              0x02
@@ -145,7 +159,8 @@ limitations under the License.
 #define RULE_FLAGS_REQUIRE_FILE         0x10
 #define RULE_FLAGS_NULL                 0x1000
 
-#define RULE_IS_NULL(x) (((x)->flags) & RULE_FLAGS_NULL)
+#define RULE_IS_NULL(x) \
+    (((x)->flags) & RULE_FLAGS_NULL)
 
 
 #define NAMESPACE_FLAGS_UNSATISFIED_GLOBAL      0x01
@@ -154,11 +169,15 @@ limitations under the License.
 
 #define EOL ((size_t) -1)
 
-
 #define DECLARE_REFERENCE(type, name) \
     union { type name; int64_t name##_; }
 
 
+#define UINT64_TO_PTR(type, x)  ((type)(size_t) x)
+
+#define PTR_TO_UINT64(x)  ((uint64_t) (size_t) x)
+
+
 typedef struct _RELOC
 {
   int32_t offset;
@@ -194,7 +213,7 @@ typedef struct _ARENA
 
 
 #pragma pack(push)
-#pragma pack(8)
+#pragma pack(1)
 
 
 typedef struct _REGEXP
diff --git a/yara-python/yara-python.c b/yara-python/yara-python.c
index 5d94f26..b0d67cd 100644
--- a/yara-python/yara-python.c
+++ b/yara-python/yara-python.c
@@ -880,7 +880,7 @@ static PyObject * yara_compile(
   FILE* fh;
 
   int fd;
-  int compile_result;
+  int compile_result = 0;
   int error_line;
   char error_message[256];
 
diff --git a/yara.c b/yara.c
index b464b77..dea99cd 100644
--- a/yara.c
+++ b/yara.c
@@ -416,7 +416,7 @@ int callback(RULE* rule, void* data)
 
           while (match != NULL)
           {
-            printf("0x%lx:%s: ", match->first_offset, string->identifier);
+            printf("0x%zx:%s: ", match->first_offset, string->identifier);
 
             if (STRING_IS_HEX(string))
             {

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