Skip to content

Uninitialized string offset 0 in Str::replaceScopeNameCapture() #110

@magarrent

Description

@magarrent

Root cause:

while ($result[0] === '.') {  // ERROR: No length check
    $result = substr($result, 1);
}

Problem: Code accesses $result[0] without checking if string has content, causing errors when processing empty capture groups.

Reproduction:

use Phiki\Phiki;
use Phiki\Theme\Theme;

$phiki = new Phiki();
$code = '@if (ViewData.Model.Count == 0) { <!-- No-data placeholder with \"New Standard +\" button --> } else { <tr> <th>Standard <span class=\"caretDwn\"></span></th> <!-- ... other headers ... --> </tr> @foreach (var standard in Model) { <tr id=\"@standard.Uid\" class=\"@rowClass hand\" ...> <td><a href=\"@navigateScript\">@standard.Title</a></td> <!-- ... other columns ... --> </tr> } } ';
$html = $phiki->codeToHtml($code, 'html', [
                    'light' => Theme::CatppuccinMocha,
                    'dark' => Theme::CatppuccinMocha,
                ])->toString();
// Throws: local.ERROR: Uninitialized string offset 0 {"code":"@if (ViewData.Model.Count == 0) { <!-- No-data placeholder with \\\"New Standard +\\\" button --> } else { <tr> <th>Standard <span class=\\\"caretDwn\\\"></span></th> <!-- ... other headers ... --> </tr> @foreach (var standard in Model) { <tr id=\\\"@standard.Uid\\\" class=\\\"@rowClass hand\\\" ...> <td><a href=\\\"@navigateScript\\\">@standard.Title</a></td> <!-- ... other columns ... --> </tr> } }  ","language":"html"} 

Possible fix:

while (!empty($result) && $result[0] === '.') {
    $result = substr($result, 1);
}

Environment:

  • PHP 8.4.11
  • Laravel 12.25.0
  • Phiki 2.0.0-beta4

Seems to be related with some "@" or on html lang

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