Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
build: python-packages links
build: python-packages links env/bin/srcml

.PHONY: links
links: env/bin/apply.py

env/bin/apply.py: env
ln -s $$(realpath scripts/patch_apply/apply.py) env/bin

env/bin/srcml:
if [ -z "$$(which srcml)" ]; then cd env; curl http://gehry.sdml.cs.kent.edu/lmcrs/v1.0.0/srcml_1.0.0-1_ubuntu20.04.tar.gz | tar -zxv ; else ln -s "$$(which srcml)" /env/bin/srcml; fi
if [ -f "env/bin/srcml" ]; then mv env/bin/srcml env/bin/srcml-binary; echo '#!/bin/bash\nLD_LIBRARY_PATH=$(CURDIR)/env/lib exec srcml-binary $$*' >env/bin/srcml; chmod +x env/bin/srcml; fi;

.PHONY: python-packages
python-packages: env
@if [ ! -d env/lib/python*/site-packages/diff_match_patch ]; then echo "Installing diff_match_patch"; . env/bin/activate; python3 -m pip install diff_match_patch; fi
@if [ ! -d env/lib/python*/site-packages/Levenshtein ]; then echo "Installing Levenshtein"; . env/bin/activate; python3 -m pip install Levenshtein; fi
@if [ ! -d env/lib/python*/site-packages/pygments ]; then echo "Installing diff_match_patch"; . env/bin/activate; python3 -m pip install pygments; fi
@if [ ! -x env/bin/pytest ]; then echo "Installing pytest"; . env/bin/activate; python3 -m pip install pytest; fi
@if [ ! -x env/bin/pytest-cov ]; then echo "Installing pytest-cov"; . env/bin/activate; python3 -m pip install pytest-cov; fi

env:
python3 -m venv env
22 changes: 15 additions & 7 deletions scripts/patch_apply/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
import scripts.patch_apply.check_file_exists_elsewhere as check_exist
from scripts.enums import MatchStatus, natureOfChange, CONTEXT_DECISION, precheckStatus

def indent(text, amount, ch = ' '):
padding = amount * ch
return ''.join(padding + line for line in text.splitlines(True))

def findGitPrefix(path):
prefix=''
resolved=False
Expand Down Expand Up @@ -240,7 +244,7 @@ def apply(pathToPatch, **kwargs):
error_message = patch_file.runResult
if kwargs['verbose'] > 0:
print("Patch failed while it was run with git apply with error:")
print(error_message)
print(indent(error_message, 4))
else:
print("Patch failed to apply with git apply.")

Expand All @@ -250,17 +254,21 @@ def apply(pathToPatch, **kwargs):
does_not_apply = set()

for line in error_message_lines:
print(line)
split_line = [s.strip() for s in line.split(":")]
if line[0:2] == " ":
pass
elif split_line[0] == "error":
if split_line[1].startswith("corrupt patch"):
line_num = re.findall(r'\d+', split_line[1])
print("The patch is corrupted at line %s, stop processing..." % line_num[0])
print("The patch is corrupted at line %s." % line_num[0])
return 1

if split_line[2] == "patch does not apply":
elif split_line[1].startswith("git diff header lacks filename information"):
print("The patch is corrupted at line %s." % line_num[0])
return 1
elif split_line[1].startswith("cannot apply binary patch"):
print("Binary patch detected.")
return 1
elif split_line[2] == "patch does not apply":
does_not_apply.add(split_line[1])
elif split_line[2] == "already exists":
already_exists.add(split_line[1])
Expand Down Expand Up @@ -307,10 +315,10 @@ def apply(pathToPatch, **kwargs):
gitFileName = os.path.join( findGitPrefix(fileName), fileName )

if see_patches:
print("\n" + ":".join([fileName, str(patch._lineschanged[0])]))
print("\n" + ":".join([fileName, str(patch._oldStart)]))
print(patch)

subpatch_name = ":".join([fileName, str(patch._lineschanged[0])])
subpatch_name = ":".join([fileName, str(patch._oldStart)])

if gitFileName in file_not_found:
correct_loc = check_exist.checkFileExistsElsewhere(patch)
Expand Down
11 changes: 7 additions & 4 deletions scripts/patch_apply/check_file_exists_elsewhere.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def checkFileExistsElsewhere(patch):

if len(matched_file_locations) == 0:
return None
else:
print("----------------------------------------------------------------------")
elif sys.stdout.isatty():
print("-" * 70)
print(
"Here are the locations of files with the same filename as the following missing file: {}".format(
toFind
Expand All @@ -44,7 +44,7 @@ def checkFileExistsElsewhere(patch):
"Select the file you would like to apply the patch to by entering the number next to it. Enter anything else to do nothing\n"
)

print("----------------------------------------------------------------------")
print("-" * 70)
try:
to_apply_file_index = int(to_apply_file_index)
if 0 <= to_apply_file_index and to_apply_file_index < len(
Expand All @@ -53,7 +53,10 @@ def checkFileExistsElsewhere(patch):
return matched_file_locations[to_apply_file_index]
except ValueError:
return None

else:
print(f"Possible files to apply the patch for {toFind} to:")
for i in matched_file_locations:
print(f" {i}")

# Testing
# patch_file = parse.PatchFile("../vulnerableforks/patches/CVE-2014-8172.patch")
Expand Down
Loading