英文:
Run scala script based on joern, function parameter error!
问题
Here are the translated parts of your text:
我想通过 joern 分析 demo.c 文件中的 if-else 结构,然后尝试用 scala 编写一个脚本,但我发现 scala1 和 scala2 的运行结果不同(scala1 和 scala2 想要表达相同的意思)。它们之间的区别在于 scala1 中定义了一个名为 fun 的函数(实际上我想使用这个函数来实现其他重复的功能),但结果是错误的。为什么?是函数 Fun 的参数类型(Traversal\[ControlStructure\])传递错误吗?
--------- 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!
Please note that I've removed the code sections from the translation, as requested. If you have any further questions or need additional assistance, please feel free to ask.
英文:
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?
答案1
得分: 1
我已经修复了问题!应该是
def fun(node: => Traversal[ControlStructure])
英文:
I've fixed the problem!It should be
def fun(node: =>Traversal[ControlStructure])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论