Skip to content

Commit 6122f67

Browse files
committed
Merge branch 'PHP-7.2' into PHP-7.3
2 parents 1640d91 + fcf78df commit 6122f67

File tree

4 files changed

+62
-17
lines changed

4 files changed

+62
-17
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ PHP NEWS
1818
- Standard:
1919
. Fixed bug #76803 (ftruncate changes file pointer). (Anatol)
2020
. Fixed bug #76818 (Memory corruption and segfault). (Remi)
21+
. Fixed bug #73457 (Wrong error message when fopen FTP wrapped fails to open
22+
data connection). (Ville Hukkamäki)
2123

2224
30 Aug 2018, PHP 7.3.0beta3
2325

ext/ftp/tests/server.inc

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ if ($pid) {
349349
fputs($s, "550 No file named \"{$matches [1]}\"\r\n");
350350
break;
351351
}
352-
}elseif (preg_match('/^RETR ([\w\h]+)/', $buf, $matches)) {
352+
}elseif (preg_match('/^RETR ([\/]*[\w\h]+)/', $buf, $matches)) {
353353
if(!empty($pasv)){
354354
;
355355
}
@@ -405,6 +405,10 @@ if ($pid) {
405405
fputs($fs, "This is line $i of the test data.\n");
406406
}
407407
fputs($s, "226 Closing data Connection.\r\n");
408+
break;
409+
case "/bug73457":
410+
fputs($s, "150 File status okay; about to open data connection.\r\n");
411+
break;
408412

409413
default:
410414
fputs($s, "550 {$matches[1]}: No such file or directory \r\n");
@@ -419,29 +423,35 @@ if ($pid) {
419423
$host = "127.0.0.1";
420424
$i=0;
421425

422-
do {
423-
if (!empty($ssl)) {
424-
$soc = @stream_socket_server("tcp://127.0.0.1:$pasv_port", $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context);
425-
} else {
426-
$soc = @stream_socket_server("tcp://127.0.0.1:$pasv_port");
427-
}
428-
/* Could bind port, Try another port */
426+
if (empty($bug73457)) {
427+
do {
428+
if (!empty($ssl)) {
429+
$soc = @stream_socket_server("tcp://127.0.0.1:$pasv_port", $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context);
430+
} else {
431+
$soc = @stream_socket_server("tcp://127.0.0.1:$pasv_port");
432+
}
433+
/* Could bind port, Try another port */
434+
if (!$soc) {
435+
$pasv_port = rand(50000, 65535);
436+
}
437+
$i++;
438+
} while ($i<10 && !$soc);
439+
429440
if (!$soc) {
430-
$pasv_port = rand(50000, 65535);
441+
echo "$errstr ($errno)\n";
442+
die("could not bind passive port\n");
431443
}
432-
$i++;
433-
} while ($i<10 && !$soc);
434-
435-
if (!$soc) {
436-
echo "$errstr ($errno)\n";
437-
die("could not bind passive port\n");
444+
} else {
445+
$pasv_port=1234;
438446
}
439447

440448
$p2 = $pasv_port % ((int) 1 << 8);
441449
$p1 = ($pasv_port-$p2)/((int) 1 << 8);
442450
fputs($s, "227 Entering Passive Mode. (127,0,0,1,{$p1},{$p2})\r\n");
443451

444-
$pasvs = stream_socket_accept($soc,10);
452+
if (empty($bug73457)) {
453+
$pasvs = stream_socket_accept($soc,10);
454+
}
445455

446456
} elseif (preg_match('/^EPSV/', $buf, $matches)) {
447457
fputs($s, "550 Extended passsive mode not supported.\r\n");
@@ -519,6 +529,8 @@ if ($pid) {
519529

520530
fputs($s, "226 Closing data Connection.\r\n");
521531
fclose($fs);
532+
}elseif (preg_match('/^SIZE \/bug73457/', $buf)) {
533+
fputs($s, "213 10\r\n");
522534
}elseif (preg_match("/^SITE/", $buf)) {
523535
fputs($s, "500 Syntax error, command unrecognized.\r\n");
524536
}else {

ext/standard/ftp_fopen_wrapper.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ php_stream * php_stream_url_wrap_ftp(php_stream_wrapper *wrapper, const char *pa
427427
int8_t read_write = 0;
428428
char *transport;
429429
int transport_len;
430+
zend_string *error_message = NULL;
430431

431432
tmp_line[0] = '\0';
432433

@@ -554,9 +555,10 @@ php_stream * php_stream_url_wrap_ftp(php_stream_wrapper *wrapper, const char *pa
554555
hoststart = ZSTR_VAL(resource->host);
555556
}
556557
transport_len = (int)spprintf(&transport, 0, "tcp://%s:%d", hoststart, portno);
557-
datastream = php_stream_xport_create(transport, transport_len, REPORT_ERRORS, STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, NULL, NULL, context, NULL, NULL);
558+
datastream = php_stream_xport_create(transport, transport_len, REPORT_ERRORS, STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, NULL, NULL, context, &error_message, NULL);
558559
efree(transport);
559560
if (datastream == NULL) {
561+
tmp_line[0]='\0';
560562
goto errexit;
561563
}
562564

@@ -580,6 +582,7 @@ php_stream * php_stream_url_wrap_ftp(php_stream_wrapper *wrapper, const char *pa
580582
php_stream_wrapper_log_error(wrapper, options, "Unable to activate SSL mode");
581583
php_stream_close(datastream);
582584
datastream = NULL;
585+
tmp_line[0]='\0';
583586
goto errexit;
584587
}
585588

@@ -599,6 +602,11 @@ php_stream * php_stream_url_wrap_ftp(php_stream_wrapper *wrapper, const char *pa
599602
}
600603
if (tmp_line[0] != '\0')
601604
php_stream_wrapper_log_error(wrapper, options, "FTP server reports %s", tmp_line);
605+
606+
if (error_message) {
607+
php_stream_wrapper_log_error(wrapper, options, "Failed to set up data channel: %s", ZSTR_VAL(error_message));
608+
zend_string_release(error_message);
609+
}
602610
return NULL;
603611
}
604612
/* }}} */
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Bug #73457. Wrong error message when fopen FTP wrapped fails to open data connection
3+
--SKIPIF--
4+
<?php
5+
if (array_search('ftp',stream_get_wrappers()) === FALSE) die("skip ftp wrapper not available.");
6+
if (!function_exists('pcntl_fork')) die("skip pcntl_fork() not available.");
7+
?>
8+
--FILE--
9+
<?php
10+
11+
$bug73457=true;
12+
require __DIR__ . "/../../../ftp/tests/server.inc";
13+
14+
$path="ftp://127.0.0.1:" . $port."/bug73457";
15+
16+
$ds=file_get_contents($path);
17+
var_dump($ds);
18+
?>
19+
==DONE==
20+
--EXPECTF--
21+
Warning: file_get_contents(ftp://127.0.0.1:%d/bug73457): failed to open stream: Failed to set up data channel: Connection refused in %s on line %d
22+
bool(false)
23+
==DONE==

0 commit comments

Comments
 (0)