opsengine/cpulimit

cpulimit - fgets () - Segmentation fault.

eugenepaniot opened this issue · 0 comments

cpulimit got segmentation fault on fgets call.

%----backtrace
# Backtrace
[New Thread 1321]
[Thread debugging using libthread_db enabled]
Core was generated by `/usr/bin/cpulimit -e gzip -l 50'.
Program terminated with signal 11, Segmentation fault.
\#0  0x00007fbd5c48fcad in fgets () from /lib64/libc.so.6

Thread 1 (Thread 0x7fbd5cbd9700 (LWP 1321)):
\#0  0x00007fbd5c48fcad in fgets () from /lib64/libc.so.6
No symbol table info available.
\#1  0x0000000000402c0f in read_process_info ()
No symbol table info available.
\#2  0x0000000000402ea2 in get_next_process ()
No symbol table info available.
\#3  0x0000000000403026 in find_process_by_name ()
No symbol table info available.
\#4  0x00000000004022c9 in main ()
No symbol table info available.
From                To                  Syms Read   Shared Object Library
0x00007fbd5c7c2130  0x00007fbd5c7c6e18  Yes         /usr/lib64/libsnoopy.so
0x00007fbd5c447aa0  0x00007fbd5c568f4c  Yes (*)     /lib64/libc.so.6
0x00007fbd5c211760  0x00007fbd5c21d0c8  Yes (*)     /lib64/libpthread.so.0
0x00007fbd5c008de0  0x00007fbd5c009998  Yes (*)     /lib64/libdl.so.2
0x00007fbd5c9cab00  0x00007fbd5c9e3a8b  Yes (*)     /lib64/ld-linux-x86-64.so.2
(gdb) info frame
Stack level 0, frame at 0x7fffdeeab280:
 rip = 0x7f243f1b7cad in _IO_fgets (iofgets.c:52); saved rip 0x402c0f
 called by frame at 0x7fffdeeab300
 source language c.
 Arglist at 0x7fffdeeab258, args: buf=0x604fc0 "3439", n=1024, fp=0x0
 Locals at 0x7fffdeeab258, Previous frame's sp is 0x7fffdeeab280
 Saved registers:
  rbx at 0x7fffdeeab260, rbp at 0x7fffdeeab268, r12 at 0x7fffdeeab270, rip at 0x7fffdeeab278

(gdb) info args
buf = 0x604fc0 "3439"
n = 1024
fp = 0x0
Thread 1 (Thread 0x7f243f901700 (LWP 1321)):
\#0  _IO_fgets (buf=0x604fc0 "3439", n=1024, fp=0x0) at iofgets.c:52
        _IO_acquire_lock_file = 0x0
        count = <value optimized out>
        result = <value optimized out>
        old_error = <value optimized out>
\#1  0x0000000000402c0f in read_process_info ()
No symbol table info available.
\#2  0x0000000000402ea2 in get_next_process ()
No symbol table info available.
\#3  0x0000000000403026 in find_process_by_name ()
No symbol table info available.
\#4  0x00000000004022c9 in main ()
No symbol table info available.

It seems due missing "if (fd==NULL)" in read_process_info function while reading command line.

Patch:

--- src-o/process_iterator_linux.c
+++ src/process_iterator_linux.c
@@ -98,6 +98,7 @@
    //read command line
    sprintf(exefile,"/proc/%d/cmdline", p->pid);
    fd = fopen(exefile, "r");
+   if (fd==NULL) return -1;
    if (fgets(buffer, sizeof(buffer), fd)==NULL) {
        fclose(fd);
        return -1;