所以在 main.c 中,我得到了打印加密内容的这部分代码如果它不为空。就这么简单。
cpp错误是:
[main.c:40]: (error) Possible null pointer dereference: encrypted - otherwise it is redundant to check if encrypted is null at line 31
代码:
char* encrypted = bmp_encrypt(key, text);
if(encrypted != NULL) //error points here (line 31)
{
printf("Encrypted:");
for(int i=0; i<strlen(text);i++)
{
printf("%x ", (unsigned char) encrypted[i]);
}
printf("\n");
}
else{printf("Encrypted:%s\n", encrypted);} //this is line 40
问题是,它按预期工作,但 cppcheck 一直困扰着我,我应该修复它吗?这样做错了吗?
最佳答案
只有当 encrypted
为 NULL 时,您的代码的 else
block 才会被输入。因此,您将 NULL 指针传递给 printf
。那可以调用 undefined behavior .
因为你知道此时指针为 NULL,只需显式打印它为 NULL:
else{printf("Encrypted: (null)\n");}
关于C: Cppcheck:可能的空点取消引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43264942/