Skip to content

Commit 67c3ffa

Browse files
bug #41801 [Uid] Fix fromString() with low base58 values (fancyweb)
This PR was merged into the 5.2 branch. Discussion ---------- [Uid] Fix fromString() with low base58 values | Q | A | ------------- | --- | Branch? | 5.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - When converting base58 values to binary, we forgot to pad the result with null byte, so if the result is too "short", the code breaks. Commits ------- cd12921 [Uid] Fix fromString() with low base58 values
2 parents e1b81e3 + cd12921 commit 67c3ffa

File tree

5 files changed

+26
-6
lines changed

5 files changed

+26
-6
lines changed

src/Symfony/Component/Uid/AbstractUid.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ abstract public static function fromString(string $uid): self;
4343
abstract public function toBinary(): string;
4444

4545
/**
46-
* Returns the identifier as a base-58 case sensitive string.
46+
* Returns the identifier as a base58 case sensitive string.
4747
*/
4848
public function toBase58(): string
4949
{
5050
return strtr(sprintf('%022s', BinaryUtil::toBase($this->toBinary(), BinaryUtil::BASE58)), '0', '1');
5151
}
5252

5353
/**
54-
* Returns the identifier as a base-32 case insensitive string.
54+
* Returns the identifier as a base32 case insensitive string.
5555
*/
5656
public function toBase32(): string
5757
{

src/Symfony/Component/Uid/Tests/UlidTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,9 @@ public function testFromStringOnExtendedClassReturnsStatic()
124124
{
125125
$this->assertInstanceOf(CustomUlid::class, CustomUlid::fromString((new CustomUlid())->toBinary()));
126126
}
127+
128+
public function testFromStringBase58Padding()
129+
{
130+
$this->assertInstanceOf(Ulid::class, Ulid::fromString('111111111u9QRyVM94rdmZ'));
131+
}
127132
}

src/Symfony/Component/Uid/Tests/UuidTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,24 @@ public function testCompare()
187187
$this->assertSame([$a, $b, $c, $d], $uuids);
188188
}
189189

190-
public function testNilUuid()
190+
/**
191+
* @testWith ["00000000-0000-0000-0000-000000000000"]
192+
* ["1111111111111111111111"]
193+
* ["00000000000000000000000000"]
194+
*/
195+
public function testNilUuid(string $uuid)
191196
{
192-
$uuid = Uuid::fromString('00000000-0000-0000-0000-000000000000');
197+
$uuid = Uuid::fromString($uuid);
193198

194199
$this->assertInstanceOf(NilUuid::class, $uuid);
195200
$this->assertSame('00000000-0000-0000-0000-000000000000', (string) $uuid);
196201
}
197202

203+
public function testNewNilUuid()
204+
{
205+
$this->assertSame('00000000-0000-0000-0000-000000000000', (string) new NilUuid());
206+
}
207+
198208
public function testFromStringOnExtendedClassReturnsStatic()
199209
{
200210
$this->assertInstanceOf(CustomUuid::class, CustomUuid::fromString(self::A_UUID_V4));
@@ -208,4 +218,9 @@ public function testGetTime()
208218
$this->assertSame(-0.0000001, (new UuidV1('13813fff-1dd2-11b2-a456-426655440000'))->getTime());
209219
$this->assertSame(-12219292800.0, ((new UuidV1('00000000-0000-1000-a456-426655440000'))->getTime()));
210220
}
221+
222+
public function testFromStringBase58Padding()
223+
{
224+
$this->assertInstanceOf(Uuid::class, Uuid::fromString('111111111u9QRyVM94rdmZ'));
225+
}
211226
}

src/Symfony/Component/Uid/Ulid.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static function fromString(string $ulid): parent
6161
if (36 === \strlen($ulid) && Uuid::isValid($ulid)) {
6262
$ulid = (new Uuid($ulid))->toBinary();
6363
} elseif (22 === \strlen($ulid) && 22 === strspn($ulid, BinaryUtil::BASE58[''])) {
64-
$ulid = BinaryUtil::fromBase($ulid, BinaryUtil::BASE58);
64+
$ulid = str_pad(BinaryUtil::fromBase($ulid, BinaryUtil::BASE58), 16, "\0", \STR_PAD_LEFT);
6565
}
6666

6767
if (16 !== \strlen($ulid)) {

src/Symfony/Component/Uid/Uuid.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct(string $uuid)
3737
public static function fromString(string $uuid): parent
3838
{
3939
if (22 === \strlen($uuid) && 22 === strspn($uuid, BinaryUtil::BASE58[''])) {
40-
$uuid = BinaryUtil::fromBase($uuid, BinaryUtil::BASE58);
40+
$uuid = str_pad(BinaryUtil::fromBase($uuid, BinaryUtil::BASE58), 16, "\0", \STR_PAD_LEFT);
4141
}
4242

4343
if (16 === \strlen($uuid)) {

0 commit comments

Comments
 (0)