Skip to content

Latest commit

 

History

History
45 lines (35 loc) · 1.5 KB

c26835.md

File metadata and controls

45 lines (35 loc) · 1.5 KB
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
C26835
RTL_COMPARE_MEMORY_MISUSE
C26835

Warning C26835

RtlCompareMemory returns the number of matching bytes. Consider replacing this call with RtlEqualMemory

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)