From 671cacb80fd438a5f4d51db853ed08d7a6edb9ab Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sun, 25 Nov 2012 22:00:02 +0000
Subject: Fortified Chrysalide a little bit against wrong APK files.

git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@291 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a
---
 ChangeLog                          | 10 +++++
 plugins/androhelpers/try_n_catch.c |  7 ++++
 src/analysis/disass/links.c        |  5 +++
 src/arch/dalvik/decomp/const.c     |  1 +
 src/arch/dalvik/operands/pool.c    | 82 ++++++++++++++++++++++++++++++--------
 src/glibext/gbufferline.c          | 10 +++++
 src/glibext/gbufferline.h          |  2 +
 7 files changed, 101 insertions(+), 16 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 638af8a..719b2fe 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+12-11-25  Cyrille Bagard <nocbos@gmail.com>
+
+	* plugins/androhelpers/try_n_catch.c:
+	* src/analysis/disass/links.c:
+	* src/arch/dalvik/decomp/const.c:
+	* src/arch/dalvik/operands/pool.c:
+	* src/glibext/gbufferline.c:
+	* src/glibext/gbufferline.h:
+	Fortify Chrysalide a little bit against wrong APK files.
+
 12-11-23  Cyrille Bagard <nocbos@gmail.com>
 
 	* plugins/androhelpers/params.c:
diff --git a/plugins/androhelpers/try_n_catch.c b/plugins/androhelpers/try_n_catch.c
index 288fa1e..27c7959 100644
--- a/plugins/androhelpers/try_n_catch.c
+++ b/plugins/androhelpers/try_n_catch.c
@@ -127,6 +127,9 @@ static void attach_caught_code(const GLoadedBinary *binary, const GBinRoutine *r
     first = g_arch_instruction_find_by_address(instrs, start, true);
     next = g_arch_instruction_find_by_address(instrs, end, true);
 
+    if (start == NULL || next == NULL)
+        return;
+
     /* Si des détachements sont nécessaires... */
 
     if (!g_arch_instruction_has_sources(first))
@@ -272,7 +275,11 @@ static caught_exception **build_all_destinations_list(const GLoadedBinary *binar
                 continue;
 
             type = get_type_from_dex_pool(format, handlers->handlers[j].type_idx);
+            if (type == NULL)
+                continue;
+
             excep->desc = g_data_type_to_string(type);
+            g_object_unref(G_OBJECT(type));
 
             (*count)[i]++;
 
diff --git a/src/analysis/disass/links.c b/src/analysis/disass/links.c
index 7cc3c27..0ba3fc6 100644
--- a/src/analysis/disass/links.c
+++ b/src/analysis/disass/links.c
@@ -122,6 +122,11 @@ void establish_links_between_lines(GArchInstruction *list, GBinRoutine **routine
 
         iter = g_arch_instruction_find_by_address(list, start, true);
 
+        if (iter == NULL)
+            printf("no match for 0x%08llx\n", start);
+
+        if (iter != NULL)
+
         for (iter = g_arch_instruction_get_next_iter(list, iter, end);
              iter != NULL;
              iter = g_arch_instruction_get_next_iter(list, iter, end))
diff --git a/src/arch/dalvik/decomp/const.c b/src/arch/dalvik/decomp/const.c
index 2e63b36..47a859e 100644
--- a/src/arch/dalvik/decomp/const.c
+++ b/src/arch/dalvik/decomp/const.c
@@ -95,6 +95,7 @@ GDecInstruction *dalvik_decomp_instr_const_str(const GArchInstruction *instr, GD
 
     format = G_DEX_FORMAT(g_object_get_data(G_OBJECT(ctx), "format"));
     value = get_string_from_dex_pool(format, index);
+    if (value == NULL) return NULL;
 
     str = g_str_expression_new(value);
 
diff --git a/src/arch/dalvik/operands/pool.c b/src/arch/dalvik/operands/pool.c
index 803d173..fbe68cf 100644
--- a/src/arch/dalvik/operands/pool.c
+++ b/src/arch/dalvik/operands/pool.c
@@ -24,6 +24,7 @@
 #include "pool.h"
 
 
+#include <stdio.h>
 #include <string.h>
 
 
@@ -181,6 +182,7 @@ static void g_dalvik_pool_operand_print(const GDalvikPoolOperand *operand, GBuff
 {
     const char *string;                     /* Chaîne de caractères #1     */
     GDataType *type;                        /* Type à représenter          */
+    size_t len;                             /* Taille du texte à créer     */
     char *tmp;                              /* Chaîne de caractères #2     */
     GBinVariable *field;                    /* Champ à représenter         */
     GBinRoutine *routine;                   /* Routine à représenter       */
@@ -188,14 +190,30 @@ static void g_dalvik_pool_operand_print(const GDalvikPoolOperand *operand, GBuff
     switch (operand->type)
     {
         case DPT_NONE:
-            g_buffer_line_insert_text(line, BLC_ASSEMBLY, "????", 4, RTT_SECTION);
+            g_buffer_line_insert_text(line, BLC_ASSEMBLY, "????", 4, RTT_ERROR);
             break;
 
         case DPT_STRING:
-            g_buffer_line_insert_text(line, BLC_ASSEMBLY, "\"", 1, RTT_STRING);
             string = get_string_from_dex_pool(operand->format, operand->index);
-            g_buffer_line_insert_text(line, BLC_ASSEMBLY, string, strlen(string), RTT_STRING);
-            g_buffer_line_insert_text(line, BLC_ASSEMBLY, "\"", 1, RTT_STRING);
+
+            if (string != NULL)
+            {
+                g_buffer_line_insert_text(line, BLC_ASSEMBLY, "\"", 1, RTT_STRING);
+                g_buffer_line_insert_text(line, BLC_ASSEMBLY, string, strlen(string), RTT_STRING);
+                g_buffer_line_insert_text(line, BLC_ASSEMBLY, "\"", 1, RTT_STRING);
+            }
+            else
+            {
+                len = strlen(_("<bad string index (%d)>")) + 10 /* 4294967295U */ + 1;
+                tmp = calloc(len, sizeof(char));
+                snprintf(tmp, len, _("<bad string index (%d)>"), operand->index);
+
+                g_buffer_line_insert_text(line, BLC_ASSEMBLY, tmp, len - 1, RTT_ERROR);
+
+                free(tmp);
+
+            }
+
             break;
 
         case DPT_TYPE:
@@ -205,14 +223,24 @@ static void g_dalvik_pool_operand_print(const GDalvikPoolOperand *operand, GBuff
             {
                 tmp = g_data_type_to_string(type);
                 g_object_unref(G_OBJECT(type));
+
+                g_buffer_line_insert_text(line, BLC_ASSEMBLY, "<", 1, RTT_HOOK);
+                g_buffer_line_insert_text(line, BLC_ASSEMBLY, tmp, strlen(tmp), RTT_VAR_NAME);
+                g_buffer_line_insert_text(line, BLC_ASSEMBLY, ">", 1, RTT_HOOK);
+
             }
             else
-                tmp = strdup(_("invalid type"));
+            {
+                len = strlen(_("<bad type index (%d)>")) + 10 /* 4294967295U */ + 1;
+                tmp = calloc(len, sizeof(char));
+                snprintf(tmp, len, _("<bad type index (%d)>"), operand->index);
+
+                g_buffer_line_insert_text(line, BLC_ASSEMBLY, tmp, len - 1, RTT_ERROR);
+
+            }
 
-            g_buffer_line_insert_text(line, BLC_ASSEMBLY, "<", 1, RTT_HOOK);
-            g_buffer_line_insert_text(line, BLC_ASSEMBLY, tmp, strlen(tmp), RTT_VAR_NAME);
-            g_buffer_line_insert_text(line, BLC_ASSEMBLY, ">", 1, RTT_HOOK);
             free(tmp);
+
             break;
 
         case DPT_PROTO:
@@ -226,13 +254,24 @@ static void g_dalvik_pool_operand_print(const GDalvikPoolOperand *operand, GBuff
             {
                 tmp = g_binary_variable_to_string(field, false);
                 g_object_unref(G_OBJECT(field));
+
+                g_buffer_line_insert_text(line, BLC_ASSEMBLY, "<", 1, RTT_HOOK);
+                g_buffer_line_insert_text(line, BLC_ASSEMBLY, tmp, strlen(tmp), RTT_VAR_NAME);
+                g_buffer_line_insert_text(line, BLC_ASSEMBLY, ">", 1, RTT_HOOK);
+
             }
             else
-                tmp = strdup(_("invalid field"));
+            {
+                len = strlen(_("<bad field index (%d)>")) + 10 /* 4294967295U */ + 1;
+                tmp = calloc(len, sizeof(char));
+                snprintf(tmp, len, _("<bad field index (%d)>"), operand->index);
+
+                g_buffer_line_insert_text(line, BLC_ASSEMBLY, tmp, len - 1, RTT_ERROR);
+
+            }
+
+            free(tmp);
 
-            g_buffer_line_insert_text(line, BLC_ASSEMBLY, "<", 1, RTT_HOOK);
-            g_buffer_line_insert_text(line, BLC_ASSEMBLY, tmp, strlen(tmp), RTT_VAR_NAME);
-            g_buffer_line_insert_text(line, BLC_ASSEMBLY, ">", 1, RTT_HOOK);
             break;
 
         case DPT_METHOD:
@@ -242,13 +281,24 @@ static void g_dalvik_pool_operand_print(const GDalvikPoolOperand *operand, GBuff
             {
                 tmp = g_binary_routine_to_string(routine);
                 g_object_unref(G_OBJECT(routine));
+
+                g_buffer_line_insert_text(line, BLC_ASSEMBLY, "<", 1, RTT_HOOK);
+                g_buffer_line_insert_text(line, BLC_ASSEMBLY, tmp, strlen(tmp), RTT_VAR_NAME);
+                g_buffer_line_insert_text(line, BLC_ASSEMBLY, ">", 1, RTT_HOOK);
+
             }
             else
-                tmp = strdup(_("invalid method"));
+            {
+                len = strlen(_("<bad method index (%d)>")) + 10 /* 4294967295U */ + 1;
+                tmp = calloc(len, sizeof(char));
+                snprintf(tmp, len, _("<bad method index (%d)>"), operand->index);
+
+                g_buffer_line_insert_text(line, BLC_ASSEMBLY, tmp, len - 1, RTT_ERROR);
+
+            }
+
+            free(tmp);
 
-            g_buffer_line_insert_text(line, BLC_ASSEMBLY, "<", 1, RTT_HOOK);
-            g_buffer_line_insert_text(line, BLC_ASSEMBLY, tmp, strlen(tmp), RTT_VAR_NAME);
-            g_buffer_line_insert_text(line, BLC_ASSEMBLY, ">", 1, RTT_HOOK);
             break;
 
     }
diff --git a/src/glibext/gbufferline.c b/src/glibext/gbufferline.c
index c8ff5f2..cef34ef 100644
--- a/src/glibext/gbufferline.c
+++ b/src/glibext/gbufferline.c
@@ -458,6 +458,16 @@ static void g_buffer_line_class_init(GBufferLineClass *class)
     attrib = pango_attr_foreground_new(0, 0, 0);
     pango_attr_list_insert(class->attribs[RTT_KEY_WORD], attrib);
 
+    /* RTT_ERROR */
+
+    class->attribs[RTT_ERROR] = pango_attr_list_new();
+
+    attrib = pango_attr_foreground_new(65535, 0, 0);
+    pango_attr_list_insert(class->attribs[RTT_ERROR], attrib);
+
+    attrib = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
+    pango_attr_list_insert(class->attribs[RTT_ERROR], attrib);
+
 }
 
 
diff --git a/src/glibext/gbufferline.h b/src/glibext/gbufferline.h
index 670df99..f1be296 100644
--- a/src/glibext/gbufferline.h
+++ b/src/glibext/gbufferline.h
@@ -93,6 +93,8 @@ typedef enum _RenderingTagType
 
     RTT_KEY_WORD,                           /* Mot clef de langage         */
 
+    RTT_ERROR,                              /* Erreur "interne"            */
+
     RTT_COUNT
 
 } RenderingTagType;
-- 
cgit v0.11.2-87-g4458