简体   繁体   English

CODESONAR 关于 stl::map 迭代器使用的缓冲区溢出注释

[英]Buffer overrun comment from CODESONAR on stl::map iterator usage

....
wstring wstrFirst;
INFO_t* pstInfo = NULL;
INFO_MAP::const_iterator itrReqInfoEnd = RequestedInfoMap_i.end();
for( INFO_MAP::const_iterator itrReqInfo = RequestedInfoMap_i.begin();
     itrReqInfo != itrReqInfoEnd; 
     ++itrReqInfo )
{
    wstrFirst = itrReqInfo->first;
    pstInfo = itrReqInfo->second;
    ...

Please see above code snippet.请参阅上面的代码片段。 I am running CODESONAR (static analysis tool) on this.我正在运行 CODESONAR(静态分析工具)。 My problem is that, at the last line ( pstInfo = itrReqInfo->second; ), CODESONAR shows following error:我的问题是,在最后一行( pstInfo = itrReqInfo->second; ),CODESONAR 显示以下错误:

This code reads past the end of the buffer pointed to by itrReqInfo->.此代码读取超过 itrReqInfo-> 指向的缓冲区的末尾。

. . itrReqInfo-> evaluates to &wstrFirst._Bx. itrReqInfo-> 计算为 &wstrFirst._Bx。

. . The first byte read is at offset 48 from the beginning of the buffer pointed to by itrReqInfo->, whose capacity is 48 bytes.读取的第一个字节位于距 itrReqInfo-> 指向的缓冲区开头偏移 48 处,其容量为 48 个字节。

. . The offset exceeds the capacity.偏移量超过容量。

. . The overrun occurs in stack memory.溢出发生在堆栈内存中。 The issue can occur if the highlighted code executes.如果突出显示的代码执行,则可能会出现此问题。

(here the highlighted code means pstInfo = itrReqInfo->second; ) (此处突出显示的代码表示pstInfo = itrReqInfo->second;

Is it false-positive?是假阳性吗? If not, how can I fix that?如果没有,我该如何解决?

Since itrReqInfo is a const_iterator and the for is only walking it through the map from beginning to end, don't see how anything can be reading past a buffer limit. 由于itrReqInfoconst_iterator ,而for只是从头到尾遍历整个map ,因此看不到如何读取超出缓冲区限制的内容。 But would need to see a more complete example of this error to know for sure. 但需要确定此错误的更完整示例才能确定。

I had a similar issue reported in codesonar and I fix it using 'const reference'. 我在codesonar中报告了类似的问题,并使用“ const reference”修复了它。

In your case I would try something like this... 在你的情况下,我会尝试这样的事情...

wstring wstrFirst;
INFO_MAP::const_iterator itrReqInfoEnd = RequestedInfoMap_i.end();
for( INFO_MAP::const_iterator itrReqInfo = RequestedInfoMap_i.begin();
     itrReqInfo != itrReqInfoEnd; 
     ++itrReqInfo )
{
    wstrFirst = itrReqInfo->first;
    const INFO_t& pstInfo = itrReqInfo->second;

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

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