您的位置:首页 > 其它

strcat的内存越界陷阱

2010-11-25 17:09 495 查看
一下代码段里,char szPlanChange[500] 未初始化,会导致在strncat进行字符串连接的时候因为没有在字符数组范围内找到字符串结束符'/0'而出现内存操作越界的问题。一个实际的结果是导致delete pStartTime 的时候失败,为局部指针变量pStartTime 的地址已经被strcat非法修改,导致delete操作的是一个非法的内存地址,结果抛出异常。

//char szPlanChange[500]={0};



char szPlanChange[500]; //未初始化

char *p = szPlanChange;
itr = m_vecStartTime.begin();
for(int nItemID = 0; itr != m_vecStartTime.end(); itr++)
{
p = szPlanChange;
pStartTime = *itr;
if(!pStartTime) continue;
strncat(p,"线路:",sizeof(szPlanChange));
strncat(p,pStartTime->szBusLineName,sizeof(szPlanChange));
strncat(p, " ",sizeof(szPlanChange));

strncat(p,"司机:",sizeof(szPlanChange));
strncat(p,pStartTime->szDriverName,sizeof(szPlanChange));
strncat(p, " ",sizeof(szPlanChange));

strncat(p,"车辆:",sizeof(szPlanChange));
strncat(p,pStartTime->szCarNo,sizeof(szPlanChange));
strncat(p, " ",sizeof(szPlanChange));

if(nItemID % 2)
strncat(p, "事件:e1",sizeof(szPlanChange));
else
strncat(p, "事件:e2",sizeof(szPlanChange));
strncat(p, " ",sizeof(szPlanChange));

delete pStartTime;
pStartTime = NULL;
nItemID ++;
}
m_vecStartTime.clear();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: