Skip to content

Commit acaa4bc

Browse files
committed
fix(smalikit): detect existing appended method by declaration, not by reference
- Change append_method existence check to match .method declaration lines instead of plain substring search. - Prevent false negatives when method signature appears only in invoke-direct references. - Add regression test covering invoke-reference-only scenario to ensure helper method is still appended.
1 parent 993f0ae commit acaa4bc

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/utils/smalikit.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ def _apply_append_method(self, content):
9696
self.log("[ERROR] append_method signature/method_block cannot be empty", Colors.FAIL)
9797
return content, False
9898

99-
if method_signature in content:
99+
declaration_pattern = re.compile(
100+
rf"^\s*\.method[^\n\r]*{re.escape(method_signature)}\s*$",
101+
re.MULTILINE,
102+
)
103+
if declaration_pattern.search(content):
100104
return content, False
101105

102106
normalized_block = method_block.replace("\\n", "\n").strip("\n")

tests/utils/test_smalikit.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,33 @@ def test_append_method_skips_when_exists():
5555

5656
assert patched is False
5757
assert new_content == content
58+
59+
60+
def test_append_method_does_not_treat_invoke_reference_as_existing_method():
61+
signature = "getLocalizedValue(Ljava/lang/Object;)Ljava/lang/String;"
62+
method_block = """
63+
.method private getLocalizedValue(Ljava/lang/Object;)Ljava/lang/String;
64+
.locals 1
65+
const-string v0, ""
66+
return-object v0
67+
.end method
68+
""".strip()
69+
content = """
70+
.class public Lcom/example/Test;
71+
.super Ljava/lang/Object;
72+
73+
.method public demo(Ljava/lang/Object;)Ljava/lang/String;
74+
.locals 1
75+
invoke-direct {p0, p1}, Lcom/example/Test;->getLocalizedValue(Ljava/lang/Object;)Ljava/lang/String;
76+
move-result-object v0
77+
return-object v0
78+
.end method
79+
""".strip()
80+
81+
args = SmaliArgs(append_method=(signature, method_block))
82+
patcher = SmaliKit(args, logger=logging.getLogger("test.smalikit"))
83+
84+
new_content, patched = patcher.process_content(content, "Test.smali")
85+
86+
assert patched is True
87+
assert new_content.count(".method private getLocalizedValue") == 1

0 commit comments

Comments
 (0)