Skip to content

Commit 62f6c58

Browse files
committed
Fixes to various stream cast on win64
This fixes further issues on win64 with casts from the streams. Sockets/descriptors handling was unitized. This has an impact only on win64, php_socket_t otherwise can be feed back to int datatype.
1 parent a6f7fca commit 62f6c58

File tree

6 files changed

+23
-24
lines changed

6 files changed

+23
-24
lines changed

ext/bz2/bz2.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "ext/standard/file.h"
3232
#include "ext/standard/info.h"
3333
#include "ext/standard/php_string.h"
34+
#include "main/php_network.h"
3435

3536
/* for fileno() */
3637
#include <stdio.h>
@@ -245,7 +246,7 @@ PHP_BZ2_API php_stream *_php_stream_bz2open(php_stream_wrapper *wrapper,
245246
stream = php_stream_open_wrapper(path, mode, options | STREAM_WILL_CAST, opened_path);
246247

247248
if (stream) {
248-
int fd;
249+
php_socket_t fd;
249250
if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD, (void **) &fd, REPORT_ERRORS)) {
250251
bz_file = BZ2_bzdopen(fd, mode);
251252
}
@@ -394,7 +395,7 @@ static PHP_FUNCTION(bzopen)
394395
NULL);
395396
} else if (Z_TYPE_PP(file) == IS_RESOURCE) {
396397
/* If it is a resource, than its a stream resource */
397-
int fd;
398+
php_socket_t fd;
398399
int stream_mode_len;
399400

400401
php_stream_from_zval(stream, file);

ext/fileinfo/libmagic/funcs.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ FILE_RCSID("@(#)$File: funcs.c,v 1.67 2014/02/12 23:20:53 christos Exp $")
4949
# define SIZE_MAX ((size_t) -1)
5050
#endif
5151

52+
#include "php.h"
53+
#include "main/php_network.h"
54+
5255
#ifndef PREG_OFFSET_CAPTURE
5356
# define PREG_OFFSET_CAPTURE (1<<8)
5457
#endif
@@ -218,7 +221,7 @@ file_buffer(struct magic_set *ms, php_stream *stream, const char *inname, const
218221

219222
/* Check if we have a CDF file */
220223
if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0) {
221-
int fd;
224+
php_socket_t fd;
222225
TSRMLS_FETCH();
223226
if (stream && SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD, (void **)&fd, 0)) {
224227
if ((m = file_trycdf(ms, fd, ubuf, nb)) != 0) {

ext/standard/proc_open.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "exec.h"
3535
#include "php_globals.h"
3636
#include "SAPI.h"
37+
#include "main/php_network.h"
3738

3839
#ifdef NETWARE
3940
#include <proc.h>
@@ -556,7 +557,7 @@ PHP_FUNCTION(proc_open)
556557
if (Z_TYPE_PP(descitem) == IS_RESOURCE) {
557558
/* should be a stream - try and dup the descriptor */
558559
php_stream *stream;
559-
int fd;
560+
php_socket_t fd;
560561

561562
php_stream_from_zval(stream, descitem);
562563

@@ -629,7 +630,7 @@ PHP_FUNCTION(proc_open)
629630

630631
} else if (strcmp(Z_STRVAL_PP(ztype), "file") == 0) {
631632
zval **zfile, **zmode;
632-
int fd;
633+
php_socket_t fd;
633634
php_stream *stream;
634635

635636
descriptors[ndesc].mode = DESC_FILE;

ext/standard/streamsfuncs.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ static int stream_array_to_fd_set(zval *stream_array, fd_set *fds, php_socket_t
613613
/* Temporary int fd is needed for the STREAM data type on windows, passing this_fd directly to php_stream_cast()
614614
would eventually bring a wrong result on x64. php_stream_cast() casts to int internally, and this will leave
615615
the higher bits of a SOCKET variable uninitialized on systems with little endian. */
616-
int tmp_fd;
616+
php_socket_t this_fd;
617617

618618
php_stream_from_zval_no_verify(stream, elem);
619619
if (stream == NULL) {
@@ -624,9 +624,7 @@ static int stream_array_to_fd_set(zval *stream_array, fd_set *fds, php_socket_t
624624
* when casting. It is only used here so that the buffered data warning
625625
* is not displayed.
626626
* */
627-
if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&tmp_fd, 1) && tmp_fd != -1) {
628-
629-
php_socket_t this_fd = (php_socket_t)tmp_fd;
627+
if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&this_fd, 1) && this_fd != -1) {
630628

631629
PHP_SAFE_FD_SET(this_fd, fds);
632630

@@ -660,10 +658,7 @@ static int stream_array_from_fd_set(zval *stream_array, fd_set *fds TSRMLS_DC)
660658
char *key;
661659
uint key_len;
662660
ulong num_ind;
663-
/* Temporary int fd is needed for the STREAM data type on windows, passing this_fd directly to php_stream_cast()
664-
would eventually bring a wrong result on x64. php_stream_cast() casts to int internally, and this will leave
665-
the higher bits of a SOCKET variable uninitialized on systems with little endian. */
666-
int tmp_fd;
661+
php_socket_t this_fd;
667662

668663

669664
type = zend_hash_get_current_key_ex(Z_ARRVAL_P(stream_array),
@@ -682,10 +677,7 @@ static int stream_array_from_fd_set(zval *stream_array, fd_set *fds TSRMLS_DC)
682677
* when casting. It is only used here so that the buffered data warning
683678
* is not displayed.
684679
*/
685-
if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&tmp_fd, 1) && tmp_fd != -1) {
686-
687-
php_socket_t this_fd = (php_socket_t)tmp_fd;
688-
680+
if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&this_fd, 1) && this_fd != SOCK_ERR) {
689681
if (PHP_SAFE_FD_ISSET(this_fd, fds)) {
690682
if (type == HASH_KEY_IS_LONG) {
691683
zend_hash_index_update(new_hash, num_ind, (void *)elem, sizeof(zval *), (void **)&dest_elem);

ext/zlib/zlib_fopen_wrapper.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include "php_zlib.h"
2626
#include "fopen_wrappers.h"
2727

28+
#include "main/php_network.h"
29+
2830
struct php_gz_stream_data_t {
2931
gzFile gz_file;
3032
php_stream *stream;
@@ -129,7 +131,7 @@ php_stream *php_stream_gzopen(php_stream_wrapper *wrapper, const char *path, con
129131
innerstream = php_stream_open_wrapper_ex(path, mode, STREAM_MUST_SEEK | options | STREAM_WILL_CAST, opened_path, context);
130132

131133
if (innerstream) {
132-
int fd;
134+
php_socket_t fd;
133135

134136
if (SUCCESS == php_stream_cast(innerstream, PHP_STREAM_AS_FD, (void **) &fd, REPORT_ERRORS)) {
135137
self = emalloc(sizeof(*self));

main/streams/plain_wrapper.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ static int php_stdiop_seek(php_stream *stream, off_t offset, int whence, off_t *
482482

483483
static int php_stdiop_cast(php_stream *stream, int castas, void **ret TSRMLS_DC)
484484
{
485-
int fd;
485+
php_socket_t fd;
486486
php_stdio_stream_data *data = (php_stdio_stream_data*) stream->abstract;
487487

488488
assert(data != NULL);
@@ -506,31 +506,31 @@ static int php_stdiop_cast(php_stream *stream, int castas, void **ret TSRMLS_DC)
506506
}
507507

508508
*(FILE**)ret = data->file;
509-
data->fd = -1;
509+
data->fd = SOCK_ERR;
510510
}
511511
return SUCCESS;
512512

513513
case PHP_STREAM_AS_FD_FOR_SELECT:
514514
PHP_STDIOP_GET_FD(fd, data);
515-
if (fd < 0) {
515+
if (SOCK_ERR == fd) {
516516
return FAILURE;
517517
}
518518
if (ret) {
519-
*(int*)ret = fd;
519+
*(php_socket_t *)ret = fd;
520520
}
521521
return SUCCESS;
522522

523523
case PHP_STREAM_AS_FD:
524524
PHP_STDIOP_GET_FD(fd, data);
525525

526-
if (fd < 0) {
526+
if (SOCK_ERR == fd) {
527527
return FAILURE;
528528
}
529529
if (data->file) {
530530
fflush(data->file);
531531
}
532532
if (ret) {
533-
*(int*)ret = fd;
533+
*(php_socket_t *)ret = fd;
534534
}
535535
return SUCCESS;
536536
default:

0 commit comments

Comments
 (0)