C6029

warning C6029: possible buffer overrun in call to <function>: use of unchecked value

此警告指示正在向采用缓冲区和大小的函数传递未经检查的大小。尚未验证从某些外部源中读取的数据是否小于缓冲区大小。攻击者可能会有意指定一个比预期值大得多的大小,这将导致缓冲区溢出。

一般而言,无论何时从不受信任的外部源中读取数据,都应确保验证它的有效性。通常情况下,最好对大小进行验证,以确保它处于预期范围内。

示例

通过两次调用带注释的 ReadFile 函数,下面的代码将生成此警告。第一次调用后,Post 属性将第二个参数值标记为不受信任。因此,在第二次调用中将不受信任的值传递到 ReadFile 将生成此警告,如下面的代码中所示:

#include "windows.h"

bool f(HANDLE hFile)
{
    char buff[MAX_PATH];

    DWORD cbLen;
    DWORD cbRead;

    // Read the number of byte to read (cbLen).
    if (!ReadFile (hFile, &cbLen, sizeof (cbLen), &cbRead, NULL))  
    {
        return false;
    }
    // Read the bytes
    if (!ReadFile (hFile, buff, cbLen, &cbRead, NULL))  // warning 6029
    {
        return false;
    }

    return true;
}

若要更正此警告,请检查缓冲区大小,如下面的代码中所示:

bool f(HANDLE hFile)
{
    char buff[MAX_PATH];

    DWORD cbLen;
    DWORD cbRead;

    // Read the number of byte to read (cbLen).
    if (!ReadFile (hFile, &cbLen, sizeof (cbLen), &cbRead, NULL))  
    {
        return false;
    }
    // Ensure that there's enough space in the buffer to read that many bytes.
    if (cbLen > sizeof(buff))
    {
        return false;
    }
    // Read the bytes
    if (!ReadFile (hFile, buff, cbLen, &cbRead, NULL))  // warning 6029
    {
        return false;
    }

    return true;
}

请参见

其他资源

批注概述