gpt4 book ai didi

c - 检测传递指向未初始化变量的指针

转载 作者:太空狗 更新时间:2023-10-29 15:36:48 26 4
gpt4 key购买 nike

有些函数有一个指针参数,指向结果来自哪里应存储调用函数,但函数还要求调用函数时,此指针指向用作输入值的某个值(例如输入/输出参数)。

我想检测调用此类函数并指向未初始化变量的情况。 Coccinelle应该能够做到这一点,但是我为实现这一目标而努力了一点。

示例目标代码:

#include <string.h>
#include <stdio.h>

static void cartoon_random_generator(int *n)
{
switch (*n) {
case 4:
*n = 4; /* http://xkcd.com/221/ */
break;
case 9:
*n = 9; /* http://dilbert.com/strips/comic/2001-10-25/ */
break;
default:
fprintf(stderr, "*n was not initialized before calling this function\n");
break;
}
}
/* alternative links http://i.stack.imgur.com/VvTef.png and http://i.stack.imgur.com/u0iJ7.gif */

static void test(const char *cartoon)
{
// not ok, missing
{
int n1;

cartoon_random_generator(&n1);
printf("Random number = %d\n", n1);
}

// ok, declaration
{
int n2 = 4;

cartoon_random_generator(&n2);
printf("Random number = %d\n", n2);
}

// ok, statement
{
int n3;

n3 = 9;
cartoon_random_generator(&n3);
printf("Random number = %d\n", n3);
}

// both ok and not ok
{
int n4, n9;

n9 = 9;
//strcmp(cartoon, "XKCD") == 0 ? cartoon_random_generator(&n4) : cartoon_random_generator(&n9);
if (strcmp(cartoon, "XKCD") == 0)
cartoon_random_generator(&n4);
else
cartoon_random_generator(&n9);
printf("Random numbers = %d, %d\n", n4, n9);
}
}

我写了下面的七星脚本

/* It is an error to call cartoon_random_generator with an uninitialized
variable. Detect this. */


/*
* This rule matches an OK case where the in variable is initialized when
* declared. No action is performed for this rule other than giving p1 a value.
*/
@rule1@
position p1;
expression init_expression;
identifier n;
@@

int n = init_expression;
...
cartoon_random_generator@p1(&n)


/*
* This rule matches an OK case where the in variable is initialized in a
* separate statement. No action is performed for this rule other than
* giving p2 a value.
*/
@rule2@
position p2;
expression init_expression;
identifier n;
@@

int n;
...
n = init_expression;
...
cartoon_random_generator@p2(&n)


/* If neither rule1 or rule2 have matched so far,
* we have a variable that is uninitialized. */

@rule3@
position p3 != rule1.p1, rule2.p2;
identifier n;
@@

int n;
...
* cartoon_random_generator@p3(&n)

但是没有考虑规则 2,我不明白为什么。运行它给出:

$ /opt/coccinelle/bin/spatch -sp_file cartoon_random.cocci cartoon_random.c
init_defs_builtins: /opt/coccinelle/share/coccinelle/standard.h
warning: rule3: inherited metavariable p2 not used in the -, +, or context code
HANDLING: cartoon_random.c
diff =
--- cartoon_random.c
+++ /tmp/cocci-output-7916-8df75b-cartoon_random.c
@@ -23,7 +23,6 @@ static void test(const char *cartoon)
{
int n1;

- cartoon_random_generator(&n1);
printf("Random number = %d\n", n1);
}

@@ -40,7 +39,6 @@ static void test(const char *cartoon)
int n3;

n3 = 9;
- cartoon_random_generator(&n3);
printf("Random number = %d\n", n3);
}

@@ -51,9 +49,7 @@ static void test(const char *cartoon)
n9 = 9;
//strcmp(cartoon, "XKCD") == 0 ? cartoon_random_generator(&n4) : cartoon_random_generator(&n9);
if (strcmp(cartoon, "XKCD") == 0)
- cartoon_random_generator(&n4);
else
- cartoon_random_generator(&n9);
printf("Random numbers = %d, %d\n", n4, n9);
}
}

最佳答案

我完全是 Coccinelle 用法的初学者,一直想了解它。您提出的问题是检测未初始化变量的一个相当好的要求,这让我进行了一些研究。在做了一些研究之后(& 从 warning: rule3: inherited metavariable p2 not used in the -, +, or context code 中得到线索)其中一种方法(可能有其他/更好的方法) 使你的 coccinelle 脚本工作是结合规则 1 和 2 & 在最终规则中仅对元变量使用单一继承。这些线路上的东西:

@rule1@
position p1;
expression init_expression;
identifier n;
@@

(
int n = init_expression;
|
int n;
...
n = init_expression;
)
...
cartoon_random_generator@p1(&n)

@rule2@
position p2 != rule1.p1;
identifier n;
@@

int n;
...
* cartoon_random_generator@p2(&n)

在这种情况下看到的输出是:

$spatch -sp_file cartoon_random.cocci cartoon_random.c
init_defs_builtins: /usr/share/coccinelle/standard.h
HANDLING: cartoon_random.c
diff =
--- cartoon_random.c
+++ /tmp/cocci-output-7916-8df75b-cartoon_random.c
@@ -23,7 +23,6 @@ static void test(const char *cartoon)
{
int n1;

- cartoon_random_generator(&n1);
printf("Random number = %d\n", n1);
}

@@ -51,9 +50,6 @@ static void test(const char *cartoon)
n9 = 9;
//strcmp(cartoon, "XKCD") == 0 ? cartoon_random_generator(&n4) : cartoon_random_generator(&n9);
if (strcmp(cartoon, "XKCD") == 0)
- cartoon_random_generator(&n4);
else
cartoon_random_generator(&n9);
printf("Random numbers = %d, %d\n", n4, n9);

这是在 FC15 上运行的,使用从 Fedora 存储库安装的 coccinelle 软件包。
希望这对您有所帮助!

关于c - 检测传递指向未初始化变量的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7685898/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com