Skip to content

Phar does not respect case-insensitiveness of __halt_compiler() when reading stub #20442

Closed

Description

@shyim

Description

The following code:

stub.php

<?php

echo "Hello World!";

__halt_compiler();

build.php

<?php

$phar = new Phar('output.phar');
$phar->startBuffering();

$phar->setStub(file_get_contents('stub.php'));
$phar->addEmptyDir('src');

$phar->stopBuffering();

Resulted in this output:

~/Downloads/foo via 🐘 v8.4.14
❯ php -dphar.readonly=0 build.php

~/Downloads/foo via 🐘 v8.4.14
❯ php -dphar.readonly=0 build.php
PHP Fatal error:  Uncaught UnexpectedValueException: internal corruption of phar "/Users/shyim/Downloads/foo/output.phar" (__HALT_COMPILER(); not found) in /Users/shyim/Downloads/foo/build.php:3
Stack trace:
#0 /Users/shyim/Downloads/foo/build.php(3): Phar->__construct('output.phar')
#1 {main}
  thrown in /Users/shyim/Downloads/foo/build.php on line 3

Fatal error: Uncaught UnexpectedValueException: internal corruption of phar "/Users/shyim/Downloads/foo/output.phar" (__HALT_COMPILER(); not found) in /Users/shyim/Downloads/foo/build.php:3
Stack trace:
#0 /Users/shyim/Downloads/foo/build.php(3): Phar->__construct('output.phar')
#1 {main}
  thrown in /Users/shyim/Downloads/foo/build.php on line 3

But when I write __HALT_COMPILER in UPPER case it works always. I normally write it always upper-case but saw in docs other mentioned, just copy pasted. https://www.php.net/manual/en/function.halt-compiler.php

it looks like it's hard-code to upper-case:

static const char halt_stub[] = "__HALT_COMPILER();";

Are the docs just wrong, should this be changed in php idk 😂 . The execution of the phar works with regardless of caseing

PHP Version

PHP 8.4.14 (cli) (built: Oct 21 2025 19:23:55) (NTS)
Copyright (c) The PHP Group
Built by Shivam Mathur
Zend Engine v4.4.14, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.14, Copyright (c), by Zend Technologies

Operating System

No response

Activity

  1. iluuu1994 commented on Nov 10, 2025

    @iluuu1994
    Member

    __halt_compiler(); is not case-sensitive. You're showing two executions in "Resulted in this output", which is the one you're getting?

  2. TimWolla commented on Nov 10, 2025

    @TimWolla
    Member

    You're showing two executions in "Resulted in this output", which is the one you're getting?

    Both are relevant. My understanding is that the second is trying to edit the existing phar created by the first which fails. I can reproduce with the given steps.

    The error message is emitted here:

    MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (__HALT_COMPILER(); not found)")

    and

    if (got > 0 && (pos = phar_strnstr(buffer, got + sizeof(token), token, sizeof(token)-1)) != NULL) {

    should probably be a CI comparison.

  3. iluuu1994 commented on Nov 10, 2025

    @iluuu1994
    Member

    Okay. I've never looked into phar, so I'll leave this up to somebody else if it's a phar-only issue.

  4. self-assigned this
    on Nov 10, 2025
  5. ndossche commented on Nov 10, 2025

    @ndossche
    Member

    Yeah the flush code does this right, the reading code doesn't.
    It should likely use php_memnistr or something alike.

  6. changed the title [-]__halt_compiler case-sensitiv?[/-] [+]Phar does not respect case-insensitiveness of __halt_compiler() when reading stub[/+] on Nov 10, 2025
  7. added a commit that references this issue on Nov 10, 2025
    1e34bab
  8. added 4 commits that reference this issue on Nov 10, 2025
    3e715d3
    5087cf3
    9d9f979
    4ee2539
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions