Browse Source

- 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
PHAR_1_2
Marcus Boerger 19 years ago
parent
commit
096e123f2d
  1. 68
      ext/phar/phar.c
  2. 11
      ext/phar/tests/phar_begin_setstub_commit.phpt
  3. 9
      ext/phar/tests/phar_commitwrite.phpt
  4. 14
      ext/phar/tests/phar_stub_write.phpt
  5. 64
      ext/phar/tests/phar_stub_write_file.phpt

68
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[] = "<?php __HALT_COMPILER();";
static const char newstub[] = "<?php __HALT_COMPILER(); ?>\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) {

11
ext/phar/tests/phar_begin_setstub_commit.phpt

@ -17,7 +17,7 @@ $p->setStub('<?php var_dump("First"); Phar::mapPhar("brandnewphar.phar"); __HALT
include 'phar://brandnewphar.phar/a.php';
var_dump($p->getStub());
$p['b.php'] = '<?php var_dump("World");';
$p->setStub('<?php var_dump("Second"); Phar::mapPhar("brandnewphar.phar"); __HALT_COMPILER(); ?>');
$p->setStub('<?php var_dump("Second"); Phar::mapPhar("brandnewphar.phar"); __HALT_COMPILER();');
include 'phar://brandnewphar.phar/b.php';
var_dump($p->getStub());
$p->stopBuffering();
@ -36,12 +36,15 @@ unlink(dirname(__FILE__) . '/brandnewphar.phar');
bool(true)
bool(false)
string(5) "Hello"
string(82) "<?php var_dump("First"); Phar::mapPhar("brandnewphar.phar"); __HALT_COMPILER(); ?>"
string(84) "<?php var_dump("First"); Phar::mapPhar("brandnewphar.phar"); __HALT_COMPILER(); ?>
"
string(5) "World"
string(83) "<?php var_dump("Second"); Phar::mapPhar("brandnewphar.phar"); __HALT_COMPILER(); ?>"
string(85) "<?php var_dump("Second"); Phar::mapPhar("brandnewphar.phar"); __HALT_COMPILER(); ?>
"
===COMMIT===
bool(true)
string(5) "Hello"
string(5) "World"
string(83) "<?php var_dump("Second"); Phar::mapPhar("brandnewphar.phar"); __HALT_COMPILER(); ?>"
string(85) "<?php var_dump("Second"); Phar::mapPhar("brandnewphar.phar"); __HALT_COMPILER(); ?>
"
===DONE===

9
ext/phar/tests/phar_commitwrite.phpt

@ -28,14 +28,15 @@ var_dump($p->getStub());
unlink(dirname(__FILE__) . '/brandnewphar.phar');
?>
--EXPECT--
string(24) "<?php __HALT_COMPILER();"
string(198) "<?php
string(29) "<?php __HALT_COMPILER(); ?>
"
string(200) "<?php
function __autoload($class)
{
include 'phar://' . str_replace('_', '/', $class);
}
Phar::mapPhar('brandnewphar.phar');
include 'phar://brandnewphar.phar/startup.php';
__HALT_COMPILER();
?>"
__HALT_COMPILER(); ?>
"
===DONE===

14
ext/phar/tests/phar_stub_write.phpt

@ -25,15 +25,20 @@ var_dump($phar->getStub());
var_dump($phar->getStub() == $stub);
$stub = '<?php echo "second stub\n"; __HALT_COMPILER(); ?>';
$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) "<?php echo "first stub\n"; __HALT_COMPILER(); ?>"
string(48) "<?php echo "first stub\n"; __HALT_COMPILER(); ?>"
bool(true)
string(49) "<?php echo "second stub\n"; __HALT_COMPILER(); ?>"
string(51) "<?php echo "second stub\n"; __HALT_COMPILER(); ?>
"
bool(false)
bool(true)
string(49) "<?php echo "second stub\n"; __HALT_COMPILER(); ?>"
string(51) "<?php echo "second stub\n"; __HALT_COMPILER(); ?>
"
bool(false)
bool(true)
bool(false)
bool(true)
===DONE===

64
ext/phar/tests/phar_stub_write_file.phpt

@ -0,0 +1,64 @@
--TEST--
Phar::setStub()/getStub() from file
--SKIPIF--
<?php if (!extension_loaded("phar")) print "skip"; ?>
--INI--
phar.require_hash=0
phar.readonly=0
--FILE--
<?php
$fname = dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.phar.php';
$pname = 'phar://' . $fname;
$stub = '<?php echo "first stub\n"; __HALT_COMPILER(); ?>';
$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 = '<?php echo "second stub\n"; __HALT_COMPILER(); ?>';
$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--
<?php
unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar.php');
__HALT_COMPILER();
?>
--EXPECT--
string(48) "<?php echo "first stub\n"; __HALT_COMPILER(); ?>"
string(48) "<?php echo "first stub\n"; __HALT_COMPILER(); ?>"
bool(true)
string(51) "<?php echo "second stub\n"; __HALT_COMPILER(); ?>
"
bool(false)
bool(true)
string(51) "<?php echo "second stub\n"; __HALT_COMPILER(); ?>
"
bool(false)
bool(true)
bool(false)
bool(true)
===DONE===
Loading…
Cancel
Save