[php-src] Issue #8240: Misleading error message on popen() when /bin/sh is missing
| From: | arnaud-lb | Date: | Fri, 01 Apr 2022 12:38:33 +0000 |
| Subject: | [php-src] Issue #8240: Misleading error message on popen() when /bin/sh is missing | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-240630@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/8240
Comment Author: arnaud-lb
Hello
I confirm. The glibc
popen() function sets errno to ENOMEM (Cannot allocate memory)
when it fails to spawn the command.
This can be seen here: https://sourceware.org/git/?p=glibc.git;a=blob;f=libio/iopopen.c;h=06778cf110ef9b2745664df00bfd37f291e23ff3;hb=HEAD#l210
This can be reproduced with this C code:
``` c
// test.c
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main() {
errno = 0;
FILE * f = popen("/bin/true", "r");
fprintf(stderr, "f: %px, errno: %d, strerror: %s\n", f, errno, strerror(errno));
}
```
```
; gcc -static -o test test.c
; sudo chroot . /test
f: (nil)x, errno: 12, strerror: Cannot allocate memory
```
The gnu man page mentions something about memory allocations and errno:
> The popen() function does not set errno if memory allocation fails.
So it _may_ done on purpose. However, this doesn't appears to be standard behavior.
Compiling against musl libc has different results:
```
; musl-gcc -static -o test test.c
; sudo chroot . /test
f: 0x, errno: 2, strerror: No such file or directory
```
We could trigger a different error when errno is ENOMEM, but other libcs appear to set errno to
ENOMEM to indicate memory allocation failures.