gpt4 book ai didi

c - 不是空终止字符串误报?

转载 作者:行者123 更新时间:2023-11-30 16:04:18 25 4
gpt4 key购买 nike

Klocwork 发出的警报似乎是假警报。它提到的 bug 描述了我们代码中大约 80% 的 bug。请指教,

这是一个片段集(释义):-

//a snip set
// no bug here //

{
char* destStr;
destStr = (char*)malloc(150);
if (destStr != NULL) {
destStr[0]= '\0'; //__here is the difference__
char * myStr = malloc(200) ;
if (myStr != NULL) {
strcpy(myStr , destStr) ;
}
free(myStr);
}
free (destStr);
destStr = NULL;
}

//__whereas a bug here__ !

{
char* destStr;
destStr = (char*) malloc(150);
if (destStr != NULL) {
destStr[0]= '\0'; // __here is the difference__
}
else {
printf("hello world \n");
}
if (destStr != NULL) {
char * myStr = malloc(200);
if (myStr != NULL) {
strcpy(myStr , destStr); // __NNTS (not NULL terminated string) – Buffer overflow of 'myStr' due to non null terminated string 'destStr'.__
}
free (myStr);
}
free (destStr);
destStr = NULL;
}
//end of snip set

最佳答案

您使用的是哪个版本的 Klocwork 产品?我只是尝试分析所提供的代码示例,但没有收到任何报告。在代码中添加有意的 NPD 确实会导致报告,只是为了证明我实际上正在运行该工具;p 建议如果您没有运行最近的东西,请尝试升级(Insight 9.1 是最新发布的产品集)。

问候,格温·费舍尔Klocwork, Inc. 首席技术官兼研发副总裁gwyn-at-klocwork.com

关于c - 不是空终止字符串误报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3237870/

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