Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Console][Table] Invalid "UTF-8" string caused by text warping #58286

Open
lcharette opened this issue Sep 17, 2024 · 1 comment
Open

[Console][Table] Invalid "UTF-8" string caused by text warping #58286

lcharette opened this issue Sep 17, 2024 · 1 comment

Comments

@lcharette
Copy link

lcharette commented Sep 17, 2024

Symfony version(s) affected

6.4

Description

When using the Console table helper, the following exception is sometimes thrown : Invalid "UTF-8" string.. I've found the cause to be the following line, which may split an UTF-8 character badly when the cell text is wrapped on multiple line.

https://github.com/symfony/console/blob/3e36da1448dd7bd64e623135da852858ec6fb72e/Formatter/OutputFormatter.php#L172

applyCurrentStyle will indeed receive an Invalid "UTF-8" string because of the result of substr:

Screenshot

Explanation : https://www.php.net/manual/en/function.substr.php#90581

How to reproduce

Code :

#!/usr/bin/env php
<?php
// First, run "composer require symfony/console"
require __DIR__.'/vendor/autoload.php';

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

$application = new Application();

// ... register commands
$application->register('debug')
    ->setCode(function (InputInterface $input, OutputInterface $output): int {
        $table = new Table($output);
        $table->setHeaders(['Message']);
        $table->setColumnMaxWidth(0, 50);

        $table->addRow([
            "Usuário <strong>{{user_name}}</strong> não é válido."
        ]);

        $table->render();

        return Command::SUCCESS;
    });

$application->run();

Run :

php test.php debug

Result should be :

In ByteString.php line 444:
                           
  Invalid "UTF-8" string.  
                           

debug

Possible Solution

Replacing substr with mb_substr on this line will solve the issue. However it might be prudent to check if mb_substr should be used elsewhere.

Additional Context

No response

@stof
Copy link
Member

stof commented Sep 17, 2024

Just replacing substr with mb_substr would break things entirely (even for cases where the current logic is lucky enough to avoid breaking UTF-8 codepoints). mb_substr expects an offset computed in codepoints, not in bytes, so the computation of the offset needs to be changed as well

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

No branches or pull requests

3 participants