我正在为 Windows 开发内核驱动程序,我最近决定从 C 过渡到 C/C++。我在代码改造的过程中,遇到了一个warning,我无法解决。基本上,我有一个包含这行特定代码的函数:
OB_PREOP_CALLBACK_STATUS
OCPreOperationCallback(
_In_ PVOID RegistrationContext,
_Inout_ POB_PRE_OPERATION_INFORMATION OperationInformation
)
{
// ...
auto test = IoThreadToProcess(static_cast<PETHREAD>(OperationInformation->Object));
// ...
}
此行高亮显示错误消息如下:
Warning C26486 Don't pass a pointer that may be invalid to a function. Parameter 0 '(*OperationInformation).Object' in call to 'IoThreadToProcess' may be invalid (lifetime.3).
警告似乎很清楚:我需要确保我作为参数传递的指针是有效的。尽管如此,我找不到任何方法来检查无效状态。我试图添加:
if(OperationInformation != nullptr && OperationInformation->Object != nullptr)
在分配之前,但警告仍然存在。
关于这个问题我是否遗漏了什么?
最佳答案
PETHREAD pThread = static_cast<PETHREAD>(OperationInformation->Object);
if(pThread != nullptr) //or 'pThread != NULL' or 'pThread'
{
auto test = IoThreadToProcess(pThread);
}
关于c++ - C26486 - 不要将可能无效的指针传递给函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58914755/