Klocwork 正在发出警报,这似乎是一个错误的警报。它提到的错误描述了我们代码中大约 80% 的错误。请指教,
特此是一个片段集(释义):-
//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