Reputation: 19
I want to analyze the if-else structure of the demo.c file through joern and try to write a script with scala, but I found that the running results of scala1 and scala2 are different (the scala1 and scala2 want to express the same meaning). The difference between them is that a function fun is defined in scala1 (actually I want to use this function to achieve other repeated functions), but the result is wrong. Why? Is the parameter type (Traversal [ControlStructure]) of function Fun passed incorrectly?
--------- demo.c ---------
#include <stdio.h>
int main () {
/* local variable definition */
int a = 100;
/* check the boolean condition */
if(a>10) {
if(a>100){
printf("a > 100");
}else{
printf("10<a<100");
}
}else if( a <= 10 && a > 0) {
printf("Value of a is 20\n" );
}else {
printf("None of the values is matching\n" );
}
printf("Exact value of a is: %d\n", a );
if ( x > 10 ) {
printf("111");
}else{
printf("222")
}
while(x++ < MAX) {
if(x!=0) {
int y = 2*x;
sink(y);
}
}
return 0;
}
--------scala script 1--------
open("demo")
def Outermost_layer_branch =
cpg.method("main").block.astChildren.isControlStructure.controlStructureType("IF")
def fun(node:Traversal[ControlStructure]){
def node1 = node.astChildren.isControlStructure.controlStructureType("ELSE")
println(node1.size)
// the result is 1, it's right
def node2 =
node1.astChildren.filter(_.isBlock).astChildren.isControlStructure.controlStructureType("IF")
println(node2.size)
//the result is 0, it's wrong!!
}
fun(Outermost_layer_branch.order(3))
--------scala 2--------
open("demo")
def Outermost_layer_branch=
cpg.method("main").block.astChildren.isControlStructure.controlStructureType("IF")
def node1 = Outermost_layer_branch.order(3).astChildren.isControlStructure.controlStructureType("ELSE")
println(node1.size)
// the result is 1,and it is right!
def node2 = node1.astChildren.filter(_.isBlock).astChildren.isControlStructure.controlStructureType("IF")
println(node2.size)
// the result is 1,and it is right!
If I want to achieve the correct results through scala1, how can I pass parameters? Or is there any other way?
Upvotes: 1
Views: 207
Reputation: 19
I've fixed the problem!It should be
def fun(node: =>Traversal[ControlStructure])
Upvotes: 1