From: Marcus Boerger Date: Sun, 27 May 2007 16:54:37 +0000 (+0000) Subject: - Verify stub X-Git-Tag: RELEASE_1_4~64 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=096e123f2d373e6832041ea5c0de47185accf8ab;p=php - Verify stub - Automatically cut off stub after __HALT_COMPILER(); - Always write longest stub ending, so there is no issue with length field - Add test for setStub from file - Fix tests --- diff --git a/ext/phar/phar.c b/ext/phar/phar.c index 83e98296ae..461b4b9877 100644 --- a/ext/phar/phar.c +++ b/ext/phar/phar.c @@ -763,7 +763,8 @@ int phar_open_file(php_stream *fp, char *fname, int fname_len, char *alias, int MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated manifest at script end)") } if ((char) nextchar == '\r') { - if (EOF == (nextchar = php_stream_getc(fp))) { + /* if we have an \r we require an \n as well */ + if (EOF == (nextchar = php_stream_getc(fp)) || (char)nextchar != '\n') { MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated manifest at script end)") } halt_offset++; @@ -2097,7 +2098,7 @@ static int phar_flush_clean_deleted_apply(void *data TSRMLS_DC) /* {{{ */ */ int phar_flush(phar_archive_data *archive, char *user_stub, long len, char **error TSRMLS_DC) /* {{{ */ { - static const char newstub[] = "\r\n"; phar_entry_info *entry; int halt_offset, restore_alias_len, global_flags = 0, closeoldfile; char *buf, *pos; @@ -2111,6 +2112,7 @@ int phar_flush(phar_archive_data *archive, char *user_stub, long len, char **err php_stream_filter *filter; php_serialize_data_t metadata_hash; smart_str main_metadata_str = {0}; + int free_user_stub; if (error) { *error = NULL; @@ -2148,7 +2150,7 @@ int phar_flush(phar_archive_data *archive, char *user_stub, long len, char **err } php_stream_close(newfile); if (error) { - spprintf(error, 0, "unable to read resource to copy stub to new phar \"%s\"", archive->fname); + spprintf(error, 0, "unable to access resource to copy stub to new phar \"%s\"", archive->fname); } return EOF; } @@ -2157,41 +2159,53 @@ int phar_flush(phar_archive_data *archive, char *user_stub, long len, char **err } else { len = -len; } - offset = php_stream_copy_to_stream(stubfile, newfile, len); - if (len != offset && len != PHP_STREAM_COPY_ALL) { + user_stub = 0; + if (!(len = php_stream_copy_to_mem(stubfile, &user_stub, len, 0)) || !user_stub) { if (closeoldfile) { php_stream_close(oldfile); } php_stream_close(newfile); if (error) { - spprintf(error, 0, "unable to copy stub from resource to new phar \"%s\"", archive->fname); + spprintf(error, 0, "unable to read resource to copy stub to new phar \"%s\"", archive->fname); } return EOF; } - archive->halt_offset = offset; + free_user_stub = 1; } else { - if ((pos = strstr(user_stub, "__HALT_COMPILER();")) == NULL) - { - if (closeoldfile) { - php_stream_close(oldfile); - } - php_stream_close(newfile); - if (error) { - spprintf(error, 0, "illegal stub for phar \"%s\"", archive->fname); - } - return EOF; + free_user_stub = 0; + } + if ((pos = strstr(user_stub, "__HALT_COMPILER();")) == NULL) + { + if (closeoldfile) { + php_stream_close(oldfile); } - if ((size_t)len != php_stream_write(newfile, user_stub, len)) { - if (closeoldfile) { - php_stream_close(oldfile); - } - php_stream_close(newfile); - if (error) { - spprintf(error, 0, "unable to create stub from string in new phar \"%s\"", archive->fname); - } - return EOF; + php_stream_close(newfile); + if (error) { + spprintf(error, 0, "illegal stub for phar \"%s\"", archive->fname); } - archive->halt_offset = len; + if (free_user_stub) { + efree(user_stub); + } + return EOF; + } + len = pos - user_stub + 18; + if ((size_t)len != php_stream_write(newfile, user_stub, len) + || 5 != php_stream_write(newfile, " ?>\r\n", 5)) { + if (closeoldfile) { + php_stream_close(oldfile); + } + php_stream_close(newfile); + if (error) { + spprintf(error, 0, "unable to create stub from string in new phar \"%s\"", archive->fname); + } + if (free_user_stub) { + efree(user_stub); + } + return EOF; + } + archive->halt_offset = len + 5; + if (free_user_stub) { + efree(user_stub); } } else { if (archive->halt_offset && oldfile && !archive->is_brandnew) { diff --git a/ext/phar/tests/phar_begin_setstub_commit.phpt b/ext/phar/tests/phar_begin_setstub_commit.phpt index 2d26fa440f..ece362f350 100755 --- a/ext/phar/tests/phar_begin_setstub_commit.phpt +++ b/ext/phar/tests/phar_begin_setstub_commit.phpt @@ -17,7 +17,7 @@ $p->setStub('getStub()); $p['b.php'] = 'setStub(''); +$p->setStub('getStub()); $p->stopBuffering(); @@ -36,12 +36,15 @@ unlink(dirname(__FILE__) . '/brandnewphar.phar'); bool(true) bool(false) string(5) "Hello" -string(82) "" +string(84) " +" string(5) "World" -string(83) "" +string(85) " +" ===COMMIT=== bool(true) string(5) "Hello" string(5) "World" -string(83) "" +string(85) " +" ===DONE=== diff --git a/ext/phar/tests/phar_commitwrite.phpt b/ext/phar/tests/phar_commitwrite.phpt index 25d8c92966..16f35701bd 100644 --- a/ext/phar/tests/phar_commitwrite.phpt +++ b/ext/phar/tests/phar_commitwrite.phpt @@ -28,14 +28,15 @@ var_dump($p->getStub()); unlink(dirname(__FILE__) . '/brandnewphar.phar'); ?> --EXPECT-- -string(24) " +" +string(200) "" +__HALT_COMPILER(); ?> +" ===DONE=== \ No newline at end of file diff --git a/ext/phar/tests/phar_stub_write.phpt b/ext/phar/tests/phar_stub_write.phpt index e151b849a2..2ea475bcf4 100755 --- a/ext/phar/tests/phar_stub_write.phpt +++ b/ext/phar/tests/phar_stub_write.phpt @@ -25,15 +25,20 @@ var_dump($phar->getStub()); var_dump($phar->getStub() == $stub); $stub = ''; +$sexp = $stub . "\r\n"; + $phar->setStub($stub); var_dump($phar->getStub()); var_dump($phar->getStub() == $stub); +var_dump($phar->getStub() == $sexp); $phar->stopBuffering(); var_dump($phar->getStub()); var_dump($phar->getStub() == $stub); +var_dump($phar->getStub() == $sexp); $phar = new Phar($fname); var_dump($phar->getStub() == $stub); +var_dump($phar->getStub() == $sexp); ?> ===DONE=== @@ -46,9 +51,14 @@ __HALT_COMPILER(); string(48) "" string(48) "" bool(true) -string(49) "" +string(51) " +" +bool(false) bool(true) -string(49) "" +string(51) " +" +bool(false) bool(true) +bool(false) bool(true) ===DONE=== diff --git a/ext/phar/tests/phar_stub_write_file.phpt b/ext/phar/tests/phar_stub_write_file.phpt new file mode 100755 index 0000000000..dbc4b8b8b5 --- /dev/null +++ b/ext/phar/tests/phar_stub_write_file.phpt @@ -0,0 +1,64 @@ +--TEST-- +Phar::setStub()/getStub() from file +--SKIPIF-- + +--INI-- +phar.require_hash=0 +phar.readonly=0 +--FILE-- +'; +$file = $stub; + +$files = array(); +$files['a'] = 'a'; +$files['b'] = 'b'; +$files['c'] = 'c'; + +include 'phar_test.inc'; + +$phar = new Phar($fname); +var_dump($stub); +var_dump($phar->getStub()); +var_dump($phar->getStub() == $stub); + +$stub = ''; +$sexp = $stub . "\r\n"; +$stub = fopen('data://,'.$stub, 'r'); +$phar->setStub($stub); +var_dump($phar->getStub()); +var_dump($phar->getStub() == $stub); +var_dump($phar->getStub() == $sexp); +$phar->stopBuffering(); +var_dump($phar->getStub()); +var_dump($phar->getStub() == $stub); +var_dump($phar->getStub() == $sexp); + +$phar = new Phar($fname); +var_dump($phar->getStub() == $stub); +var_dump($phar->getStub() == $sexp); + +?> +===DONE=== +--CLEAN-- + +--EXPECT-- +string(48) "" +string(48) "" +bool(true) +string(51) " +" +bool(false) +bool(true) +string(51) " +" +bool(false) +bool(true) +bool(false) +bool(true) +===DONE===