Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Function JIT: wrong result (null) from a string-scanning loop with strcspn() after a comment-start byte

未关闭
#24,578 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
54/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
php
领域
performance

调研方向

Start by reducing the provided reproducer to isolate the strcspn() scanning loop under opcache.jit=function, then compare its result with tracing and disabled JIT modes. The issue names no PHP source files or tests, so first locate the function JIT implementation and its tests for string scanning. Done means both inputs return their full expected strings in function JIT mode, with the other modes still behaving correctly.

由索引模型根据 Issue 内容生成。

描述

Bug Category: JIT Extension: opcache Status: Verified
Description

With opcache.jit=function, the static method Repro::interpolate() below returns null for two inputs that it handles correctly with opcache.jit=tracing and opcache.jit=disable. The two inputs contain a byte that may start a comment (- or /) but no placeholder, so the scanner should copy the whole string.

The following code:

<?php
declare(strict_types=1);

final class Repro {
    public static function skipComment(string $sql, int $position): int {
        $byte = $sql[$position]; $next = $sql[$position + 1] ?? '';
        if ($byte === '/' && $next === '*') { $end = strpos($sql, '*/', $position + 2); return $end === false ? strlen($sql) : $end + 2; }
        $dashes = $byte === '-' && $next === '-' && strspn($sql[$position + 2] ?? "\n", " \t\r\n") === 1;
        if ($byte !== '#' && !$dashes) return $position; $end = strpos($sql, "\n", $position);
        return $end === false ? strlen($sql) : $end + 1; }
    public static function skipQuoted(string $sql, int $position): int {
        $quote = $sql[$position]; $length = strlen($sql); $stops = $quote === '`' ? '`' : $quote . '\\'; $position++;
        while ($position < $length) { $position += strcspn($sql, $stops, $position); if ($position >= $length) break;
            if ($sql[$position] === '\\') $position += 2; elseif (($sql[$position + 1] ?? '') === $quote) $position += 2; else return $position + 1; }
        return $length; }
    public static function literal(mixed $value): ?string {
        if ($value === null) return 'NULL'; if (is_bool($value)) return $value ? '1' : '0'; if (is_int($value)) return (string) $value;
        if (is_float($value)) return is_finite($value) ? var_export($value, true) : null; if (!is_string($value)) return null;
        return $value === '' ? "''" : "'" . addslashes($value) . "'"; }
    public static function interpolate(string $sql, array $params): ?string {
        if (count($params) > 65535 || !array_is_list($params)) return null;
        $length = strlen($sql); $count = count($params); $text = ''; $used = 0; $copied = 0; $position = 0;
        while ($position < $length) { $position += strcspn($sql, "?'\"`#-/", $position); if ($position >= $length) break; $byte = $sql[$position];
            if ($byte === '?') { $literal = $used < $count ? self::literal(value: $params[$used]) : null; if ($literal === null) return null;
                $text .= substr($sql, $copied, $position - $copied) . $literal; $used++; $position++; $copied = $position;
            } elseif ($byte === "'" || $byte === '"' || $byte === '`') $position = self::skipQuoted(sql: $sql, position: $position);
            else { $after = self::skipComment(sql: $sql, position: $position); $position = $after === $position ? $position + 1 : $after; }
        }
        return $used === $count ? $text . substr($sql, $copied) : null;
    }
}
echo json_encode([Repro::interpolate('SELECT ? AS v', [3]), Repro::interpolate('SELECT 5 -', []), Repro::interpolate('SELECT 5 / 2', [])]), "\n";

Run with:

php -d opcache.enable_cli=1 -d opcache.file_update_protection=0 -d opcache.jit=function repro.php

Resulted in this output:

["SELECT 3 AS v",null,null]

But I expected this output instead (what opcache.jit=tracing and opcache.jit=disable print):

["SELECT 3 AS v","SELECT 5 -","SELECT 5 \/ 2"]

Reproduces on PHP 8.5.12 and on master (8.7.0-dev, da7da45a44eb07ac999f5183b232a40d5f857131). The script is a reduced version of a SQL placeholder interpolator.

PHP Version

PHP 8.5.12 (cli) (NTS), built from source; also master 8.7.0-dev at da7da45a44eb07ac999f5183b232a40d5f857131

Operating System

Amazon Linux 2023, aarch64 (container)

主要语言
C
星标
40.7k
派生
8.3k
平均合并
2 天 20 小时
30 天内合并 PR
147

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

php/php-src 的其他 Issue

查看 php/php-src 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。