根据 joern 运行 Scala 脚本时出现函数参数错误!

huangapple go评论88阅读模式
英文:

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 &lt;stdio.h&gt;
 
int main () {

   /* local variable definition */
   int a = 100;
 
   /* check the boolean condition */
   if(a&gt;10) {
      if(a&gt;100){
         printf(&quot;a &gt; 100&quot;);
      }else{
         printf(&quot;10&lt;a&lt;100&quot;);
      }
   }else if( a &lt;= 10 &amp;&amp; a &gt; 0) {
      
      printf(&quot;Value of a is 20\n&quot; );
   }else {  
      printf(&quot;None of the values is matching\n&quot; );
   }
   printf(&quot;Exact value of a is: %d\n&quot;, a );

   if ( x &gt; 10 ) {
    printf(&quot;111&quot;);
   }else{
      printf(&quot;222&quot;)
   }

   while(x++ &lt; MAX) {
      if(x!=0) {
         int y = 2*x;
         sink(y);
      }
   }
         
   return 0;
}
--------scala script 1--------
open(&quot;demo&quot;)
def Outermost_layer_branch =     
       cpg.method(&quot;main&quot;).block.astChildren.isControlStructure.controlStructureType(&quot;IF&quot;)
def fun(node:Traversal[ControlStructure]){
    def node1 = node.astChildren.isControlStructure.controlStructureType(&quot;ELSE&quot;)
    println(node1.size)
    // the result is 1, it&#39;s right
    def node2 = 
         node1.astChildren.filter(_.isBlock).astChildren.isControlStructure.controlStructureType(&quot;IF&quot;)
    println(node2.size)
    //the result is 0, it&#39;s wrong!! 
}
fun(Outermost_layer_branch.order(3))
--------scala 2--------
open(&quot;demo&quot;)
def Outermost_layer_branch= 
        cpg.method(&quot;main&quot;).block.astChildren.isControlStructure.controlStructureType(&quot;IF&quot;)
def node1 = Outermost_layer_branch.order(3).astChildren.isControlStructure.controlStructureType(&quot;ELSE&quot;)
println(node1.size)
// the result is 1,and it is right!
def node2 = node1.astChildren.filter(_.isBlock).astChildren.isControlStructure.controlStructureType(&quot;IF&quot;)
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: =&gt;Traversal[ControlStructure])

huangapple
  • 本文由 发表于 2023年2月18日 14:21:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定