]> granicus.if.org Git - php/commitdiff
- Verify stub
authorMarcus Boerger <helly@php.net>
Sun, 27 May 2007 16:54:37 +0000 (16:54 +0000)
committerMarcus Boerger <helly@php.net>
Sun, 27 May 2007 16:54:37 +0000 (16:54 +0000)
- 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

ext/phar/phar.c
ext/phar/tests/phar_begin_setstub_commit.phpt
ext/phar/tests/phar_commitwrite.phpt
ext/phar/tests/phar_stub_write.phpt
ext/phar/tests/phar_stub_write_file.phpt [new file with mode: 0755]

index 83e98296ae8d05e735b0cac150293345a4de1e44..461b4b98774d74e1b3171737302a01a3663218b5 100644 (file)
@@ -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) {
index 2d26fa440f20b71832bfcc512c687b8d1e919293..ece362f35066ccea4d53a29e5ef2bc8340c07902 100755 (executable)
@@ -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===
index 25d8c929667ec135dbc7615190e34ea7181014c2..16f35701bd2666747394d9a285473c281de3f697 100644 (file)
@@ -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===
\ No newline at end of file
index e151b849a2e035ec88ff5bbfba38baba4dc6b8af..2ea475bcf4217584ef723391aecf576dda487d62 100755 (executable)
@@ -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===
diff --git a/ext/phar/tests/phar_stub_write_file.phpt b/ext/phar/tests/phar_stub_write_file.phpt
new file mode 100755 (executable)
index 0000000..dbc4b8b
--- /dev/null
@@ -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===