如何排查大型C程序中的内存写越界导致的coredump

如何排查大型C程序中的内存写越界导致的coredump,第1张

这类问题手工排查是很麻烦的,基本上是大海捞针。

遇到这类问题个人建议先找个好的纤轿malloc配合挂valgrind运行看看。

常规的思路是让malloc和free在分配和释放内存时,分配和释放(unmap掉)新的内存页,从而让系统在后续访问该虚拟地址时触发page fault并最终导致SIGSEGV,让程序立即崩溃。

FreeBSD内建的jemalloc还提供了许多其他辅助调试特性,如防止反复free(常见的可以安全问题的一类use after free,abort:true)、填充无效数据(防止malloc之后吵竖大不做升竖初始化,junk:true)等等,此外还可以配合valgrind做redzone(帮助检测缓冲区溢出)

linux终端下,编译C语言程序稿御步骤为: 采用vi进行毁敬灶源代码编写,编写完成后,:wq存盘退出,如: vi test.c 在命令行下,运行gcc编纤扮译程序,生成执行码,如: gcc -o test test.c -o 表示指明生成的执行码名称 运行编译后的执行码 ./test

# include "iostream"

# include "String.h"

using namespace std

struct List

{

char name[20]

int NO

}

template <class List>

struct Node

{

List data

Node<List>*next

}

template <class List>

class Student

{

Node<List>*head

public:

Student(int m)

void Setdata(int n)

void PrintStudent()

int Get(char b[])

void Delete(int i)

void Insert(int i,List item)

void Change(char a[],int num)

~Student()

}

template <class List>

Student<List>::Student(int m)

{

Node<颤颂List>*r,*s

head = new Node<List>

r = head

for (int i = 0i <mi++)

{

s = new Node<List>

r->next = s

r = s

}

r->next = NULL

}

template <class List>

void Student<List>::Setdata(int n)

{

Node<List>*p

p = head->next

for(int i = 0i <ni++)

{

cout<<"请输入姓名:"

cin>>p->data.name

cout<<"请输入身份z号码:"模嫌

cin>>p->data.NO

p = p->next

}

}

template <class List>

void Student<List>::PrintStudent()

{

Node<List>*p

p = head->next

while (p)

{

cout<<p->data.name<<","<<p->data.NO<<endl

p = p->next

}

}

template <class List>

int Student<List>::Get(char b[])

{

Node<List>*p

p = head->next

while(p &&strcmp(p->data.name,b) != 0)

{

p = p->next

}

return p->data.NO

}

template <class List>

void Student<List>::Delete(int i)

{

Node<List>*p,*q

p = head

int j = 0

while(p &&j <i-1)

{

p = p->next

j++

}

if(!p || !p->next) {cerr<<旦洞手"删除位置非法"exit(1)}

else

{

List x

q = p->next

x = q->data

p->next = q->next

delete q

cout<<x.name<<","<<x.NO<<endl

}

}

template <class List>

void Student<List>::Insert(int i,List item)

{

Node<List>*p,*s

p = head

int j = 0

while (p &&j <i-1)

{

p = p->next

j++

}

if(!p) {cerr<<"插入位置非法"exit(1)}

else

{

s = new Node<List>

strcpy(s->data.name,item.name)

s->data.NO = item.NO

s->next = p->next

p->next = s

}

}

template <class List>

void Student<List>::Change(char a[],int num)

{

Node<List>*p

p = head->next

while (p &&strcmp(p->data.name,a) != 0)

{

p = p->next

}

p->data.NO = num

}

template <class List>

Student<List>::~Student()

{

Node<List>*p,*q

p = head

while (p)

{

q = p

p = p->next

delete q

}

head = NULL

}

void main()

{

cout<<"请建立学籍系统"<<endl

int m

cout<<"请输入总人数:"

cin>>m

Student<List>stu(m)

int n = m

stu.Setdata(n)

stu.PrintStudent()

int choice

do

{

cout<<"********"<<endl

cout<<"1.查询"<<endl

cout<<"2.删除"<<endl

cout<<"3.插入"<<endl

cout<<"4.修改"<<endl

cout<<"0.退出"<<endl

cout<<"********"<<endl

cout<<"请输入您的选择:"

cin>>choice

switch(choice)

{

case 1:

{

char b[20]

cout<<"请输入需查询的姓名:"

cin>>b

cout<<"查询人的学号为:"

cout<<stu.Get(b)<<endl

}break

case 2:

{

int i

cout<<"请输入需删除数据的位置:"

cin>>i

cout<<"删除的数据为:"

stu.Delete(i)

}break

case 3:

{

List item

cout<<"请输入需插入学生的姓名:"

cin>>item.name

cout<<"请输入需插入学生的学号:"

cin>>item.NO

int i

cout<<"请输入需插入的位置:"

cin>>i

stu.Insert(i,item)

stu.PrintStudent()

}break

case 4:

{

char a[20]

cout<<"请输入需修改人的姓名:"

cin>>a

int num

cout<<"请输入修改的新学号:"

cin>>num

stu.Change(a,num)

stu.PrintStudent()

}break

case 0:

{

exit(1)

}break

default:

{

cout<<"选择项非法,请重新选择"

cout<<endl

cout<<endl

}

}

} while(choice)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12299160.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存