Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ PHP NEWS
unsigned int timeout. (Weilin Du)
. Fixed bug GH-22671 (assert.bail aborts the process when the assert callback
throws an exception whose reporting re-throws). (iliaal)
. Fixed bug GH-22678 (Use-after-free in array_multisort() when the comparator
mutates the array being sorted). (azchin, iliaal)

- Streams:
. Fixed bug GH-21468 (Segfault in file_get_contents w/ a https URL
Expand Down
16 changes: 7 additions & 9 deletions ext/dom/obj_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,22 +345,20 @@ static void dom_map_get_by_class_name_item(dom_nnodemap_object *map, zend_long i
if (nodep && index >= 0) {
dom_node_idx_pair start_point = dom_obj_map_get_start_point(map, nodep, index);
if (start_point.node) {
if (start_point.index > 0) {
/* Only start iteration at next point if we actually have an index to seek to. */
itemnode = php_dom_next_in_tree_order(start_point.node, nodep);
} else {
itemnode = start_point.node;
}
itemnode = start_point.node;
} else {
itemnode = php_dom_first_child_of_container_node(nodep);
while (itemnode != NULL && !dom_matches_class_name(map, itemnode)) {
itemnode = php_dom_next_in_tree_order(itemnode, nodep);
}
}

do {
--start_point.index;
for (; start_point.index > 0 && itemnode != NULL; --start_point.index) {
itemnode = php_dom_next_in_tree_order(itemnode, nodep);
while (itemnode != NULL && !dom_matches_class_name(map, itemnode)) {
itemnode = php_dom_next_in_tree_order(itemnode, nodep);
}
} while (start_point.index > 0 && itemnode);
}
}
dom_ret_node_to_zobj(map, itemnode, return_value);
if (itemnode) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
--TEST--
Dom\Element::getElementsByClassName() item() random access (cold and backwards)
--EXTENSIONS--
dom
--FILE--
<?php

/* Regression: the item() lookup must return the n-th match for random access,
* not only for a strictly ascending / foreach walk. A fresh collection whose
* first access is item(n) exercises the uncached path, and a decreasing index
* on the same collection exercises the cache-discard path. Non-matching and
* nested elements verify that iteration advances to the next matching element
* in tree order, not merely to the next sibling. */

$dom = Dom\HTMLDocument::createFromString(<<<HTML
<!DOCTYPE html>
<body>
<span class="x" id="E0"></span>
<div class="y" id="skip1"><span class="x" id="E1"></span></div>
<span class="x" id="E2"></span>
<p class="z"><b class="x" id="E3"></b></p>
<span class="x" id="E4"></span>
</body>
HTML);

$body = $dom->getElementsByTagName('body')->item(0);

function id(?Dom\Element $e): string {
return $e === null ? 'NULL' : $e->id;
}

echo "-- cold random access (fresh collection per call) --\n";
foreach ([0, 1, 2, 3, 4, 5] as $i) {
$collection = $body->getElementsByClassName('x');
echo "item($i) = ", id($collection->item($i)), "\n";
}

echo "-- backwards seek on one collection --\n";
$collection = $body->getElementsByClassName('x');
foreach ([4, 2, 0, 3, 1] as $i) {
echo "item($i) = ", id($collection->item($i)), "\n";
}

echo "-- item() seed then foreach --\n";
$collection = $body->getElementsByClassName('x');
$collection->item(3);
$ids = [];
foreach ($collection as $node) {
$ids[] = $node->id;
}
echo implode(" ", $ids), "\n";

echo "-- last-element idiom --\n";
$collection = $body->getElementsByClassName('x');
echo "length = ", $collection->length, ", last = ", id($collection->item($collection->length - 1)), "\n";

echo "-- live collection after mutation --\n";
$collection = $body->getElementsByClassName('x');
echo "item(1) = ", id($collection->item(1)), "\n";
$dom->getElementById('E1')->remove();
echo "item(1) = ", id($collection->item(1)), ", length = ", $collection->length, "\n";

?>
--EXPECT--
-- cold random access (fresh collection per call) --
item(0) = E0
item(1) = E1
item(2) = E2
item(3) = E3
item(4) = E4
item(5) = NULL
-- backwards seek on one collection --
item(4) = E4
item(2) = E2
item(0) = E0
item(3) = E3
item(1) = E1
-- item() seed then foreach --
E0 E1 E2 E3 E4
-- last-element idiom --
length = 5, last = E4
-- live collection after mutation --
item(1) = E1
item(1) = E2, length = 4
53 changes: 53 additions & 0 deletions ext/ftp/ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,11 @@ bool ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_
if (ftp == NULL) {
return false;
}
if (ftp->in_use) {
php_error_docref(NULL, E_WARNING, "FTP\\Connection is already in use");
return false;
}
ftp->in_use = true;
if (!ftp_type(ftp, type)) {
goto bail;
}
Expand Down Expand Up @@ -913,9 +918,11 @@ bool ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_
goto bail;
}

ftp->in_use = false;
return true;
bail:
data_close(ftp);
ftp->in_use = false;
return false;
}

Expand Down Expand Up @@ -1000,6 +1007,11 @@ bool ftp_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream
if (ftp == NULL) {
return false;
}
if (ftp->in_use) {
php_error_docref(NULL, E_WARNING, "FTP\\Connection is already in use");
return false;
}
ftp->in_use = true;
if (!ftp_type(ftp, type)) {
goto bail;
}
Expand Down Expand Up @@ -1040,9 +1052,11 @@ bool ftp_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream
if (!ftp_getresp(ftp) || (ftp->resp != 226 && ftp->resp != 250 && ftp->resp != 200)) {
goto bail;
}
ftp->in_use = false;
return true;
bail:
data_close(ftp);
ftp->in_use = false;
return false;
}

Expand All @@ -1053,6 +1067,11 @@ bool ftp_append(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stre
if (ftp == NULL) {
return false;
}
if (ftp->in_use) {
php_error_docref(NULL, E_WARNING, "FTP\\Connection is already in use");
return false;
}
ftp->in_use = true;
if (!ftp_type(ftp, type)) {
goto bail;
}
Expand Down Expand Up @@ -1080,9 +1099,11 @@ bool ftp_append(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stre
if (!ftp_getresp(ftp) || (ftp->resp != 226 && ftp->resp != 250 && ftp->resp != 200)) {
goto bail;
}
ftp->in_use = false;
return true;
bail:
data_close(ftp);
ftp->in_use = false;
return false;
}

Expand Down Expand Up @@ -1939,6 +1960,10 @@ static char** ftp_genlist(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len,
char **entry;
char *text;

if (ftp->in_use) {
php_error_docref(NULL, E_WARNING, "FTP\\Connection is already in use");
return NULL;
}

if ((tmpstream = php_stream_fopen_tmpfile()) == NULL) {
php_error_docref(NULL, E_WARNING, "Unable to create temporary file. Check permissions in temporary files directory.");
Expand Down Expand Up @@ -2037,6 +2062,11 @@ int ftp_nb_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const siz
return PHP_FTP_FAILED;
}

if (ftp->in_use) {
php_error_docref(NULL, E_WARNING, "FTP\\Connection is already in use");
return PHP_FTP_FAILED;
}

if (ftp->data != NULL) {
/* If there is a transfer in action, abort it.
* If we don't, we get an invalid state and memory leaks when the new connection gets opened. */
Expand Down Expand Up @@ -2101,11 +2131,17 @@ int ftp_nb_continue_read(ftpbuf_t *ftp)

data = ftp->data;

if (ftp->in_use) {
php_error_docref(NULL, E_WARNING, "FTP\\Connection is already in use");
return PHP_FTP_FAILED;
}

/* check if there is already more data */
if (!data_available(ftp, data->fd, false)) {
return PHP_FTP_MOREDATA;
}

ftp->in_use = true;
type = ftp->type;

lastch = ftp->lastch;
Expand All @@ -2129,6 +2165,7 @@ int ftp_nb_continue_read(ftpbuf_t *ftp)
}

ftp->lastch = lastch;
ftp->in_use = false;
return PHP_FTP_MOREDATA;
}

Expand All @@ -2143,9 +2180,11 @@ int ftp_nb_continue_read(ftpbuf_t *ftp)
}

ftp->nb = false;
ftp->in_use = false;
return PHP_FTP_FINISHED;
bail:
ftp->nb = false;
ftp->in_use = false;
data_close(ftp);
return PHP_FTP_FAILED;
}
Expand All @@ -2158,6 +2197,10 @@ int ftp_nb_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_strea
if (ftp == NULL) {
return 0;
}
if (ftp->in_use) {
php_error_docref(NULL, E_WARNING, "FTP\\Connection is already in use");
return PHP_FTP_FAILED;
}
if (!ftp_type(ftp, type)) {
goto bail;
}
Expand Down Expand Up @@ -2201,16 +2244,24 @@ int ftp_nb_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_strea

int ftp_nb_continue_write(ftpbuf_t *ftp)
{
if (ftp->in_use) {
php_error_docref(NULL, E_WARNING, "FTP\\Connection is already in use");
return PHP_FTP_FAILED;
}

/* check if we can write more data */
if (!data_writeable(ftp, ftp->data->fd)) {
return PHP_FTP_MOREDATA;
}

ftp->in_use = true;

if (ftp_send_stream_to_data_socket(ftp, ftp->data, ftp->stream, ftp->type, true) != SUCCESS) {
goto bail;
}

if (!php_stream_eof(ftp->stream)) {
ftp->in_use = false;
return PHP_FTP_MOREDATA;
}

Expand All @@ -2220,9 +2271,11 @@ int ftp_nb_continue_write(ftpbuf_t *ftp)
goto bail;
}
ftp->nb = false;
ftp->in_use = false;
return PHP_FTP_FINISHED;
bail:
data_close(ftp);
ftp->nb = false;
ftp->in_use = false;
return PHP_FTP_FAILED;
}
1 change: 1 addition & 0 deletions ext/ftp/ftp.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ typedef struct ftpbuf
databuf_t *data; /* Data connection for "nonblocking" transfers */
php_stream *stream; /* output stream for "nonblocking" transfers */
bool nb; /* "nonblocking" transfer in progress */
bool in_use; /* engine transfer in progress; blocks re-entrant ftp_close */
char lastch; /* last char of previous call */
bool direction; /* recv = 0 / send = 1 */
bool closestream;/* close or not close stream */
Expand Down
Loading