[Forensics-changes] [yara] 11/368: Handle divisions by zero

Hilko Bengen bengen at moszumanska.debian.org
Sat Jul 1 10:30:05 UTC 2017


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

bengen pushed a commit to annotated tag v3.5.0
in repository yara.

commit 6fff18a475cf021b273156342ef43482a7f7f7f6
Author: Victor M. Alvarez <plusvic at gmail.com>
Date:   Tue Jun 30 20:55:35 2015 +0200

    Handle divisions by zero
---
 libyara/compiler.c           |  6 ++++++
 libyara/exec.c               | 10 ++++++++--
 libyara/grammar.y            | 24 ++++++++++++++++++++----
 libyara/include/yara/error.h |  1 +
 4 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/libyara/compiler.c b/libyara/compiler.c
index 60743e1..1863925 100644
--- a/libyara/compiler.c
+++ b/libyara/compiler.c
@@ -871,6 +871,12 @@ YR_API char* yr_compiler_get_error_message(
           buffer_size,
           "internal fatal error");
       break;
+    case ERROR_DIVISION_BY_ZERO:
+      snprintf(
+          buffer,
+          buffer_size,
+          "division by zero");
+      break;
   }
 
   return buffer;
diff --git a/libyara/exec.c b/libyara/exec.c
index 1147343..4afb5a2 100644
--- a/libyara/exec.c
+++ b/libyara/exec.c
@@ -340,7 +340,10 @@ int yr_execute_code(
         pop(r1);
         ensure_defined(r2);
         ensure_defined(r1);
-        r1.i = r1.i % r2.i;
+        if (r2.i != 0)
+          r1.i = r1.i % r2.i;
+        else
+          r1.i = UNDEFINED;
         push(r1);
         break;
 
@@ -923,7 +926,10 @@ int yr_execute_code(
         pop(r1);
         ensure_defined(r2);
         ensure_defined(r1);
-        r1.i = r1.i / r2.i;
+        if (r2.i != 0)
+          r1.i = r1.i / r2.i;
+        else
+          r1.i = UNDEFINED;
         push(r1);
         break;
 
diff --git a/libyara/grammar.y b/libyara/grammar.y
index 5e7d60d..4f9717a 100644
--- a/libyara/grammar.y
+++ b/libyara/grammar.y
@@ -1795,8 +1795,16 @@ primary_expression
         if ($1.type == EXPRESSION_TYPE_INTEGER &&
             $3.type == EXPRESSION_TYPE_INTEGER)
         {
-          $$.value.integer = OPERATION(/, $1.value.integer, $3.value.integer);
-          $$.type = EXPRESSION_TYPE_INTEGER;
+          if ($3.value.integer != 0)
+          {
+            $$.value.integer = OPERATION(/, $1.value.integer, $3.value.integer);
+            $$.type = EXPRESSION_TYPE_INTEGER;
+          }
+          else
+          {
+            compiler->last_result = ERROR_DIVISION_BY_ZERO;
+            ERROR_IF(compiler->last_result != ERROR_SUCCESS);
+          }
         }
         else
         {
@@ -1810,8 +1818,16 @@ primary_expression
 
         yr_parser_emit(yyscanner, OP_MOD, NULL);
 
-        $$.type = EXPRESSION_TYPE_INTEGER;
-        $$.value.integer = OPERATION(%, $1.value.integer, $3.value.integer);
+        if ($3.value.integer != 0)
+        {
+          $$.value.integer = OPERATION(%, $1.value.integer, $3.value.integer);
+          $$.type = EXPRESSION_TYPE_INTEGER;
+        }
+        else
+        {
+          compiler->last_result = ERROR_DIVISION_BY_ZERO;
+          ERROR_IF(compiler->last_result != ERROR_SUCCESS);
+        }
       }
     | primary_expression '^' primary_expression
       {
diff --git a/libyara/include/yara/error.h b/libyara/include/yara/error.h
index c907b83..0ab3982 100644
--- a/libyara/include/yara/error.h
+++ b/libyara/include/yara/error.h
@@ -65,6 +65,7 @@ limitations under the License.
 #define ERROR_WRONG_RETURN_TYPE                 41
 #define ERROR_DUPLICATED_STRUCTURE_MEMBER       42
 #define ERROR_EMPTY_STRING                      43
+#define ERROR_DIVISION_BY_ZERO                  44
 
 
 #define FAIL_ON_ERROR(x) { \

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