Skip to content

Commit 020f600

Browse files
committed
fix(macOS): SEGV on proc_open macOS 26 SDK
1 parent 43b56c9 commit 020f600

2 files changed

Lines changed: 61 additions & 2 deletions

File tree

ext/standard/proc_open.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,31 @@
4545
#define USE_POSIX_SPAWN
4646

4747
/* The non-_np variant is in macOS 26 (and _np deprecated) */
48-
#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR
48+
#if defined(__APPLE__) && defined(HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR) && defined(HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR_NP)
49+
static inline int php_posix_spawn_file_actions_addchdir(posix_spawn_file_actions_t *actions, const char *path)
50+
{
51+
/* The standardized symbol is weak-linked when building with a newer SDK. */
52+
# if defined(__clang__)
53+
# pragma clang diagnostic push
54+
# pragma clang diagnostic ignored "-Wunguarded-availability-new"
55+
# endif
56+
if (posix_spawn_file_actions_addchdir != NULL) {
57+
return posix_spawn_file_actions_addchdir(actions, path);
58+
}
59+
# if defined(__clang__)
60+
# pragma clang diagnostic pop
61+
# endif
62+
63+
return posix_spawn_file_actions_addchdir_np(actions, path);
64+
}
65+
#define POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR php_posix_spawn_file_actions_addchdir
66+
#define POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR_NAME "posix_spawn_file_actions_addchdir"
67+
#elif defined(HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR)
4968
#define POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR posix_spawn_file_actions_addchdir
69+
#define POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR_NAME "posix_spawn_file_actions_addchdir"
5070
#else
5171
#define POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR posix_spawn_file_actions_addchdir_np
72+
#define POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR_NAME "posix_spawn_file_actions_addchdir_np"
5273
#endif
5374
#endif
5475

@@ -1401,7 +1422,7 @@ PHP_FUNCTION(proc_open)
14011422
if (cwd) {
14021423
r = POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR(&factions, cwd);
14031424
if (r != 0) {
1404-
php_error_docref(NULL, E_WARNING, ZEND_TOSTR(POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR) "() failed: %s", strerror(r));
1425+
php_error_docref(NULL, E_WARNING, POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR_NAME "() failed: %s", strerror(r));
14051426
}
14061427
}
14071428

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
proc_open() with cwd changes child working directory
3+
--SKIPIF--
4+
<?php
5+
if (!function_exists("proc_open")) echo "skip proc_open() is not available";
6+
?>
7+
--FILE--
8+
<?php
9+
$php = getenv('TEST_PHP_EXECUTABLE') ?: PHP_BINARY;
10+
$cwd = __DIR__ . DIRECTORY_SEPARATOR . 'proc_open_cwd_basic';
11+
@mkdir($cwd);
12+
13+
$descriptors = [
14+
1 => ['pipe', 'w'],
15+
2 => ['pipe', 'w'],
16+
];
17+
$proc = proc_open([$php, '-n', '-r', 'echo basename(getcwd()), "\n";'], $descriptors, $pipes, $cwd);
18+
19+
var_dump(is_resource($proc));
20+
echo stream_get_contents($pipes[1]);
21+
echo stream_get_contents($pipes[2]);
22+
fclose($pipes[1]);
23+
fclose($pipes[2]);
24+
var_dump(proc_close($proc));
25+
26+
rmdir($cwd);
27+
?>
28+
--CLEAN--
29+
<?php
30+
$cwd = __DIR__ . DIRECTORY_SEPARATOR . 'proc_open_cwd_basic';
31+
if (is_dir($cwd)) {
32+
rmdir($cwd);
33+
}
34+
?>
35+
--EXPECT--
36+
bool(true)
37+
proc_open_cwd_basic
38+
int(0)

0 commit comments

Comments
 (0)