C - 检测到堆栈粉碎,然后出现分段错误



我是新手。我需要打印系统上所有进程的详细信息。我已将"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];
 };

从你展示的代码中(顺便说一句,这仍然不足以让我们继续!),你正在用以下一个(或两个)覆盖堆栈:

  1. 您超出了ProcVar数组大小,因为您不检查数组
    边界(即CurrProcessNum < elements in the array
  2. 其中之一您读入数组的字符串超过了 CHAR_BUF_SIZE .要解决此问题,您可以使用安全版本斯坎夫。Microsoft有一个安全的sscanf,称为sscanf_s在缓冲区参数之后传递缓冲区大小。或者你可以尝试完全抛弃斯坎夫。或者使用更大的缓冲区然后复制那些使用安全字符串副本进入数组(例如 strncpy )。

相关内容

  • 没有找到相关文章

最新更新