diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 3f95dae60..cde62f887 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -22,10 +22,12 @@ jobs: run: | pip install isort pip install black + pip install flake8 - name: Run linters run: | git diff-index --quiet HEAD -- pwndbg tests isort --check-only --diff pwndbg tests black --diff --check pwndbg tests + flake8 --show-source pwndbg/ tests python3 -m py_compile $(git ls-files 'pwndbg/*.py') diff --git a/pwndbg/color/__init__.py b/pwndbg/color/__init__.py index 85bb46cf8..625876038 100644 --- a/pwndbg/color/__init__.py +++ b/pwndbg/color/__init__.py @@ -142,7 +142,7 @@ def generateColorFunction(config): def strip(x): - return re.sub("\x1b\\[[\d;]+m", "", x) + return re.sub("\x1b\\[[\\d;]+m", "", x) def terminateWith(x, color): diff --git a/pwndbg/color/lexer.py b/pwndbg/color/lexer.py index b85dbf381..fe0935e19 100644 --- a/pwndbg/color/lexer.py +++ b/pwndbg/color/lexer.py @@ -26,7 +26,7 @@ class PwntoolsLexer(RegexLexer): #: optional Comment or Whitespace string = r'"(\\"|[^"])*"' char = r"[\w$.@-]" - identifier = r"(?:[a-zA-Z$_]" + char + "*|\." + char + "+|or)" + identifier = r"(?:[a-zA-Z$_]" + char + r"*|\." + char + r"+|or)" number = r"(?:0[xX][a-zA-Z0-9]+|\d+)" memory = r"(?:[\]\[])" diff --git a/pwndbg/commands/attachp.py b/pwndbg/commands/attachp.py index f1522be57..97abd624c 100644 --- a/pwndbg/commands/attachp.py +++ b/pwndbg/commands/attachp.py @@ -13,7 +13,7 @@ parser = argparse.ArgumentParser( description="""Attaches to a given pid, process name or device file. This command wraps the original GDB `attach` command to add the ability -to debug a process with given name. In such case the process identifier is +to debug a process with given name. In such case the process identifier is fetched via the `pidof ` command. Original GDB attach command help: diff --git a/pwndbg/commands/comments.py b/pwndbg/commands/comments.py index 90359274b..2663f7a6c 100644 --- a/pwndbg/commands/comments.py +++ b/pwndbg/commands/comments.py @@ -27,7 +27,7 @@ def comm(addr=None, comment=None): else: f.write("file:%s=" % pwndbg.proc.exe) f.write("%#x:%s\n" % (target, comment)) - if not pwndbg.proc.exe in file_lists.keys(): + if pwndbg.proc.exe not in file_lists.keys(): file_lists[pwndbg.proc.exe] = {} file_lists[pwndbg.proc.exe][hex(target)] = comment except Exception: @@ -46,7 +46,7 @@ def init(): filename = text1.split(":")[1] addr_comm = text2.split(":") - if not filename in file_lists: + if filename not in file_lists: file_lists[filename] = {} file_lists[filename][addr_comm[0]] = addr_comm[1] diff --git a/pwndbg/commands/leakfind.py b/pwndbg/commands/leakfind.py index 9d62a5ebb..5daeb3537 100644 --- a/pwndbg/commands/leakfind.py +++ b/pwndbg/commands/leakfind.py @@ -22,8 +22,8 @@ def get_rec_addr_string(addr, visited_map): page = pwndbg.vmmap.find(addr) arrow_right = C.arrow(" %s " % config_arrow_right) - if not page is None: - if not addr in visited_map: + if page is not None: + if addr not in visited_map: return "" parent_info = visited_map[addr] diff --git a/pwndbg/commands/p2p.py b/pwndbg/commands/p2p.py index e2afcdf05..71caba092 100644 --- a/pwndbg/commands/p2p.py +++ b/pwndbg/commands/p2p.py @@ -101,7 +101,7 @@ def p2p_walk(addr, ranges, current_level): maybe_addr = maybe_points_to_ranges(addr, ranges[current_level]) - if maybe_addr == None: + if maybe_addr is None: return None if current_level == levels - 1: @@ -124,5 +124,5 @@ def p2p(mapping_names: [] = None): for addr in range(rng.begin, rng.end): maybe_pointer = p2p_walk(addr, mapping_names, current_level=1) - if maybe_pointer != None: + if maybe_pointer is not None: ts(address=addr, count=1) diff --git a/pwndbg/commands/probeleak.py b/pwndbg/commands/probeleak.py index 7487eb47b..c6626e09e 100644 --- a/pwndbg/commands/probeleak.py +++ b/pwndbg/commands/probeleak.py @@ -87,7 +87,7 @@ def probeleak(address=None, count=0x40, max_distance=0x0, point_to=None, max_ptr ptrsize = pwndbg.arch.ptrsize count = max(int(count), ptrsize) off_zeros = int(math.ceil(math.log(count, 2) / 4)) - if flags != None: + if flags is not None: require_flags = flags_str2int(flags) if count > address > 0x10000: # in case someone puts in an end address and not a count (smh) @@ -119,9 +119,9 @@ def probeleak(address=None, count=0x40, max_distance=0x0, point_to=None, max_ptr p = pwndbg.arch.unpack(data[i : i + ptrsize]) page = find_module(p, max_distance) if page: - if point_to != None and point_to not in page.objfile: + if point_to is not None and point_to not in page.objfile: continue - if flags != None and not satisfied_flags(require_flags, page.flags): + if flags is not None and not satisfied_flags(require_flags, page.flags): continue if not found: print(M.legend()) diff --git a/pwndbg/commands/version.py b/pwndbg/commands/version.py index 398889095..9ca91c719 100644 --- a/pwndbg/commands/version.py +++ b/pwndbg/commands/version.py @@ -108,7 +108,7 @@ Briefly describe the problem you are having in a few paragraphs. diff --git a/pwndbg/dt.py b/pwndbg/dt.py index 890894f8a..edea1611d 100644 --- a/pwndbg/dt.py +++ b/pwndbg/dt.py @@ -35,7 +35,7 @@ def get_field_by_name(obj, field): # Dereference once if obj.type.code == gdb.TYPE_CODE_PTR: obj = obj.dereference() - for f in re.split("(->|\.|\[\d+\])", field): + for f in re.split(r"(->|\.|\[\d+\])", field): if not f: continue if f == "->": diff --git a/pwndbg/enhance.py b/pwndbg/enhance.py index efd03297d..952a063f8 100644 --- a/pwndbg/enhance.py +++ b/pwndbg/enhance.py @@ -67,7 +67,7 @@ def enhance(value, code=True, safe_linking=False): # If it's not in a page we know about, try to dereference # it anyway just to test. can_read = True - if not page or None == pwndbg.memory.peek(value): + if not page or None is pwndbg.memory.peek(value): can_read = False if not can_read: diff --git a/pwndbg/net.py b/pwndbg/net.py index 2dc293cd8..07075c533 100644 --- a/pwndbg/net.py +++ b/pwndbg/net.py @@ -165,27 +165,27 @@ def unix(): NETLINK_TYPES = { - 0: "NETLINK_ROUTE", # /* Routing/device hook */ - 1: "NETLINK_UNUSED", # /* Unused number */ - 2: "NETLINK_USERSOCK", # /* Reserved for user mode socket protocols */ - 3: "NETLINK_FIREWALL", # /* Unused number", formerly ip_queue */ - 4: "NETLINK_SOCK_DIAG", # /* socket monitoring */ - 5: "NETLINK_NFLOG", # /* netfilter/iptables ULOG */ - 6: "NETLINK_XFRM", # /* ipsec */ - 7: "NETLINK_SELINUX", # /* SELinux event notifications */ - 8: "NETLINK_ISCSI", # /* Open-iSCSI */ - 9: "NETLINK_AUDIT", # /* auditing */ + 0: "NETLINK_ROUTE", # Routing/device hook + 1: "NETLINK_UNUSED", # Unused number + 2: "NETLINK_USERSOCK", # Reserved for user mode socket protocols + 3: "NETLINK_FIREWALL", # Unused number", formerly ip_queue + 4: "NETLINK_SOCK_DIAG", # socket monitoring + 5: "NETLINK_NFLOG", # netfilter/iptables ULOG + 6: "NETLINK_XFRM", # ipsec + 7: "NETLINK_SELINUX", # SELinux event notifications + 8: "NETLINK_ISCSI", # Open-iSCSI + 9: "NETLINK_AUDIT", # auditing 10: "NETLINK_FIB_LOOKUP", # 11: "NETLINK_CONNECTOR", # - 12: "NETLINK_NETFILTER", # /* netfilter subsystem */ + 12: "NETLINK_NETFILTER", # netfilter subsystem 13: "NETLINK_IP6_FW", # - 14: "NETLINK_DNRTMSG", # /* DECnet routing messages */ - 15: "NETLINK_KOBJECT_UEVENT", # /* Kernel messages to userspace */ + 14: "NETLINK_DNRTMSG", # DECnet routing messages + 15: "NETLINK_KOBJECT_UEVENT", # Kernel messages to userspace 16: "NETLINK_GENERIC", # - 18: "NETLINK_SCSITRANSPORT", # /* SCSI Transports */ + 18: "NETLINK_SCSITRANSPORT", # SCSI Transports 19: "NETLINK_ECRYPTFS", # 20: "NETLINK_RDMA", # - 21: "NETLINK_CRYPTO", # /* Crypto layer */ + 21: "NETLINK_CRYPTO", # Crypto layer } diff --git a/setup.cfg b/setup.cfg index bb694021e..445ca73f9 100644 --- a/setup.cfg +++ b/setup.cfg @@ -2,3 +2,66 @@ ignore = E203,E227,E251,E226,E501,E221,E241,E402,E261,E265,E704,W603,W503 max-line-length=120 exclude = capstone,unicorn,pwndbg/constants + +[flake8] +ignore = + # continuation line under-indented for hanging indent (default) + E121, + # closing bracket does not match indentation of opening bracket's line (default) + E123, + # continuation line over-indented for hanging indent (default) + E126, + # continuation line under-indented for visual indent + E128, + # closing bracket is missing indentation (default) + E133, + # whitespace before ‘:’ + E203, + # multiple spaces before operator + E221, + # missing whitespace around arithmetic operator (default) + E226, + # missing whitespace around bitwise or shift operator + E227, + # multiple spaces after ‘,’ (default) + E241, + # tab after ',' (default) + E242, + # unexpected spaces around keyword / parameter equals + E251, + # missing whitespace around arithmetic operator + E261, + # too many leading '#' for block comment + E266, + # block comment should start with ‘# ‘ + E265, + # module level import not at top of file + E402, + # line too long (82 > 79 characters) + E501, + # multiple statements on one line (def) (default) + E704, + # do not use bare except, specify exception instead + E722, + # do not assign a lambda expression, use a def + E731 + # line break before binary operator (default) + W503, + # line break occurred after a binary operator (default) + W504, + # doc line too long (82 > 79 characters) (default) + W505, + # module imported but unused + F401, + # 'from module import *' used; unable to detect undefined names + F403, + # name may be undefined, or defined from star imports: module + F405, + # redefinition of unused name from line n + F811, + # undefined name name + F821, + # local variable name is assigned to but never used + F841 +max-line-length=120 +exclude = capstone,unicorn,pwndbg/constants diff --git a/tests/test_attachp.py b/tests/test_attachp.py index 32e080d7d..607d709be 100644 --- a/tests/test_attachp.py +++ b/tests/test_attachp.py @@ -40,7 +40,7 @@ def launched_bash_binary(): os.remove(path) # Cleanup -@pytest.mark.skipif(can_attach == False, reason=REASON_CANNOT_ATTACH) +@pytest.mark.skipif(can_attach is False, reason=REASON_CANNOT_ATTACH) def test_attachp_command_attaches_to_procname(launched_bash_binary): pid, binary_path = launched_bash_binary @@ -53,7 +53,7 @@ def test_attachp_command_attaches_to_procname(launched_bash_binary): assert re.search(r"Detaching from program: %s, process %s" % (binary_path, pid), result) -@pytest.mark.skipif(can_attach == False, reason=REASON_CANNOT_ATTACH) +@pytest.mark.skipif(can_attach is False, reason=REASON_CANNOT_ATTACH) def test_attachp_command_attaches_to_pid(launched_bash_binary): pid, binary_path = launched_bash_binary @@ -65,7 +65,7 @@ def test_attachp_command_attaches_to_pid(launched_bash_binary): assert re.search(r"Detaching from program: %s, process %s" % (binary_path, pid), result) -@pytest.mark.skipif(can_attach == False, reason=REASON_CANNOT_ATTACH) +@pytest.mark.skipif(can_attach is False, reason=REASON_CANNOT_ATTACH) def test_attachp_command_attaches_to_procname_too_many_pids(launched_bash_binary): pid, binary_path = launched_bash_binary @@ -86,7 +86,7 @@ def test_attachp_command_attaches_to_procname_too_many_pids(launched_bash_binary assert matches == expected_pids -@pytest.mark.skipif(can_attach == False, reason=REASON_CANNOT_ATTACH) +@pytest.mark.skipif(can_attach is False, reason=REASON_CANNOT_ATTACH) def test_attachp_command_nonexistent_procname(): result = run_gdb_with_script( pyafter="attachp some-nonexistent-process-name" diff --git a/tests/test_loads.py b/tests/test_loads.py index 3afaf200d..c6c0561e2 100644 --- a/tests/test_loads.py +++ b/tests/test_loads.py @@ -56,13 +56,13 @@ def test_loads_binary_with_core_without_crashing(): assert all(item in output for item in expected) - lwp_line = re.compile("^\[New LWP \d+\]$") + lwp_line = re.compile(r"^\[New LWP \d+\]$") assert any([lwp_line.match(line) for line in output]) binary_line = re.compile("^Core was generated by .+$") assert any([binary_line.match(line) for line in output]) - crash_address_line = re.compile("^#0 0x[0-9a-fA-F]+ in main \(\)$") + crash_address_line = re.compile(r"^#0 0x[0-9a-fA-F]+ in main \(\)$") assert any([crash_address_line.match(line) for line in output]) @@ -82,13 +82,13 @@ def test_loads_core_without_crashing(): assert all(item in output for item in expected) - lwp_line = re.compile("^\[New LWP \d+\]$") + lwp_line = re.compile(r"^\[New LWP \d+\]$") assert any([lwp_line.match(line) for line in output]) binary_line = re.compile("^Core was generated by .+$") assert any([binary_line.match(line) for line in output]) - crash_address_line = re.compile("^#0 0x[0-9a-fA-F]+ in \?\? \(\)$") + crash_address_line = re.compile(r"^#0 0x[0-9a-fA-F]+ in \?\? \(\)$") assert any([crash_address_line.match(line) for line in output])