Skip to content

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

Open
@lcharette

Description

@lcharette

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions