Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7daef4f

Browse files
[Uid] fix checking for valid UUIDs
1 parent a933c3e commit 7daef4f

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

src/Symfony/Component/Uid/BinaryUtil.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class BinaryUtil
4040
// 0x01b21dd213814000 is the number of 100-ns intervals between the
4141
// UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
4242
private const TIME_OFFSET_INT = 0x01b21dd213814000;
43-
private const TIME_OFFSET_COM = "\xfe\x4d\xe2\x2d\xec\x7e\xc0\x00";
43+
private const TIME_OFFSET_COM2 = "\xfe\x4d\xe2\x2d\xec\x7e\xc0\x00";
4444

4545
public static function toBase(string $bytes, array $map): string
4646
{
@@ -121,9 +121,15 @@ public static function timeToFloat(string $time): float
121121
}
122122

123123
$time = str_pad(hex2bin($time), 8, "\0", \STR_PAD_LEFT);
124-
$time = self::add($time, self::TIME_OFFSET_COM);
125-
$time[0] = $time[0] & "\x7F";
124+
$time = self::add($time, self::TIME_OFFSET_COM2);
126125

127-
return self::toBase($time, self::BASE10) / 10000000;
126+
if ($time >= self::TIME_OFFSET_COM2) {
127+
$time = -1 * self::toBase($time ^ "\xff\xff\xff\xff\xff\xff\xff\xff", self::BASE10);
128+
} else {
129+
$time[0] = $time[0] & "\x7F";
130+
$time = self::toBase($time, self::BASE10);
131+
}
132+
133+
return $time / 10000000;
128134
}
129135
}

src/Symfony/Component/Uid/Ulid.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ private static function generate(): string
153153
if (\PHP_INT_SIZE >= 8) {
154154
$time = base_convert($time, 10, 32);
155155
} else {
156-
$time = bin2hex(BinaryUtil::fromBase($time, BinaryUtil::BASE10));
156+
$time = str_pad(bin2hex(BinaryUtil::fromBase($time, BinaryUtil::BASE10)), 12, '0', \STR_PAD_LEFT);
157157
$time = sprintf('%s%04s%04s',
158158
base_convert(substr($time, 0, 2), 16, 32),
159159
base_convert(substr($time, 2, 5), 16, 32),

src/Symfony/Component/Uid/Uuid.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ class Uuid extends AbstractUid
2222

2323
public function __construct(string $uuid)
2424
{
25-
try {
26-
$type = uuid_type($uuid);
27-
} catch (\ValueError $e) {
28-
throw new \InvalidArgumentException(sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid), 0, $e);
29-
}
25+
$type = uuid_is_valid($uuid) ? uuid_type($uuid) : false;
3026

3127
if (false === $type || \UUID_TYPE_INVALID === $type || (static::TYPE ?: $type) !== $type) {
3228
throw new \InvalidArgumentException(sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
@@ -59,13 +55,11 @@ public static function fromString(string $uuid): parent
5955
return new static($uuid);
6056
}
6157

62-
try {
63-
$type = uuid_type($uuid);
64-
} catch (\ValueError $e) {
65-
throw new \InvalidArgumentException(sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid), 0, $e);
58+
if (!uuid_is_valid($uuid)) {
59+
throw new \InvalidArgumentException(sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
6660
}
6761

68-
switch ($type) {
62+
switch (uuid_type($uuid)) {
6963
case UuidV1::TYPE: return new UuidV1($uuid);
7064
case UuidV3::TYPE: return new UuidV3($uuid);
7165
case UuidV4::TYPE: return new UuidV4($uuid);
@@ -114,7 +108,7 @@ public static function isValid(string $uuid): bool
114108
return uuid_is_valid($uuid);
115109
}
116110

117-
return static::TYPE === uuid_type($uuid);
111+
return uuid_is_valid($uuid) && static::TYPE === uuid_type($uuid);
118112
}
119113

120114
public function toBinary(): string

0 commit comments

Comments
 (0)