Skip to content

Commit 7117a67

Browse files
committed
better error handling for invalid functions
1 parent a05001b commit 7117a67

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

__init__.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
import os
55
import re
66
from PySide2.QtWidgets import (QLineEdit, QPushButton, QApplication, QTextEdit, QWidget,
7-
QVBoxLayout, QHBoxLayout, QDialog, QFileSystemModel, QTreeView, QLabel, QSplitter,
7+
QVBoxLayout, QHBoxLayout, QDialog, QFileSystemModel, QTreeView, QLabel, QSplitter,
88
QInputDialog, QMessageBox, QHeaderView, QMenu, QAction, QKeySequenceEdit,
99
QPlainTextEdit)
1010
from PySide2.QtCore import (QDir, QObject, Qt, QFileInfo, QItemSelectionModel, QSettings)
1111
from PySide2.QtGui import (QFont, QFontMetrics, QDesktopServices, QKeySequence)
1212
from binaryninja import user_plugin_path
1313
from binaryninja.plugin import PluginCommand, MainThreadActionHandler
1414
from binaryninja.mainthread import execute_on_main_thread
15-
from binaryninja.log import (log_info, log_warn, log_alert, log_debug)
15+
from binaryninja.log import (log_error, log_debug)
1616
from binaryninjaui import (getMonospaceFont, UIAction, UIActionHandler, Menu)
1717
import numbers
1818

@@ -36,14 +36,14 @@ def loadSnippetFromFile(snippetPath):
3636
try:
3737
snippetText = open(snippetPath, 'r').readlines()
3838
except:
39-
return (False, [], False)
39+
return ("", "", "")
4040
if (len(snippetText) < 3):
41-
return (False, [], False)
41+
return ("", "", "")
4242
else:
4343
qKeySequence = QKeySequence(snippetText[1].strip()[1:])
4444
if qKeySequence.isEmpty():
4545
qKeySequence = None
46-
return (snippetText[0].strip()[1:],
46+
return (snippetText[0].strip()[1:],
4747
qKeySequence,
4848
''.join(snippetText[2:])
4949
)
@@ -52,7 +52,26 @@ def executeSnippet(code, context):
5252
snippetGlobals = {}
5353
snippetGlobals['current_view'] = context.binaryView
5454
snippetGlobals['bv'] = context.binaryView
55-
snippetGlobals['current_function'] = context.function
55+
if not context.function:
56+
if not context.lowLevelILFunction:
57+
if not context.mediumLevelILFunction:
58+
snippetGlobals['current_mlil'] = None
59+
snippetGlobals['current_function'] = None
60+
snippetGlobals['current_llil'] = None
61+
else:
62+
snippetGlobals['current_mlil'] = context.mediumLevelILFunction
63+
snippetGlobals['current_function'] = context.mediumLevelILFunction.source_function
64+
snippetGlobals['current_llil'] = context.mediumLevelILFunction.source_function.llil
65+
else:
66+
snippetGlobals['current_llil'] = context.lowLevelILFunction
67+
snippetGlobals['current_function'] = context.lowLevelILFunction.source_function
68+
snippetGlobals['current_mlil'] = context.lowLevelILFunction.source_function.mlil
69+
else:
70+
snippetGlobals['current_function'] = context.function
71+
snippetGlobals['current_mlil'] = context.function.mlil
72+
snippetGlobals['current_llil'] = context.function.llil
73+
snippetGlobals['current_token'] = context.function.llil
74+
5675
if context.function is not None:
5776
snippetGlobals['current_basic_block'] = context.function.get_basic_block_at(context.address)
5877
else:
@@ -63,8 +82,7 @@ def executeSnippet(code, context):
6382
snippetGlobals['current_selection'] = (context.address, context.address+context.length)
6483
else:
6584
snippetGlobals['current_selection'] = None
66-
snippetGlobals['current_llil'] = context.lowLevelILFunction
67-
snippetGlobals['current_mlil'] = context.mediumLevelILFunction
85+
snippetGlobals['uicontext'] = context
6886

6987
exec("from binaryninja import *", snippetGlobals)
7088
exec(code, snippetGlobals)
@@ -124,7 +142,7 @@ def __init__(self, parent=None):
124142
self.tree.setRootIndex(self.files.index(snippetPath))
125143
for x in range(self.columns):
126144
#self.tree.resizeColumnToContents(x)
127-
self.tree.header().setSectionResizeMode(x, QHeaderView.ResizeToContents)
145+
self.tree.header().setSectionResizeMode(x, QHeaderView.ResizeToContents)
128146
treeLayout = QVBoxLayout()
129147
treeLayout.addWidget(self.tree)
130148
treeButtons = QHBoxLayout()

0 commit comments

Comments
 (0)