Mak*_*kis 2 c static-analysis splint syntax-checking
我一直在用我最近编写的C程序试用Splint,并尝试理解并删除它给出的警告.我理解但无法理解如何删除它来自以下代码片段:
static MyType_t *findById(const int id)
{
int i;
for (i = 0; i < MY_ARR_SIZE; i++) {
if (my_arr[i].id == NOT_SET) {
/* Items are sorted so that items with
NOT_SET as ID are at the end of the array */
break;
}
if (my_arr[i].id == id) {
return &(my_arr[i]);
}
}
return NULL;
}
Run Code Online (Sandbox Code Playgroud)
Splint不满意该函数可以返回NULL,但在这种情况下它很有意义.
我尝试使用/ @ nullwhenfalse @ /但它似乎只有在函数返回true/false时才会起作用,并且还试图将代码更改为使用retVal并且在前面尝试了两个/ @ null @ /和/ @ relnull @ /宣言,但这些都没有做到.
(就像一个侧面说明,该表只有20个大的atm,所以使用聪明的搜索算法没有意义.)
您应该在声明前仔细检查/*@ null @*/的使用.
在您的示例的以下可编译版本中,它确实删除了警告(使用splint 3.1.2):
typedef struct { int id; } MyType_t;
#define NOT_SET -1
#define MY_ARR_SIZE 20
static MyType_t my_arr[MY_ARR_SIZE];
/*@null@*/ static MyType_t *findById(const int id)
{
int i;
for (i = 0; i < MY_ARR_SIZE; i++) {
if (my_arr[i].id == NOT_SET) {
/* Items are sorted so that items with
NOT_SET as ID are at the end of the array */
break;
}
if (my_arr[i].id == id) {
return &(my_arr[i]);
}
}
return NULL;
}
int main() {
(void)findById(10);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果您仍然有类似的警告,可能是您的代码的另一部分?
| 归档时间: |
|
| 查看次数: |
580 次 |
| 最近记录: |