Warning C26835
RtlCompareMemory
returns the number of matching bytes. Consider replacing this call withRtlEqualMemory
Remarks
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.
Example
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
{
// ...
}
}
See also
RtlEqualMemory
macro (wdm.h
)
RtlCompareMemory
function (wdm.h
)