UUID: properly account for garbage inputs which aren't valid hexadecimal

this would previously throw a TypeError and crash.
This commit is contained in:
Dylan K. Taylor 2020-04-15 09:44:14 +01:00
parent e3ebf8bb61
commit ccad97727f

View File

@ -61,7 +61,12 @@ class UUID{
* Creates an UUID from an hexadecimal representation
*/
public static function fromString(string $uuid, int $version = null) : UUID{
return self::fromBinary(hex2bin(str_replace("-", "", trim($uuid))), $version);
//TODO: should we be stricter about the notation (8-4-4-4-12)?
$binary = @hex2bin(str_replace("-", "", trim($uuid)));
if($binary === false){
throw new \InvalidArgumentException("Invalid hex string UUID representation");
}
return self::fromBinary($binary, $version);
}
/**