From: Greg Beaver Date: Wed, 29 Apr 2009 03:24:09 +0000 (+0000) Subject: MFPECL: fix PECL bug #16338, php_stream_copy_to_stream{,_ex}() X-Git-Tag: php-5.4.0alpha1~191^2~3812 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=31f3c2ad47b3723b410369912e620fcd293daf59;p=php MFPECL: fix PECL bug #16338, php_stream_copy_to_stream{,_ex}() --- diff --git a/ext/phar/phar.c b/ext/phar/phar.c index ad8ca95da5..b2f6a82ade 100644 --- a/ext/phar/phar.c +++ b/ext/phar/phar.c @@ -1635,7 +1635,7 @@ static int phar_open_from_fp(php_stream* fp, char *fname, int fname_len, char *a php_stream_filter_append(&temp->writefilters, filter); - if (0 == php_stream_copy_to_stream(fp, temp, PHP_STREAM_COPY_ALL)) { + if (SUCCESS != phar_stream_copy_to_stream(fp, temp, PHP_STREAM_COPY_ALL, NULL)) { if (err) { php_stream_close(temp); MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\", ext/zlib is buggy in PHP versions older than 5.2.6") @@ -1677,7 +1677,7 @@ static int phar_open_from_fp(php_stream* fp, char *fname, int fname_len, char *a php_stream_filter_append(&temp->writefilters, filter); - if (0 == php_stream_copy_to_stream(fp, temp, PHP_STREAM_COPY_ALL)) { + if (SUCCESS != phar_stream_copy_to_stream(fp, temp, PHP_STREAM_COPY_ALL, NULL)) { php_stream_close(temp); MAPPHAR_ALLOC_FAIL("unable to decompress bzipped phar archive \"%s\" to temporary file") } @@ -2661,7 +2661,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, long len, int convert, size_t written; if (!user_stub && phar->halt_offset && oldfile && !phar->is_brandnew) { - written = php_stream_copy_to_stream(oldfile, newfile, phar->halt_offset); + phar_stream_copy_to_stream(oldfile, newfile, phar->halt_offset, &written); newstub = NULL; } else { /* this is either a brand new phar or a default stub overwrite */ @@ -2849,7 +2849,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, long len, int convert, return EOF; } php_stream_filter_append((&entry->cfp->writefilters), filter); - if (entry->uncompressed_filesize != php_stream_copy_to_stream(file, entry->cfp, entry->uncompressed_filesize)) { + if (SUCCESS != phar_stream_copy_to_stream(file, entry->cfp, entry->uncompressed_filesize, NULL)) { if (closeoldfile) { php_stream_close(oldfile); } @@ -3059,7 +3059,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, long len, int convert, /* this will have changed for all files that have either changed compression or been modified */ entry->offset = entry->offset_abs = offset; offset += entry->compressed_filesize; - wrote = php_stream_copy_to_stream(file, newfile, entry->compressed_filesize); + phar_stream_copy_to_stream(file, newfile, entry->compressed_filesize, &wrote); if (entry->compressed_filesize != wrote) { if (closeoldfile) { @@ -3207,7 +3207,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, long len, int convert, } php_stream_filter_append(&phar->fp->writefilters, filter); - php_stream_copy_to_stream(newfile, phar->fp, PHP_STREAM_COPY_ALL); + phar_stream_copy_to_stream(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL); php_stream_filter_flush(filter, 1); php_stream_filter_remove(filter, 1 TSRMLS_CC); php_stream_close(phar->fp); @@ -3216,14 +3216,14 @@ int phar_flush(phar_archive_data *phar, char *user_stub, long len, int convert, } else if (phar->flags & PHAR_FILE_COMPRESSED_BZ2) { filter = php_stream_filter_create("bzip2.compress", NULL, php_stream_is_persistent(phar->fp) TSRMLS_CC); php_stream_filter_append(&phar->fp->writefilters, filter); - php_stream_copy_to_stream(newfile, phar->fp, PHP_STREAM_COPY_ALL); + phar_stream_copy_to_stream(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL); php_stream_filter_flush(filter, 1); php_stream_filter_remove(filter, 1 TSRMLS_CC); php_stream_close(phar->fp); /* use the temp stream as our base */ phar->fp = newfile; } else { - php_stream_copy_to_stream(newfile, phar->fp, PHP_STREAM_COPY_ALL); + phar_stream_copy_to_stream(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL); /* we could also reopen the file in "rb" mode but there is no need for that */ php_stream_close(newfile); } diff --git a/ext/phar/phar_internal.h b/ext/phar/phar_internal.h index 0e65a988b4..cbb6cf2db1 100755 --- a/ext/phar/phar_internal.h +++ b/ext/phar/phar_internal.h @@ -521,6 +521,23 @@ extern char *(*phar_save_resolve_path)(const char *filename, int filename_len TS # endif #endif +#if PHP_VERSION_ID < 50209 +static inline size_t phar_stream_copy_to_stream(php_stream *src, php_stream *dest, size_t maxlen, size_t *len STREAMS_DC TSRMLS_DC) +{ + size_t ret = php_stream_copy_to_stream(src, dest, maxlen); + if (len) { + *len = ret; + } + if (ret) { + return SUCCESS; + } + return FAILURE; +} +#else +# define phar_stream_copy_to_stream(src, dest, maxlen, len) _php_stream_copy_to_stream_ex((src), (dest), (maxlen), (len) STREAMS_CC TSRMLS_CC) + +#endif + #if PHP_VERSION_ID >= 60000 typedef zstr phar_zstr; #define PHAR_STR(a, b) \ diff --git a/ext/phar/phar_object.c b/ext/phar/phar_object.c index 1c4acf7349..3cc681752b 100755 --- a/ext/phar/phar_object.c +++ b/ext/phar/phar_object.c @@ -298,23 +298,31 @@ static int phar_file_action(phar_archive_data *phar, phar_entry_info *info, char zend_try { zend_execute(new_op_array TSRMLS_CC); - } zend_catch { - } zend_end_try(); - destroy_op_array(new_op_array TSRMLS_CC); - efree(new_op_array); + if (PHAR_G(cwd)) { + efree(PHAR_G(cwd)); + PHAR_G(cwd) = NULL; + PHAR_G(cwd_len) = 0; + } - if (PHAR_G(cwd)) { - efree(PHAR_G(cwd)); - PHAR_G(cwd) = NULL; - PHAR_G(cwd_len) = 0; - } + PHAR_G(cwd_init) = 0; + efree(name); + destroy_op_array(new_op_array TSRMLS_CC); + efree(new_op_array); - PHAR_G(cwd_init) = 0; - efree(name); - if (EG(return_value_ptr_ptr) && *EG(return_value_ptr_ptr)) { - zval_ptr_dtor(EG(return_value_ptr_ptr)); - } + if (EG(return_value_ptr_ptr) && *EG(return_value_ptr_ptr)) { + zval_ptr_dtor(EG(return_value_ptr_ptr)); + } + } zend_catch { + if (PHAR_G(cwd)) { + efree(PHAR_G(cwd)); + PHAR_G(cwd) = NULL; + PHAR_G(cwd_len) = 0; + } + + PHAR_G(cwd_init) = 0; + efree(name); + } zend_end_try(); zend_bailout(); } @@ -1414,7 +1422,7 @@ static int phar_build(zend_object_iterator *iter, void *puser TSRMLS_DC) /* {{{ uint str_key_len, base_len = p_obj->l, fname_len; phar_entry_data *data; php_stream *fp; - long contents_len; + size_t contents_len; char *fname, *error = NULL, *base = p_obj->b, *opened, *save = NULL, *temp = NULL; phar_zstr key; char *str_key; @@ -1716,7 +1724,7 @@ after_open_fp: data->internal_file->fp_type = PHAR_UFP; data->internal_file->offset_abs = data->internal_file->offset = php_stream_tell(p_obj->fp); data->fp = NULL; - contents_len = php_stream_copy_to_stream(fp, p_obj->fp, PHP_STREAM_COPY_ALL); + phar_stream_copy_to_stream(fp, p_obj->fp, PHP_STREAM_COPY_ALL, &contents_len); data->internal_file->uncompressed_filesize = data->internal_file->compressed_filesize = php_stream_tell(p_obj->fp) - data->internal_file->offset; } @@ -1998,7 +2006,7 @@ static int phar_copy_file_contents(phar_entry_info *entry, php_stream *fp TSRMLS link = entry; } - if (link->uncompressed_filesize != php_stream_copy_to_stream(phar_get_efp(link, 0 TSRMLS_CC), fp, link->uncompressed_filesize)) { + if (SUCCESS != phar_stream_copy_to_stream(phar_get_efp(link, 0 TSRMLS_CC), fp, link->uncompressed_filesize, NULL)) { zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Cannot convert phar archive \"%s\", unable to copy entry \"%s\" contents", entry->phar->fname, entry->filename); return FAILURE; @@ -3560,7 +3568,7 @@ PHP_METHOD(Phar, offsetGet) static void phar_add_file(phar_archive_data **pphar, char *filename, int filename_len, char *cont_str, int cont_len, zval *zresource TSRMLS_DC) { char *error; - long contents_len; + size_t contents_len; phar_entry_data *data; php_stream *contents_file; @@ -3594,7 +3602,7 @@ static void phar_add_file(phar_archive_data **pphar, char *filename, int filenam zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Entry %s could not be written to", filename); return; } - contents_len = php_stream_copy_to_stream(contents_file, data->fp, PHP_STREAM_COPY_ALL); + phar_stream_copy_to_stream(contents_file, data->fp, PHP_STREAM_COPY_ALL, &contents_len); } data->internal_file->compressed_filesize = data->internal_file->uncompressed_filesize = contents_len; @@ -4151,7 +4159,7 @@ static int phar_extract_file(zend_bool overwrite, phar_entry_info *entry, char * return FAILURE; } - if (entry->uncompressed_filesize != php_stream_copy_to_stream(phar_get_efp(entry, 0 TSRMLS_CC), fp, entry->uncompressed_filesize)) { + if (SUCCESS != phar_stream_copy_to_stream(phar_get_efp(entry, 0 TSRMLS_CC), fp, entry->uncompressed_filesize, NULL)) { spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", copying contents failed", entry->filename, fullpath); efree(fullpath); php_stream_close(fp); diff --git a/ext/phar/tar.c b/ext/phar/tar.c index 84789b1411..8101cdf21f 100644 --- a/ext/phar/tar.c +++ b/ext/phar/tar.c @@ -696,7 +696,7 @@ static int phar_tar_writeheaders(void *pDest, void *argument TSRMLS_DC) /* {{{ * return ZEND_HASH_APPLY_STOP; } - if (entry->uncompressed_filesize != php_stream_copy_to_stream(phar_get_efp(entry, 0 TSRMLS_CC), fp->new, entry->uncompressed_filesize)) { + if (SUCCESS != phar_stream_copy_to_stream(phar_get_efp(entry, 0 TSRMLS_CC), fp->new, entry->uncompressed_filesize, NULL)) { if (fp->error) { spprintf(fp->error, 4096, "tar-based phar \"%s\" cannot be created, contents of file \"%s\" could not be written", entry->phar->fname, entry->filename); } @@ -1180,7 +1180,7 @@ nostub: if (!filter) { /* copy contents uncompressed rather than lose them */ - php_stream_copy_to_stream(newfile, phar->fp, PHP_STREAM_COPY_ALL); + phar_stream_copy_to_stream(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL); php_stream_close(newfile); if (error) { spprintf(error, 4096, "unable to compress all contents of phar \"%s\" using zlib, PHP versions older than 5.2.6 have a buggy zlib", phar->fname); @@ -1189,7 +1189,7 @@ nostub: } php_stream_filter_append(&phar->fp->writefilters, filter); - php_stream_copy_to_stream(newfile, phar->fp, PHP_STREAM_COPY_ALL); + phar_stream_copy_to_stream(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL); php_stream_filter_flush(filter, 1); php_stream_filter_remove(filter, 1 TSRMLS_CC); php_stream_close(phar->fp); @@ -1200,14 +1200,14 @@ nostub: filter = php_stream_filter_create("bzip2.compress", NULL, php_stream_is_persistent(phar->fp) TSRMLS_CC); php_stream_filter_append(&phar->fp->writefilters, filter); - php_stream_copy_to_stream(newfile, phar->fp, PHP_STREAM_COPY_ALL); + phar_stream_copy_to_stream(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL); php_stream_filter_flush(filter, 1); php_stream_filter_remove(filter, 1 TSRMLS_CC); php_stream_close(phar->fp); /* use the temp stream as our base */ phar->fp = newfile; } else { - php_stream_copy_to_stream(newfile, phar->fp, PHP_STREAM_COPY_ALL); + phar_stream_copy_to_stream(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL); /* we could also reopen the file in "rb" mode but there is no need for that */ php_stream_close(newfile); } diff --git a/ext/phar/util.c b/ext/phar/util.c index d2c9d17d2d..121cb0a64c 100644 --- a/ext/phar/util.c +++ b/ext/phar/util.c @@ -893,7 +893,7 @@ int phar_copy_entry_fp(phar_entry_info *source, phar_entry_info *dest, char **er link = source; } - if (link->uncompressed_filesize != php_stream_copy_to_stream(phar_get_efp(link, 0 TSRMLS_CC), dest->fp, link->uncompressed_filesize)) { + if (SUCCESS != phar_stream_copy_to_stream(phar_get_efp(link, 0 TSRMLS_CC), dest->fp, link->uncompressed_filesize, NULL)) { php_stream_close(dest->fp); dest->fp_type = PHAR_FP; if (error) { @@ -994,7 +994,7 @@ int phar_open_entry_fp(phar_entry_info *entry, char **error, int follow_links TS php_stream_filter_append(&ufp->writefilters, filter); php_stream_seek(phar_get_entrypfp(entry TSRMLS_CC), phar_get_fp_offset(entry TSRMLS_CC), SEEK_SET); - if (php_stream_copy_to_stream(phar_get_entrypfp(entry TSRMLS_CC), ufp, entry->compressed_filesize) != entry->compressed_filesize) { + if (SUCCESS != phar_stream_copy_to_stream(phar_get_entrypfp(entry TSRMLS_CC), ufp, entry->compressed_filesize, NULL)) { spprintf(error, 4096, "phar error: internal corruption of phar \"%s\" (actual filesize mismatch on file \"%s\")", phar->fname, entry->filename); php_stream_filter_remove(filter, 1 TSRMLS_CC); return FAILURE; @@ -1131,7 +1131,7 @@ int phar_separate_entry_fp(phar_entry_info *entry, char **error TSRMLS_DC) /* {{ link = entry; } - if (link->uncompressed_filesize != php_stream_copy_to_stream(phar_get_efp(link, 0 TSRMLS_CC), fp, link->uncompressed_filesize)) { + if (SUCCESS != phar_stream_copy_to_stream(phar_get_efp(link, 0 TSRMLS_CC), fp, link->uncompressed_filesize, NULL)) { if (error) { spprintf(error, 4096, "phar error: cannot separate entry file \"%s\" contents in phar archive \"%s\" for write access", entry->filename, entry->phar->fname); } diff --git a/ext/phar/zip.c b/ext/phar/zip.c index 1409b65248..0396d69a28 100644 --- a/ext/phar/zip.c +++ b/ext/phar/zip.c @@ -833,7 +833,7 @@ static int phar_zip_changed_apply(void *data, void *arg TSRMLS_DC) /* {{{ */ php_stream_filter_append((&entry->cfp->writefilters), filter); - if (entry->uncompressed_filesize != php_stream_copy_to_stream(efp, entry->cfp, entry->uncompressed_filesize)) { + if (SUCCESS != phar_stream_copy_to_stream(efp, entry->cfp, entry->uncompressed_filesize, NULL)) { spprintf(p->error, 0, "unable to copy compressed file contents of file \"%s\" while creating new phar \"%s\"", entry->filename, entry->phar->fname); return ZEND_HASH_APPLY_STOP; } @@ -937,7 +937,7 @@ continue_dir: if (!not_really_modified && entry->is_modified) { if (entry->cfp) { - if (entry->compressed_filesize != php_stream_copy_to_stream(entry->cfp, p->filefp, entry->compressed_filesize)) { + if (SUCCESS != phar_stream_copy_to_stream(entry->cfp, p->filefp, entry->compressed_filesize, NULL)) { spprintf(p->error, 0, "unable to write compressed contents of file \"%s\" in zip-based phar \"%s\"", entry->filename, entry->phar->fname); return ZEND_HASH_APPLY_STOP; } @@ -951,7 +951,7 @@ continue_dir: phar_seek_efp(entry, 0, SEEK_SET, 0, 0 TSRMLS_CC); - if (entry->uncompressed_filesize != php_stream_copy_to_stream(phar_get_efp(entry, 0 TSRMLS_CC), p->filefp, entry->uncompressed_filesize)) { + if (SUCCESS != phar_stream_copy_to_stream(phar_get_efp(entry, 0 TSRMLS_CC), p->filefp, entry->uncompressed_filesize, NULL)) { spprintf(p->error, 0, "unable to write contents of file \"%s\" in zip-based phar \"%s\"", entry->filename, entry->phar->fname); return ZEND_HASH_APPLY_STOP; } @@ -977,7 +977,7 @@ continue_dir: } } - if (!entry->is_dir && entry->compressed_filesize && entry->compressed_filesize != php_stream_copy_to_stream(p->old, p->filefp, entry->compressed_filesize)) { + if (!entry->is_dir && entry->compressed_filesize && SUCCESS != phar_stream_copy_to_stream(p->old, p->filefp, entry->compressed_filesize, NULL)) { spprintf(p->error, 0, "unable to copy contents of file \"%s\" while creating zip-based phar \"%s\"", entry->filename, entry->phar->fname); return ZEND_HASH_APPLY_STOP; } @@ -1241,11 +1241,15 @@ nocentralerror: PHAR_SET_32(eocd.cdir_offset, cdir_offset); php_stream_seek(pass.centralfp, 0, SEEK_SET); - if (cdir_size != php_stream_copy_to_stream(pass.centralfp, pass.filefp, PHP_STREAM_COPY_ALL)) { - if (error) { - spprintf(error, 4096, "phar zip flush of \"%s\" failed: unable to write central-directory", phar->fname); + { + size_t len; + int ret = phar_stream_copy_to_stream(pass.centralfp, pass.filefp, PHP_STREAM_COPY_ALL, &len); + if (SUCCESS != ret || len != cdir_size) { + if (error) { + spprintf(error, 4096, "phar zip flush of \"%s\" failed: unable to write central-directory", phar->fname); + } + goto temperror; } - goto temperror; } php_stream_close(pass.centralfp); @@ -1312,7 +1316,7 @@ nocentralerror: return EOF; } php_stream_rewind(pass.filefp); - php_stream_copy_to_stream(pass.filefp, phar->fp, PHP_STREAM_COPY_ALL); + phar_stream_copy_to_stream(pass.filefp, phar->fp, PHP_STREAM_COPY_ALL, NULL); /* we could also reopen the file in "rb" mode but there is no need for that */ php_stream_close(pass.filefp); }