title | description | ms.date | f1_keywords | helpviewer_keywords | |||
---|---|---|---|---|---|---|---|
Warning C26835 |
Describes the Microsoft C/C++ code analysis warning C26835, its causes, and how to address it. |
03/20/2023 |
|
|
RtlCompareMemory
returns the number of matching bytes. Consider replacing this call withRtlEqualMemory
When RtlCompareMemory
's return value is treated as a boolean, it evaluates to true when there is at least 1 equal byte before finding a difference. Moreover, comparing the result of RtlCompareMemory
to 0 evaluates to false if there is at least 1 matching byte. This behavior may be unexpected because it's different from other comparison functions such as strcmp
, making the code harder to understand. To check for equality, consider using RtlEqualMemory
instead.
This warning is available in Visual Studio 2022 version 17.7 and later versions.
int foo(const void* ptr)
{
if (RtlCompareMemory("test", ptr, 5) == 0) // C26835
{
// ...
}
}
To fix the issue, verify if the original intention was to check for equality and replace the function call with RtlEqualMemory
:
int foo(const void* ptr)
{
if (RtlEqualMemory("test", ptr, 5)) // C26835
{
// ...
}
}
RtlEqualMemory
macro (wdm.h
)
RtlCompareMemory
function (wdm.h
)