Skip to content

pdo_pgsql: prepare() hangs on a query containing a NUL byte #24490

Closed

Description

@dev-null-undefined

Description

Since 5ccc56b (the pdo_pgsql SQL scanner speed-up), a NUL byte in the query makes PDO::prepare() loop forever. If the NUL is the first byte, it segfaults instead.

The following code:

<?php
$db = new PDO('pgsql:host=127.0.0.1;dbname=postgres', 'postgres', '');
$db->prepare("SELECT 'abc\0def'");
echo "done\n";

Resulted in this output:

(It never returns. With "\0SELECT 1" it segfaults.)

But I expected this output instead:

done

No rule in pgsql_sql_parser.re matches \0, and re2c already warns about that. Before the change, a NUL at the start of a token ended up in the SKIP_ONE action. Now it ends up in RET(PDO_PARSER_TEXT), with YYMARKER (s->ptr) still pointing at the previous token. So the scanner keeps returning the same zero-length token. When the NUL is the first byte, s->ptr was never set at all, hence the crash.

Adding a default rule fixes it for me. ext/pdo_pgsql/tests still pass:

 		([eE]* TEXTCHR)+						{ RET(PDO_PARSER_TEXT); }
+		*										{ SKIP_ONE(PDO_PARSER_TEXT); }

PHP Version

PHP 8.7.0-dev (cli) (NTS DEBUG), master at 385f368057

Operating System

Linux x86_64

Activity

  1. devnexen commented on Oct 21, 2026

    @devnexen
    Member

    Hi @dev-null-undefined I think your fix is correct would you mind creating a PR ?

  2. added a commit that references this issue on Oct 21, 2026
    2c4971c
  3. added a commit that references this issue on Oct 22, 2026
    b91ea88
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