【发布时间】:2017-04-06 20:13:28
【问题描述】:
所以在 main.c 中,我得到了打印加密内容的这部分代码如果它不为空。就这么简单。
cpp 错误是:
[main.c:40]:(错误)可能的空指针取消引用:加密 - 否则在第 31 行检查 encrypted 是否为空是多余的
代码:
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 一直在困扰我,我应该修复它吗?这样做有错吗?
【问题讨论】:
-
如果到第 40 行,
encrypted肯定是 null,printf("Encrypted:%s\n", encrypted)是未定义的行为。