# Buggy code
def run(request):
    """Find in source files."""
    try:
        match = re.match("^[0-9a-f]{32}$", request.POST["md5"])
        if match:
            md5 = request.POST["md5"]
            query = request.POST["q"]
            code = request.POST["code"]
            matches = []
            if code == "java":
                src = os.path.join(settings.UPLD_DIR, md5 + "/java_source/")
                ext = ".java"
            elif code == "smali":
                src = os.path.join(settings.UPLD_DIR, md5 + "/smali_source/")
                ext = ".smali"
            else:
                return HttpResponseRedirect("/error/")
            # 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(ext):
                        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, "")
                        with io.open(
                            file_path, mode="r", encoding="utf8", errors="ignore"
                        ) as file_pointer:
                            dat = file_pointer.read()
                        if query in dat:
                            matches.append(
                                "<a href='../ViewSource/?file="
                                + escape(fileparam)
                                + "&md5="
                                + md5
                                + "&type=apk'>"
                                + escape(fileparam)
                                + "</a>"
                            )
        flz = len(matches)
        context = {
            "title": "Search Results",
            "matches": matches,
            "term": query,
            "found": str(flz),
        }
        template = "general/search.html"
        return render(request, template, context)
    except:
        PrintException("[ERROR] Searching Failed")
        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):
    """Find in source files."""
    try:
        match = re.match("^[0-9a-f]{32}$", request.POST["md5"])
        if match:
            md5 = request.POST["md5"]
            query = request.POST["q"]
            code = request.POST["code"]
            matches = []
            if code == "java":
                src = os.path.join(settings.UPLD_DIR, md5 + "/java_source/")
                ext = ".java"
            elif code == "smali":
                src = os.path.join(settings.UPLD_DIR, md5 + "/smali_source/")
                ext = ".smali"
            else:
                return print_n_send_error_response(
                    request, "Only Java/Smali files are allowed"
                )
            # 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(ext):
                        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, "")
                        with io.open(
                            file_path, mode="r", encoding="utf8", errors="ignore"
                        ) as file_pointer:
                            dat = file_pointer.read()
                        if query in dat:
                            matches.append(
                                "<a href='../ViewSource/?file="
                                + escape(fileparam)
                                + "&md5="
                                + md5
                                + "&type=apk'>"
                                + escape(fileparam)
                                + "</a>"
                            )
        flz = len(matches)
        context = {
            "title": "Search Results",
            "matches": matches,
            "term": query,
            "found": str(flz),
        }
        template = "general/search.html"
        return render(request, template, context)
    except:
        PrintException("Searching Failed")
        return print_n_send_error_response(request, "Searching Failed")