Skip to content

Lexer raises a PHP 8.6 notice on hex literals larger than PHP_INT_MAX #694

Backlog

Description

@Daeroni

Bug

Token::extract() passes every 0x... number to hexdec(). Since PHP 8.6, hexdec() raises an E_NOTICE when the result does not fit in an integer:

Input number is larger than PHP_INT_MAX, precision has been lost in conversion

Any query with a hex literal longer than 16 digits triggers it while lexing. Such literals are common in dumps and generated SQL, for example CONVERT(0x... USING utf8mb4) for text or binary column data. Applications that turn notices into exceptions fail on these queries before parsing finishes.

PHP 8.5 returns the same float without a notice, so the value was already lossy there (5.735816763073855E+30 below). MySQL and MariaDB treat such a literal as a binary string in string context, not as a number.

To reproduce

PHP 8.6.0, sql-parser 6.0.2:

vendor/bin/sql-parser --tokenize --query "SELECT 0x48656C6C6F2C20776F726C6421"
PHP Notice:  Input number is larger than PHP_INT_MAX, precision has been lost in conversion in vendor/phpmyadmin/sql-parser/src/Token.php on line 144
...
[TOKEN 2]
Type = 6
Flags = 1
Value = 5.735816763073855E+30
Token = '0x48656C6C6F2C20776F726C6421'

--lint triggers the same notice.

Expected behaviour

Lexing succeeds without a notice, and the token value does not pretend to be an exact number.

Affected versions

6.0.2 and the current 6.0.x and master branches, which have the same code in Token::extract().

Possible fix

Keep the token text as value when the literal is out of integer range, as is already done for FLAG_NUMBER_BINARY and FLAG_NUMBER_HEX_STRING:

if ($this->flags & self::FLAG_NUMBER_HEX) {
    $ret = str_replace(['-', '+'], '', $this->token);
    $digits = ltrim(substr($ret, 2), '0');
    if (strlen($digits) > 16 || (strlen($digits) === 16 && $digits[0] > '7')) {
        return ($this->flags & self::FLAG_NUMBER_NEGATIVE ? '-' : '') . $ret;
    }

    // existing hexdec() code
}

The sign needs the explicit prefix: $ret has the signs stripped, so an early return $ret would turn -0x8000000000000000 into the unsigned text 0x8000000000000000.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions