用于 memcpy、strcpy 和 strcpy_s 的 C6387
C6387 for memcpy, strcpy and strcpy_s
看来还是无法摆脱C6387的警告
typedef struct HashBind{
char* cKeyIdentifier;
void* vValue;
} HashBind;
....
HashBind* strNewBind = malloc(sizeof(HashBind));
strNewBind -> cKeyIdentifier = (char*) malloc((strlen(pcKey) + 1) * sizeof(char));
memcpy(strNewBind -> cKeyIdentifier, pcKey, strlen(pcKey + 1));
pcKey 是一个 const char* 类型。我怎样才能通过
Warning C6387 'strNewBind->cKeyIdentifier' could be '0': this does not adhere to the specification for the function 'memcpy'.
当我尝试使用 strcpy 或 strcpy_s 而不是 memcpy 时同样适用。任何想法或任何替代方案?如何跳过 strcpy/memcpy 的这种不安全使用(防止缓冲区溢出)? C4496 and C6387 for using strcpy and strcat 没有太大帮助:/
'strNewBind->cKeyIdentifier' could be '0': this does not adhere to the specification for the function 'memcpy'.
测试来自 malloc()
的 NULL
return。
size_t n = (strlen(pcKey) + 1) * sizeof(char);
strNewBind -> cKeyIdentifier = malloc(n);
// Add test
if (strNewBind->cKeyIdentifier) {
memcpy(strNewBind -> cKeyIdentifier, pcKey, n);
} else {
Handle_OutOfMemory(); // TBD code.
}
看来还是无法摆脱C6387的警告
typedef struct HashBind{
char* cKeyIdentifier;
void* vValue;
} HashBind;
....
HashBind* strNewBind = malloc(sizeof(HashBind));
strNewBind -> cKeyIdentifier = (char*) malloc((strlen(pcKey) + 1) * sizeof(char));
memcpy(strNewBind -> cKeyIdentifier, pcKey, strlen(pcKey + 1));
pcKey 是一个 const char* 类型。我怎样才能通过
Warning C6387 'strNewBind->cKeyIdentifier' could be '0': this does not adhere to the specification for the function 'memcpy'.
当我尝试使用 strcpy 或 strcpy_s 而不是 memcpy 时同样适用。任何想法或任何替代方案?如何跳过 strcpy/memcpy 的这种不安全使用(防止缓冲区溢出)? C4496 and C6387 for using strcpy and strcat 没有太大帮助:/
'strNewBind->cKeyIdentifier' could be '0': this does not adhere to the specification for the function 'memcpy'.
测试来自 malloc()
的 NULL
return。
size_t n = (strlen(pcKey) + 1) * sizeof(char);
strNewBind -> cKeyIdentifier = malloc(n);
// Add test
if (strNewBind->cKeyIdentifier) {
memcpy(strNewBind -> cKeyIdentifier, pcKey, n);
} else {
Handle_OutOfMemory(); // TBD code.
}