Skip to content

Commit c6ccc43

Browse files
committed
- Fixed several comparisons that always result in true of false
due to signedness of one of the operands, either by removing dead code or fixing it. - Thrown some comments around in php_stream_get_record. - See http://www.mail-archive.com/internals@lists.php.net/msg49525.html
1 parent 44cd358 commit c6ccc43

File tree

4 files changed

+19
-14
lines changed

4 files changed

+19
-14
lines changed

ext/standard/streamsfuncs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ static int stream_array_to_fd_set(zval *stream_array, fd_set *fds, php_socket_t
634634
* when casting. It is only used here so that the buffered data warning
635635
* is not displayed.
636636
* */
637-
if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&this_fd, 1) && this_fd >= 0) {
637+
if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&this_fd, 1) && this_fd != -1) {
638638

639639
PHP_SAFE_FD_SET(this_fd, fds);
640640

@@ -686,7 +686,7 @@ static int stream_array_from_fd_set(zval *stream_array, fd_set *fds TSRMLS_DC)
686686
* when casting. It is only used here so that the buffered data warning
687687
* is not displayed.
688688
*/
689-
if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&this_fd, 1) && this_fd >= 0) {
689+
if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&this_fd, 1) && this_fd != -1) {
690690
if (PHP_SAFE_FD_ISSET(this_fd, fds)) {
691691
if (type == HASH_KEY_IS_LONG) {
692692
zend_hash_index_update(new_hash, num_ind, (void *)elem, sizeof(zval *), (void **)&dest_elem);

main/network.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,8 @@ PHPAPI int php_poll2(php_pollfd *ufds, unsigned int nfds, int timeout)
11331133
{
11341134
fd_set rset, wset, eset;
11351135
php_socket_t max_fd = SOCK_ERR;
1136-
unsigned int i, n;
1136+
unsigned int i;
1137+
int n;
11371138
struct timeval tv;
11381139

11391140
/* check the highest numbered descriptor */

main/streams/filter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ PHPAPI int php_stream_filter_append_ex(php_stream_filter_chain *chain, php_strea
360360
php_stream_bucket_append(brig_inp, bucket TSRMLS_CC);
361361
status = filter->fops->filter(stream, filter, brig_inp, brig_outp, &consumed, PSFS_FLAG_NORMAL TSRMLS_CC);
362362

363-
if (stream->readpos + consumed > (uint)stream->writepos || consumed < 0) {
363+
if (stream->readpos + consumed > (uint)stream->writepos) {
364364
/* No behaving filter should cause this. */
365365
status = PSFS_ERR_FATAL;
366366
}

main/streams/streams.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,7 @@ PHPAPI char *php_stream_get_record(php_stream *stream, size_t maxlen, size_t *re
940940

941941
len = stream->writepos - stream->readpos;
942942

943+
/* make sure the stream read buffer has maxlen bytes */
943944
while (len < maxlen) {
944945

945946
size_t just_read;
@@ -950,6 +951,8 @@ PHPAPI char *php_stream_get_record(php_stream *stream, size_t maxlen, size_t *re
950951
just_read = (stream->writepos - stream->readpos) - len;
951952
len += just_read;
952953

954+
/* read operation have less data than request; assume the stream is
955+
* temporarily or permanently out of data */
953956
if (just_read < toread) {
954957
break;
955958
}
@@ -960,6 +963,7 @@ PHPAPI char *php_stream_get_record(php_stream *stream, size_t maxlen, size_t *re
960963
} else {
961964
size_t seek_len;
962965

966+
/* set the maximum number of bytes we're allowed to read from buffer */
963967
seek_len = stream->writepos - stream->readpos;
964968
if (seek_len > maxlen) {
965969
seek_len = maxlen;
@@ -972,12 +976,17 @@ PHPAPI char *php_stream_get_record(php_stream *stream, size_t maxlen, size_t *re
972976
}
973977

974978
if (!e) {
979+
/* return with error if the delimiter string was not found, we
980+
* could not completely fill the read buffer with maxlen bytes
981+
* and we don't know we've reached end of file. Added with
982+
* non-blocking streams in mind, where this situation is frequent */
975983
if (seek_len < maxlen && !stream->eof) {
976984
return NULL;
977985
}
978986
toread = maxlen;
979987
} else {
980988
toread = e - (char *) stream->readbuf - stream->readpos;
989+
/* we found the delimiter, so advance the read pointer past it */
981990
skip = 1;
982991
}
983992
}
@@ -989,17 +998,12 @@ PHPAPI char *php_stream_get_record(php_stream *stream, size_t maxlen, size_t *re
989998
buf = emalloc(toread + 1);
990999
*returned_len = php_stream_read(stream, buf, toread);
9911000

992-
if (*returned_len >= 0) {
993-
if (skip) {
994-
stream->readpos += delim_len;
995-
stream->position += delim_len;
996-
}
997-
buf[*returned_len] = '\0';
998-
return buf;
999-
} else {
1000-
efree(buf);
1001-
return NULL;
1001+
if (skip) {
1002+
stream->readpos += delim_len;
1003+
stream->position += delim_len;
10021004
}
1005+
buf[*returned_len] = '\0';
1006+
return buf;
10031007
}
10041008

10051009
/* Writes a buffer directly to a stream, using multiple of the chunk size */

0 commit comments

Comments
 (0)