# Buggy code
def run(request):
    """Show the smali code."""
    try:
        match = re.match("^[0-9a-f]{32}$", request.GET["md5"])
        if match:
            md5 = request.GET["md5"]
            src = os.path.join(settings.UPLD_DIR, md5 + "/smali_source/")
            html = ""
            # pylint: disable=unused-variable
            # Needed by os.walk
            for dir_name, sub_dir, files in os.walk(src):
                for jfile in files:
                    if jfile.endswith(".smali"):
                        file_path = os.path.join(src, dir_name, jfile)
                        if "+" in jfile:
                            fp2 = os.path.join(src, dir_name, jfile.replace("+", "x"))
                            shutil.move(file_path, fp2)
                            file_path = fp2
                        fileparam = file_path.replace(src, "")
                        html += (
                            "<tr><td><a href='../ViewSource/?file="
                            + escape(fileparam)
                            + "&type=apk&md5="
                            + md5
                            + "'>"
                            + escape(fileparam)
                            + "</a></td></tr>"
                        )
        context = {
            "title": "Smali Source",
            "files": html,
            "md5": md5,
        }

        template = "static_analysis/smali.html"
        return render(request, template, context)
    except:
        PrintException("[ERROR] Getting Smali Files")
        return HttpResponseRedirect("/error/")


# Traceback (KeyError)
[INFO] 08/Feb/2019 16:17:54 - VM Starting
[INFO] 08/Feb/2019 16:17:55 - "POST /DynamicAnalyzer/ HTTP/1.1" 200 32400
[INFO] 08/Feb/2019 16:17:55 - "GET /static/css/devices.min.css HTTP/1.1" 200 44241
[INFO] 08/Feb/2019 16:17:55 - "GET /download/screen/screen.png HTTP/1.1" 302 0
[DEBUG] 08/Feb/2019 16:17:55 - Exception while resolving variable 'exp' in template 'general/error.html'.
Traceback (most recent call last):
File "/home/userhome/Mobile-Security-Framework-MobSF/venv/lib/python3.6/site-packages/django/template/base.py", line 829, in _resolve_lookup
current = current[bit]
File "/home/userhome/Mobile-Security-Framework-MobSF/venv/lib/python3.6/site-packages/django/template/context.py", line 83, in __getitem__
raise KeyError(key)
KeyError: 'exp'

During handling of the above exception, another exception occurred:

# Fixed code
def run(request):
    """Show the smali code."""
    try:
        match = re.match("^[0-9a-f]{32}$", request.GET["md5"])
        if match:
            md5 = request.GET["md5"]
            src = os.path.join(settings.UPLD_DIR, md5 + "/smali_source/")
            html = ""
            # pylint: disable=unused-variable
            # Needed by os.walk
            for dir_name, sub_dir, files in os.walk(src):
                for jfile in files:
                    if jfile.endswith(".smali"):
                        file_path = os.path.join(src, dir_name, jfile)
                        if "+" in jfile:
                            fp2 = os.path.join(src, dir_name, jfile.replace("+", "x"))
                            shutil.move(file_path, fp2)
                            file_path = fp2
                        fileparam = file_path.replace(src, "")
                        html += (
                            "<tr><td><a href='../ViewSource/?file="
                            + escape(fileparam)
                            + "&type=apk&md5="
                            + md5
                            + "'>"
                            + escape(fileparam)
                            + "</a></td></tr>"
                        )
        context = {
            "title": "Smali Source",
            "files": html,
            "md5": md5,
        }

        template = "static_analysis/smali.html"
        return render(request, template, context)
    except:
        PrintException("Getting Smali Files")
        return print_n_send_error_response(request, "Error Getting Smali Files")