jjzjj

c++ - windows C++ opening printer with documentproperties get C6836 "Write Overrun"代码分析警告

coder 2024-06-06 原文

在下面的代码中:

// If GetPrinter didn't fill in the DEVMODE, try to get it by calling
// DocumentProperties...
if (pi2->pDevMode == NULL)
{
    dwNeeded = DocumentProperties(NULL, hPrinter,
        printerName,
        NULL, NULL, 0);

    if (dwNeeded <= 0)
    {
        GlobalFree(pi2);
        ClosePrinter(hPrinter);
        return FALSE;
    }

    pDevMode = (DEVMODE *)GlobalAlloc(GPTR, dwNeeded);
    if (pDevMode == NULL)
    {
        GlobalFree(pi2);
        ClosePrinter(hPrinter);
        return FALSE;
    }

    lFlag = DocumentProperties(NULL, hPrinter,
        printerName,
        pDevMode, NULL,
        DM_OUT_BUFFER);

    if (lFlag != IDOK || pDevMode == NULL)
    {
        GlobalFree(pDevMode);
        GlobalFree(pi2);
        ClosePrinter(hPrinter);
        return FALSE;
    }

    pi2->pDevMode = pDevMode;
}

在线

lFlag = DocumentProperties(NULL, hPrinter,
printerName,
pDevMode, NULL,
DM_OUT_BUFFER);

当我运行 Visual Studio 2012“代码分析”功能时,它会抛出警告:

C6386 写入“pDevMode”时缓冲区溢出:可写大小为“dwNeeded”字节,但可能写入“220”字节。对“pDevMode”的写入无效,(超出其可写范围)

代码运行良好,但想知道如何解决此警告(最好不禁用警告)

此错误的帮助页面似乎不适用(或者我不知道它是如何应用的)http://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&l=EN-US&k=k(C6386)&rd=true

最佳答案

DocumentProperties 的 SAL 注释无法表示 DEVMODE 是一个可能大于其声明大小的结构。该函数也不采用说明 DEVMODE 传递大小的参数。该结构也没有说明大小的单个字段。因此不能使用像 __out_bcount_opt__out_bcount_part 这样的注解。

这是所有采用 DEVMODE 的 winapi 函数的问题。这是一个可以追溯到石器时代的结构,远在 SAL 出现之前。如果微软可以重来一遍,那么他们会采取不同的做法。现在太晚了。

除了知道自己做对了而工具做错了之外,您无能为力。这只是一个警告。

关于c++ - windows C++ opening printer with documentproperties get C6836 "Write Overrun"代码分析警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15986496/

有关c++ - windows C++ opening printer with documentproperties get C6836 "Write Overrun"代码分析警告的更多相关文章

随机推荐