0

我正在尝试寻找无 malloc 的系列。考虑到我们有 1000 个测试用例。人们可能会忘记释放 ptr。请帮助我优化脚本。

正在测试的示例文件

Test(func_class,func1)
{
  int i,j;
  char* ptr = (char*) malloc(sizeof(char));
  free(ptr);
}

Test(func_class,func1)
{
  int i,j;
  char* ptr = (char*) malloc(sizeof(char));
  / Memory Leak / 
}

正在开发的脚本:

export MY_ROOT=`pwd`
_COUNT=0
pwd
_COUNT_WORD=0
filename=test.c
cat $filename | while read line
do 
    echo "Reading Line = $LINE" 
    for word in $line
    do
    _COUNT_WORD=$(($_COUNT_WORD+1))
    echo $_COUNT_WORD $word    
    if [ "$word" == "malloc\(sizeof\(char\)\);" ]; then    
        _MALLOC_FLAG=1   #this part of the code is not reached
        echo "Malloc Flag Hi"
        echo $word[2]
    fi
    done
    _COUNT_WORD=0
done 

我在匹配 malloc 正则表达式时遇到了一些问题。我知道脚本需要大量修改,因为我们必须找到个人编写 malloc 的模式。

4

1 回答 1

0

There are other ways to check:

Using awk:

awk '/malloc/ || /free/{count++;}END{printf "%d",count}' fileundertest

This will search for "malloc" and "free" words in file and will print out the count. if count is even you can say for every malloc there is free.

Using grep:

grep -c "malloc" fileundertest 

This will count the malloc word in file

Similarly,

grep -c "free" fileundertest

To list out the line number

grep -n "malloc" fileundertest
于 2013-10-07T13:21:08.543 回答