[英]Java: ArrayList.clear removes elements from ArrayList passed to Map
這是我多年來第一次編寫 Java 代碼,我正在編寫一個 Ghidra 腳本,將系統調用符號映射到它們的調用函數。
private HashMap<Symbol, Reference[]> symbolRefs = new HashMap<Symbol, Reference[]>();
private HashMap<Symbol, List<Function>> callerFuncs = new HashMap<Symbol, List<Function>>();
.
.
.
private void mapSysCallToCallerFunctions(FunctionManager funcMan) throws Exception {
List<Function> funcs = new ArrayList<Function>();
for(HashMap.Entry<Symbol, Reference[]> entry: this.symbolRefs.entrySet()) {
for(Reference ref : entry.getValue()) {
Function caller = funcMan.getFunctionContaining(ref.getFromAddress());
if(caller != null) {
funcs.add(caller);
}
}
this.callerFuncs.put(entry.getKey(), funcs);
funcs.clear();
}
}
我的問題是我想清除“funcs”列表,以便我可以再次使用空列表進行下一次迭代。 由於某些未知原因,這導致我的 HashMap 中的 Function 列表也為空。 如果我在這里打印我的 HashMap:
private void printCallerSymbolMap() throws Exception {
for(HashMap.Entry<Symbol, List<Function>> entry: this.callerFuncs.entrySet()) {
printf("Symbol %s:\n", entry.getKey().toString());
for(Function func : entry.getValue()) {
printf("Called by function %s\n", func.getName());
}
}
}
我剛拿到 output:
INFO Symbol system: (GhidraScript)
INFO Symbol system: (GhidraScript)
但是,當我刪除 funcs.clear() 時,我得到:
INFO Symbol system: (GhidraScript)
INFO Called by function system (GhidraScript)
INFO Called by function system (GhidraScript)
INFO Called by function main (GhidraScript)
INFO Symbol system: (GhidraScript)
INFO Called by function system (GhidraScript)
INFO Called by function system (GhidraScript)
INFO Called by function main (GhidraScript)
不過應該是這樣的:
INFO Symbol system: (GhidraScript)
INFO Called by function system (GhidraScript)
INFO Called by function system (GhidraScript)
INFO Symbol system: (GhidraScript)
INFO Called by function main (GhidraScript)
我有兩個系統符號,因為它是 thunked。
清除列表的設置,每次初始化列表。
private void mapSysCallToCallerFunctions(FunctionManager funcMan) throws Exception {
List<Function> funcs;
for(HashMap.Entry<Symbol, Reference[]> entry: this.symbolRefs.entrySet()) {
funcs = new ArrayList<Function>();
for(Reference ref : entry.getValue()) {
Function caller = funcMan.getFunctionContaining(ref.getFromAddress());
if(caller != null) {
funcs.add(caller);
}
}
this.callerFuncs.put(entry.getKey(), funcs);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.