Subject: bug#36279: 26.2.90; (process-attributes nonexistent-pid) segmentation fault

From: xuchunyang <[email-masked]>
Date: Wed, 19 Jun 2019 00:23:20 +0800

On Tue, Jun 18, 2019, at 11:23 PM, Robert Pluim wrote:
> >>>>> On Tue, 18 Jun 2019 22:05:38 +0800, xuchunyang <[email-masked]> said:
> 
>     xuchunyang> On Tue, Jun 18, 2019, at 9:31 PM, Noam Postavsky wrote:
>     >> Xu Chunyang <[email-masked]> writes:
>     >> 
>     >> > Emacs crashes when I run the following (the PID 123456 doesn't 
> exist)
>     >> >
>     >> >     (process-attributes 123456)
>     >> >
>     >> > And I can reproduce it from Emacs -Q:
>     >> >
>     >> >     ~ $ emacs -Q --batch --eval '(print (process-attributes 
> 123456))'
>     >> >     Fatal error 11: Segmentation faultzsh: segmentation fault  
> emacs -Q --batch --eval '(print (process-attributes 123456))'
>     >> >     ~ $ emacs --version | head -1
>     >> >     GNU Emacs 26.2.90
>     >> >     ~ $
>     >> >
>     >> >
>     >> > In GNU Emacs 26.2.90 (build 1, x86_64-apple-darwin18.6.0, 
> Carbon Version 158 AppKit 1671.5)
>     >> 
>     >> Doesn't happen here[1], I just get nil.  Can you show a 
> backtrace with a
>     >> gdb (or lldb if that's not available)?
> 
> I get the same crash here. You'd expect sysctl to return an error when
> requesting info about a non-existent process, but instead it
> passive-agressively sets proclen to 0. This fixes it for me here, can
> you try it?

It fixes the issue for me too. (process-attributes 123456) returns nil instead of crashing Emacs.

> 
> diff --git a/src/sysdep.c b/src/sysdep.c
> index 1e35e06b63..518ecebcf6 100644
> --- a/src/sysdep.c
> +++ b/src/sysdep.c
> @@ -3798,7 +3798,7 @@ system_process_attributes (Lisp_Object pid)
>    CONS_TO_INTEGER (pid, int, proc_id);
>    mib[3] = proc_id;
>  
> -  if (sysctl (mib, 4, &proc, &proclen, NULL, 0) != 0)
> +  if (sysctl (mib, 4, &proc, &proclen, NULL, 0) != 0 || proclen == 0)
>      return attrs;
>  
>    uid = proc.kp_eproc.e_ucred.cr_uid;
>