我一直在尝试为Java编程语言找到“跟踪方法参数”的确切术语,我通常发现“污点分析”,但仍不确定我是否处于正确的路径。如何跟踪方法参数?
我想要的是跟踪一个方法的参数,看看该方法的哪一部分(在范围内)做参数效果。例如,如果一个参数被分配给另一个变量,我也想保持跟踪所分配的变量。通过提及“部分”,它可以是代码行,语句或控制流图的分支。
我也检查过工具,遇到了Checker Framework和Findbugs,但是看起来他们并不完全满足我的需要,或者我无法让他们满足我的需求。
请告诉我,“污点分析”是否是我正在寻找的正确术语。此外,欢迎任何其他工具建议。
从Checker Framework Live Demo下面有一个编辑的代码。我期望的是,在processRequest()
里面当变量String input
被污染时,我期望在executeQuery()
方法中的所有行中都会得到警告或错误。因为一个被污染的变量被传递给它的参数。
import org.checkerframework.checker.tainting.qual.*;
public class TaintingExampleWithWarnings {
String getUserInput() {
return "taintedStr";
}
void processRequest() {
@Tainted String input = getUserInput();
executeQuery(input); //error: pass tainted string to executeQeury()
}
public void executeQuery(@Untainted String input) {
// Do some SQL Query
String token = input + " Hello World";
String tokens[] = token.split(" ");
for(int i=0; i<tokens.length; i++)
{
System.out.println((i+1)+"String: "+tokens[i])
}
}
/* To eliminate warning in line 10, replace line 10 by
* executeQuery(validate(input)); */
/*@Untainted*/ public String validate(String userInput) {
// Do some validation here
@SuppressWarnings("tainting")
@Untainted String result = userInput;
return result;
}
}
对于不同的语言,有不同的污点检查工具。你没有指定一种语言,所以我不能推荐任何东西,但谷歌是你最好的朋友 –
@UlugToprak感谢提醒,我通过指定语言为“Java”来编辑问题。 – Ekin
IntelliJ IDEA集成了可供您使用的工具。 Checkstyle将是我的选择,也可以集成到IDE的 –