简体   繁体   English

如何解决此 C6385 代码分析警告:读取无效数据

[英]How to resolve this C6385 code analysis warning: Reading invalid data

I am trying to address a code analysis warning that appears in the following method:我正在尝试解决以下方法中出现的代码分析警告:

CStringArray* CCreateReportDlg::BuildCustomAssignArray(ROW_DATA_S &rsRowData)
{
    INT_PTR         iAssign, iNumAssigns, iUsedAssign;
    CStringArray    *pAryStrCustom = nullptr;
    CUSTOM_ASSIGN_S *psAssign = nullptr;

    if (rsRowData.uNumCustomToFill > 0)
    {
        pAryStrCustom = new CStringArray[rsRowData.uNumCustomToFill];
        iNumAssigns = m_aryPtrAssign.GetSize();
        for (iAssign = 0, iUsedAssign = 0; iAssign < iNumAssigns; iAssign++)
        {
            psAssign = (CUSTOM_ASSIGN_S*)m_aryPtrAssign.GetAt(iAssign);
            if (psAssign != nullptr)
            {
                if (!psAssign->bExcluded)
                {
                    pAryStrCustom[iUsedAssign].Copy(psAssign->aryStrBrothersAll);
                    iUsedAssign++;
                }
            }
        }
    }

    return pAryStrCustom;
}

The offending line of code is:有问题的代码行是:

pAryStrCustom[iUsedAssign].Copy(psAssign->aryStrBrothersAll);

I compile this code for both 32 bit and 64 bit.我为 32 位和 64 位编译了这段代码。 The warning being raised is:提出的警告是:

Warning ( C6385 ) Reading invalid data from pAryStrCustom : the readable size is (size_t)*40+8 bytes, but 80 bytes may be read.警告 ( C6385 ) 从pAryStrCustom读取无效数据:可读大小为(size_t)*40+8字节,但可以读取80字节。

I don't know if it is relevant, but the CUSTOM_ASSIGN_S structure is defined as:我不知道它是否相关,但CUSTOM_ASSIGN_S结构定义为:

typedef struct tagCustomAssignment
{
    int             iIndex;
    CString         strDescription;
    CString         strHeading;
    BOOL            bExcluded;
    CStringArray    aryStrBrothersAll;
    CStringArray    aryStrBrothersWT;
    CStringArray    aryStrBrothersSM;
    BOOL            bIncludeWT;
    BOOL            bIncludeTMS;
    BOOL            bFixed;
    int             iFixedType;
} CUSTOM_ASSIGN_S;

My code is functional (for years) but is there a coding improvement I can make to address this issue?我的代码可以正常运行(多年),但是我可以进行编码改进来解决这个问题吗? I have read the linked article and it is not clear to me.我已经阅读了链接的文章,我不清楚。 I have also seen this question ( Reading Invalid Data c6385 ) along similar lines.我也看到了类似的问题( Reading Invalid Data c6385 )。 But in my code I can't see how that applies.但在我的代码中,我看不出这是如何适用的。

Warning... the readable size is (size_t)*40+8 bytes, but 80 bytes may be read.警告...可读大小为 (size_t)*40+8 字节,但可以读取 80 字节。

The wording for this warning is not accurate, because size_t is not a number, it's a data type.此警告的措辞不准确,因为size_t不是数字,而是一种数据类型。 (size_t)*40+8 doesn't make sense. (size_t)*40+8没有意义。 It's probably meant to be:大概意思是:

Warning... the readable size is '40+8 bytes', but '80 bytes' may be read.警告...可读大小为“40+8 字节”,但可以读取“80 字节”。

This warning can be roughly reproduced with the following example:可以通过以下示例大致重现此警告:

//don't run this code, it's just for viewing the warning
size_t my_size = 1;
char* buf = new char[my_size];

buf[1];
//warning C6385: Reading invalid data from 'buf':  
//the readable size is 'my_size*1' bytes, but '2' bytes may be read

The warning is correct and obvious.警告是正确和明显的。 buf[1] is out of bound. buf[1]越界。 The compiler sees allocation size for buf is my_size*1 , and index 1 is accessing byte '2' .编译器看到buf分配大小是my_size*1 ,索引1正在访问byte '2' I think in other place the compiler prints it incorrectly, but the actual warning is valid.我认为在其他地方编译器打印不正确,但实际警告是有效的。

In any case, just make sure iUsedAssign is within range在任何情况下,只要确保iUsedAssign在范围内

if (!psAssign->bExcluded && iUsedAssign < rsRowData.uNumCustomToFill)
{
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM