Skip to content

[VarDumper] Wrong encoding, conversion from "CP850" to "UTF-8" is not allowed #61141

@OxCom

Description

@OxCom

Symfony version(s) affected

7.3

Description

In the class \Symfony\Component\VarDumper\Dumper\AbstractDumper the method utf8Encode fails to process iconv() call when some encodings are missing.

How to reproduce

Call AbstractDumper::utf8Encode() on environment without CP850 encoding

$s = hex2bin('0196cdc7816e3d48ac16a3771681af71');
$abstractDumper->utf8Encode($s);

Possible Solution

  • Mute warnings
  • Pass to utf8Encode flag $bin and convert binary data to hex string representation:
protected function utf8Encode(?string $s, bool $bin = false): ?string
{   
    if (null === $s || preg_match('//u', $s)) {
        return $s;
    }

    if ($bin) {
        return '0x' . \bin2hex($s);
    }

    if (!\function_exists('iconv')) {
        throw new \RuntimeException('Unable to convert a non-UTF-8 string to UTF-8: required function iconv() does not exist. You should install ext-iconv or symfony/polyfill-iconv.');
    }

    if (false !== $c = @iconv($this->charset, 'UTF-8', $s)) {
        return $c;
    }
    if ('CP1252' !== $this->charset && false !== $c = @iconv('CP1252', 'UTF-8', $s)) {
        return $c;
    }  

    return @iconv('CP850', 'UTF-8', $s);
}

Additional Context

Real scenario: use #[ORM\Column(type: 'ulid', unique: true)] on entity ID. Visit page of the symfony profile -> doctrine. Some IDs from such entities lands to this function and fails on return iconv('CP850', 'UTF-8', $s);

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