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();
//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();
相关文章推荐
- PHP Fileinfo组件越界内存破坏漏洞
- Valgrind内存读写越界检测工具使用说明
- 【转】内存访问越界
- ios 内存使用陷阱 和imageNamed 、imageWithContentsOfFile:
- 关于mfc 中使用gflags检测内存越界的问题
- linux 内存泄露,数组越界检测工具valgrind
- 当心数组越界陷阱
- 内存越界导致的malloc失效问题
- C基础 内存越界和内存监测的简单处理
- 代码测试之内存越界
- C++ 防 陷阱4 指针参数传递内存
- [转载] 内存越界
- 内存越界
- JNI创建共享内存导致JVM terminated的问题解决(segfault,shared memory,内存越界,内存泄漏,共享内存)
- iOS内存暴增问题追查与使用陷阱
- 内存访问越界
- JavaScript 中 4 种常见的内存泄露陷阱
- 内存访问越界
- C++内存越界问题及解决方法
- 介绍两个检测内存越界的工具pagehedap和gflags