[英]*** stack smashing detected ***: ./asem terminated Segmentation fault (core dumped)
[英]stack smashing detected followed by segmentation fault
我是新手。 我需要在系统上打印所有过程的详细信息。 我已经将"ps -aux"
的输出重定向到一个文本文件并按顺序打开以显示。 尽管我正确显示了所需的详细信息,但遇到堆栈粉碎错误,然后出现分段错误。 我可以理解分段错误是来自fgets
/ sscanf
函数之一。 我可以知道哪里出了问题吗?
if ( NULL != ( FileDesc = fopen( FileName , "r" ) ) )
{
if( ! fgets(buf, sizeof( buf ), FileDesc) )
{
Status = -1;
}
while( NULL != fgets( buf, sizeof( buf ), FileDesc ) )
{
sscanf( buf, "%*s %d %*s %s %*d %*d %*s %s %*s %s %[^\n] ",
&(ProcVar[CurrProcessNum].Pid),
&(ProcVar[CurrProcessNum].Size),
(ProcVar[CurrProcessNum].State),
(ProcVar[CurrProcessNum].CpuTime),
(ProcVar[CurrProcessNum].Cmd));
printf (" PID: %d size: %s State: %s CpuTime: %s Cmd %s",
(ProcVar[CurrProcessNum].Pid),
(ProcVar[CurrProcessNum].Size),
(ProcVar[CurrProcessNum].State),
(ProcVar[CurrProcessNum].CpuTime),
(ProcVar[CurrProcessNum].Cmd));
CurrProcessNum ++;
}
}
样本输出为:
PID: 21342 size: 0.0 State: S CpuTime: 0:00 Cmd [kjournald]
PID: 23384 size: 2.6 State: Sl CpuTime: 39:59 Cmd /opt/Adobe/Reader9/Reader/intellinux/bin/acroread /root/Documents/Comcast_RDK2.0-B13.4_Broadcom_release_notes_20140123.pdf
PID: 23495 size: 0.9 State: Ssl CpuTime: 9:01 Cmd gnome-terminal
PID: 23498 size: 0.0 State: S CpuTime: 0:00 Cmd gnome-pty-helper
PID: 23499 size: 0.0 State: Ss CpuTime: 0:00 Cmd bash
PID: 26733 size: 0.1 State: Ss CpuTime: 0:18 Cmd sshd: root@pts/3
PID: 26843 size: 0.2 State: Ss CpuTime: 0:01 Cmd -bash
PID: 26943 size: 0.1 State: Ss CpuTime: 0:06 Cmd sshd: root@notty
PID: 27052 size: 0.0 State: Ss CpuTime: 0:00 Cmd /usr/lib/openssh/sftp-server
PID: 29510 size: 0.0 State: S CpuTime: 0:00 Cmd su root
PID: 29517 size: 0.1 State: S+ CpuTime: 0:04 Cmd bash
PID: 29951 size: 0.1 State: S+ CpuTime: 1:06 Cmd minicom
PID: 30056 size: 0.0 State: Ss+ CpuTime: 0:00 Cmd bash
PID: 30293 size: 0.0 State: Ss CpuTime: 0:00 Cmd bash
PID: 30329 size: 0.0 State: S+ CpuTime: 0:01 Cmd ssh root@192.168.70.54
PID: 30597 size: 0.0 State: Ss CpuTime: 0:00 Cmd bash
PID: 30632 size: 0.0 State: S+ CpuTime: 0:00 Cmd ssh root@192.168.70.54
PID: 31508 size: 0.0 State: Ss+ CpuTime: 0:00 Cmd bash
PID: 31522 size: 0.1 State: Ss+ CpuTime: 0:00 Cmd bash
*** stack smashing detected ***: bin/TR69_DM terminated
Segmentation fault
@vonbrand请查看结构的字段
struct ProcessInfo {
char ProcName[CHAR_BUF_SIZE];
char Cmd[CHAR_BUF_SIZE];
char CpuTime[CHAR_BUF_SIZE];
int32_t Pid;
int32_t Priority;
char Size[CHAR_BUF_SIZE];
char State[CHAR_BUF_SIZE];
};
从您显示的代码(仍然不足以继续进行下去!),您将用以下任何一个(或可能两个)覆盖堆栈:
ProcVar
数组大小,因为您不检查数组边界 CurrProcessNum < elements in the array
) CHAR_BUF_SIZE
的长度。 为了解决这个问题,您可以使用安全版本的sscanf。 Microsoft有一个名为sscanf_s的安全sscanf ,您可以在buffer参数后传递缓冲区大小。 或者,您可以尝试完全放弃sscanf。 或者使用更大的缓冲区,然后复制这些到您的阵列使用安全的字符串拷贝(如strncpy
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.