Skip to content

Commit 1d05bba

Browse files
committed
Boolify functions in proc_open.c
1 parent a112c2d commit 1d05bba

File tree

1 file changed

+46
-45
lines changed

1 file changed

+46
-45
lines changed

ext/standard/proc_open.c

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -588,26 +588,26 @@ static void init_process_info(PROCESS_INFORMATION *pi)
588588
memset(&pi, 0, sizeof(pi));
589589
}
590590

591-
static int convert_command_to_use_shell(wchar_t **cmdw, size_t cmdw_len)
591+
static bool convert_command_to_use_shell(wchar_t **cmdw, size_t cmdw_len)
592592
{
593593
size_t len = sizeof(COMSPEC_NT) + sizeof(" /s /c ") + cmdw_len + 3;
594594
wchar_t *cmdw_shell = (wchar_t *)malloc(len * sizeof(wchar_t));
595595

596596
if (cmdw_shell == NULL) {
597597
php_error_docref(NULL, E_WARNING, "Command conversion failed");
598-
return FAILURE;
598+
return false;
599599
}
600600

601601
if (_snwprintf(cmdw_shell, len, L"%hs /s /c \"%s\"", COMSPEC_NT, *cmdw) == -1) {
602602
free(cmdw_shell);
603603
php_error_docref(NULL, E_WARNING, "Command conversion failed");
604-
return FAILURE;
604+
return false;
605605
}
606606

607607
free(*cmdw);
608608
*cmdw = cmdw_shell;
609609

610-
return SUCCESS;
610+
return true;
611611
}
612612
#endif
613613

@@ -659,26 +659,26 @@ static zend_string* get_string_parameter(zval *array, int index, char *param_nam
659659
return zval_try_get_string(array_item);
660660
}
661661

662-
static int set_proc_descriptor_to_blackhole(descriptorspec_item *desc)
662+
static bool set_proc_descriptor_to_blackhole(descriptorspec_item *desc)
663663
{
664664
#ifdef PHP_WIN32
665665
desc->childend = CreateFileA("nul", GENERIC_READ | GENERIC_WRITE,
666666
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
667667
if (desc->childend == NULL) {
668668
php_error_docref(NULL, E_WARNING, "Failed to open nul");
669-
return FAILURE;
669+
return false;
670670
}
671671
#else
672672
desc->childend = open("/dev/null", O_RDWR);
673673
if (desc->childend < 0) {
674674
php_error_docref(NULL, E_WARNING, "Failed to open /dev/null: %s", strerror(errno));
675-
return FAILURE;
675+
return false;
676676
}
677677
#endif
678-
return SUCCESS;
678+
return true;
679679
}
680680

681-
static int set_proc_descriptor_to_pty(descriptorspec_item *desc, int *master_fd, int *slave_fd)
681+
static bool set_proc_descriptor_to_pty(descriptorspec_item *desc, int *master_fd, int *slave_fd)
682682
{
683683
#if HAVE_OPENPTY
684684
/* All FDs set to PTY in the child process will go to the slave end of the same PTY.
@@ -689,18 +689,18 @@ static int set_proc_descriptor_to_pty(descriptorspec_item *desc, int *master_fd,
689689
if (*master_fd == -1) {
690690
if (openpty(master_fd, slave_fd, NULL, NULL, NULL)) {
691691
php_error_docref(NULL, E_WARNING, "Could not open PTY (pseudoterminal): %s", strerror(errno));
692-
return FAILURE;
692+
return false;
693693
}
694694
}
695695

696696
desc->type = DESCRIPTOR_TYPE_PIPE;
697697
desc->childend = dup(*slave_fd);
698698
desc->parentend = dup(*master_fd);
699699
desc->mode_flags = O_RDWR;
700-
return SUCCESS;
700+
return true;
701701
#else
702702
php_error_docref(NULL, E_WARNING, "PTY (pseudoterminal) not supported on this system");
703-
return FAILURE;
703+
return false;
704704
#endif
705705
}
706706

@@ -717,13 +717,13 @@ static php_file_descriptor_t make_descriptor_cloexec(php_file_descriptor_t fd)
717717
#endif
718718
}
719719

720-
static int set_proc_descriptor_to_pipe(descriptorspec_item *desc, zend_string *zmode)
720+
static bool set_proc_descriptor_to_pipe(descriptorspec_item *desc, zend_string *zmode)
721721
{
722722
php_file_descriptor_t newpipe[2];
723723

724724
if (pipe(newpipe)) {
725725
php_error_docref(NULL, E_WARNING, "Unable to create pipe %s", strerror(errno));
726-
return FAILURE;
726+
return false;
727727
}
728728

729729
desc->type = DESCRIPTOR_TYPE_PIPE;
@@ -745,7 +745,7 @@ static int set_proc_descriptor_to_pipe(descriptorspec_item *desc, zend_string *z
745745
desc->mode_flags |= O_BINARY;
746746
#endif
747747

748-
return SUCCESS;
748+
return true;
749749
}
750750

751751
#ifdef PHP_WIN32
@@ -754,15 +754,15 @@ static int set_proc_descriptor_to_pipe(descriptorspec_item *desc, zend_string *z
754754
#define create_socketpair(socks) socketpair(AF_UNIX, SOCK_STREAM, 0, (socks))
755755
#endif
756756

757-
static int set_proc_descriptor_to_socket(descriptorspec_item *desc)
757+
static bool set_proc_descriptor_to_socket(descriptorspec_item *desc)
758758
{
759759
php_socket_t sock[2];
760760

761761
if (create_socketpair(sock)) {
762762
zend_string *err = php_socket_error_str(php_socket_errno());
763763
php_error_docref(NULL, E_WARNING, "Unable to create socket pair: %s", ZSTR_VAL(err));
764764
zend_string_release(err);
765-
return FAILURE;
765+
return false;
766766
}
767767

768768
desc->type = DESCRIPTOR_TYPE_SOCKET;
@@ -771,10 +771,10 @@ static int set_proc_descriptor_to_socket(descriptorspec_item *desc)
771771
/* Pass sock[1] to child because it will never use overlapped IO on Windows. */
772772
desc->childend = (php_file_descriptor_t) sock[1];
773773

774-
return SUCCESS;
774+
return true;
775775
}
776776

777-
static int set_proc_descriptor_to_file(descriptorspec_item *desc, zend_string *file_path,
777+
static bool set_proc_descriptor_to_file(descriptorspec_item *desc, zend_string *file_path,
778778
zend_string *file_mode)
779779
{
780780
php_socket_t fd;
@@ -783,13 +783,13 @@ static int set_proc_descriptor_to_file(descriptorspec_item *desc, zend_string *f
783783
php_stream *stream = php_stream_open_wrapper(ZSTR_VAL(file_path), ZSTR_VAL(file_mode),
784784
REPORT_ERRORS|STREAM_WILL_CAST, NULL);
785785
if (stream == NULL) {
786-
return FAILURE;
786+
return false;
787787
}
788788

789789
/* force into an fd */
790790
if (php_stream_cast(stream, PHP_STREAM_CAST_RELEASE|PHP_STREAM_AS_FD, (void **)&fd,
791791
REPORT_ERRORS) == FAILURE) {
792-
return FAILURE;
792+
return false;
793793
}
794794

795795
#ifdef PHP_WIN32
@@ -804,30 +804,30 @@ static int set_proc_descriptor_to_file(descriptorspec_item *desc, zend_string *f
804804
#else
805805
desc->childend = fd;
806806
#endif
807-
return SUCCESS;
807+
return true;
808808
}
809809

810-
static int dup_proc_descriptor(php_file_descriptor_t from, php_file_descriptor_t *to,
810+
static bool dup_proc_descriptor(php_file_descriptor_t from, php_file_descriptor_t *to,
811811
size_t nindex)
812812
{
813813
#ifdef PHP_WIN32
814814
*to = dup_handle(from, TRUE, FALSE);
815815
if (*to == NULL) {
816816
php_error_docref(NULL, E_WARNING, "Failed to dup() for descriptor %zu", nindex);
817-
return FAILURE;
817+
return false;
818818
}
819819
#else
820820
*to = dup(from);
821821
if (*to < 0) {
822822
php_error_docref(NULL, E_WARNING, "Failed to dup() for descriptor %zu: %s",
823823
nindex, strerror(errno));
824-
return FAILURE;
824+
return false;
825825
}
826826
#endif
827-
return SUCCESS;
827+
return true;
828828
}
829829

830-
static int redirect_proc_descriptor(descriptorspec_item *desc, int target,
830+
static bool redirect_proc_descriptor(descriptorspec_item *desc, int target,
831831
descriptorspec_item *descriptors, size_t ndesc, size_t nindex)
832832
{
833833
php_file_descriptor_t redirect_to = PHP_INVALID_FD;
@@ -842,7 +842,7 @@ static int redirect_proc_descriptor(descriptorspec_item *desc, int target,
842842
if (redirect_to == PHP_INVALID_FD) { /* Didn't find the index we wanted */
843843
if (target < 0 || target > 2) {
844844
php_error_docref(NULL, E_WARNING, "Redirection target %d not found", target);
845-
return FAILURE;
845+
return false;
846846
}
847847

848848
/* Support referring to a stdin/stdout/stderr pipe adopted from the parent,
@@ -863,15 +863,16 @@ static int redirect_proc_descriptor(descriptorspec_item *desc, int target,
863863
}
864864

865865
/* Process one item from `$descriptorspec` argument to `proc_open` */
866-
static int set_proc_descriptor_from_array(zval *descitem, descriptorspec_item *descriptors,
866+
static bool set_proc_descriptor_from_array(zval *descitem, descriptorspec_item *descriptors,
867867
size_t ndesc, size_t nindex, int *pty_master_fd, int *pty_slave_fd) {
868868
zend_string *ztype = get_string_parameter(descitem, 0, "handle qualifier");
869869
if (!ztype) {
870-
return FAILURE;
870+
return false;
871871
}
872872

873873
zend_string *zmode = NULL, *zfile = NULL;
874-
int retval = FAILURE;
874+
bool retval = false;
875+
875876
if (zend_string_equals_literal(ztype, "pipe")) {
876877
/* Set descriptor to pipe */
877878
zmode = get_string_parameter(descitem, 1, "mode parameter for 'pipe'");
@@ -923,35 +924,35 @@ static int set_proc_descriptor_from_array(zval *descitem, descriptorspec_item *d
923924
return retval;
924925
}
925926

926-
static int set_proc_descriptor_from_resource(zval *resource, descriptorspec_item *desc, size_t nindex)
927+
static bool set_proc_descriptor_from_resource(zval *resource, descriptorspec_item *desc, size_t nindex)
927928
{
928929
/* Should be a stream - try and dup the descriptor */
929930
php_stream *stream = (php_stream*)zend_fetch_resource(Z_RES_P(resource), "stream",
930931
php_file_le_stream());
931932
if (stream == NULL) {
932-
return FAILURE;
933+
return false;
933934
}
934935

935936
php_socket_t fd;
936937
int status = php_stream_cast(stream, PHP_STREAM_AS_FD, (void **)&fd, REPORT_ERRORS);
937938
if (status == FAILURE) {
938-
return FAILURE;
939+
return false;
939940
}
940941

941942
#ifdef PHP_WIN32
942943
php_file_descriptor_t fd_t = (php_file_descriptor_t)_get_osfhandle(fd);
943944
#else
944945
php_file_descriptor_t fd_t = fd;
945946
#endif
946-
if (dup_proc_descriptor(fd_t, &desc->childend, nindex) == FAILURE) {
947-
return FAILURE;
947+
if (!dup_proc_descriptor(fd_t, &desc->childend, nindex)) {
948+
return false;
948949
}
949950

950-
return SUCCESS;
951+
return true;
951952
}
952953

953954
#ifndef PHP_WIN32
954-
static int close_parentends_of_pipes(descriptorspec_item *descriptors, size_t ndesc)
955+
static bool close_parentends_of_pipes(descriptorspec_item *descriptors, size_t ndesc)
955956
{
956957
/* We are running in child process
957958
* Close the 'parent end' of pipes which were opened before forking/spawning
@@ -965,13 +966,13 @@ static int close_parentends_of_pipes(descriptorspec_item *descriptors, size_t nd
965966
if (dup2(descriptors[i].childend, descriptors[i].index) < 0) {
966967
php_error_docref(NULL, E_WARNING, "Unable to copy file descriptor %d (for pipe) into " \
967968
"file descriptor %d: %s", descriptors[i].childend, descriptors[i].index, strerror(errno));
968-
return FAILURE;
969+
return false;
969970
}
970971
close(descriptors[i].childend);
971972
}
972973
}
973974

974-
return SUCCESS;
975+
return true;
975976
}
976977
#endif
977978

@@ -1096,12 +1097,12 @@ PHP_FUNCTION(proc_open)
10961097
descriptors[ndesc].index = nindex;
10971098

10981099
if (Z_TYPE_P(descitem) == IS_RESOURCE) {
1099-
if (set_proc_descriptor_from_resource(descitem, &descriptors[ndesc], ndesc) == FAILURE) {
1100+
if (!set_proc_descriptor_from_resource(descitem, &descriptors[ndesc], ndesc)) {
11001101
goto exit_fail;
11011102
}
11021103
} else if (Z_TYPE_P(descitem) == IS_ARRAY) {
1103-
if (set_proc_descriptor_from_array(descitem, descriptors, ndesc, nindex,
1104-
&pty_master_fd, &pty_slave_fd) == FAILURE) {
1104+
if (!set_proc_descriptor_from_array(descitem, descriptors, ndesc, nindex,
1105+
&pty_master_fd, &pty_slave_fd)) {
11051106
goto exit_fail;
11061107
}
11071108
} else {
@@ -1160,7 +1161,7 @@ PHP_FUNCTION(proc_open)
11601161
}
11611162

11621163
if (!bypass_shell) {
1163-
if (convert_command_to_use_shell(&cmdw, cmdw_len) == FAILURE) {
1164+
if (!convert_command_to_use_shell(&cmdw, cmdw_len)) {
11641165
goto exit_fail;
11651166
}
11661167
}
@@ -1188,7 +1189,7 @@ PHP_FUNCTION(proc_open)
11881189
if (child == 0) {
11891190
/* This is the child process */
11901191

1191-
if (close_parentends_of_pipes(descriptors, ndesc) == FAILURE) {
1192+
if (!close_parentends_of_pipes(descriptors, ndesc)) {
11921193
/* We are already in child process and can't do anything to make
11931194
* `proc_open` return an error in the parent
11941195
* All we can do is exit with a non-zero (error) exit code */

0 commit comments

Comments
 (0)