Use unicode modifier for preg_match to avoid problems when parameters contain non-unicode strings

This commit is contained in:
Jan Böhmer 2024-09-09 00:08:15 +02:00
parent 6bc1808fd6
commit 95701a6438

View file

@ -1194,7 +1194,7 @@ class OEMSecretsProvider implements InfoProviderInterface
$result['value_text'] = trim($textValue); $result['value_text'] = trim($textValue);
} else { } else {
// Check if the value is numeric with a unit // Check if the value is numeric with a unit
if (preg_match('/^([\+\-]?\d+(\.\d+)?)([a-zA-Z%°]+)?$/', $value, $matches)) { if (preg_match('/^([\+\-]?\d+(\.\d+)?)([a-zA-Z%°]+)?$/u', $value, $matches)) {
// It is a number with or without a unit // It is a number with or without a unit
$result['value_typ'] = isset($matches[1]) ? (float)$matches[1] : null; $result['value_typ'] = isset($matches[1]) ? (float)$matches[1] : null;
$result['unit'] = $matches[3] ?? null; $result['unit'] = $matches[3] ?? null;
@ -1234,7 +1234,7 @@ class OEMSecretsProvider implements InfoProviderInterface
$value_typ = null; $value_typ = null;
// Search for the number + unit pattern // Search for the number + unit pattern
if (preg_match('/^([\+\-]?\d+(\.\d+)?)([a-zA-Z%°]+)?$/', $value1, $matches)) { if (preg_match('/^([\+\-]?\d+(\.\d+)?)([a-zA-Z%°]+)?$/u', $value1, $matches)) {
$value_typ = $matches[1]; $value_typ = $matches[1];
$unit = $matches[3] ?? null; $unit = $matches[3] ?? null;
} }
@ -1245,7 +1245,7 @@ class OEMSecretsProvider implements InfoProviderInterface
]; ];
if ($value2 !== null) { if ($value2 !== null) {
if (preg_match('/^([\+\-]?\d+(\.\d+)?)([a-zA-Z%°]+)?$/', $value2, $matches2)) { if (preg_match('/^([\+\-]?\d+(\.\d+)?)([a-zA-Z%°]+)?$/u', $value2, $matches2)) {
$result['value_min'] = $value_typ; $result['value_min'] = $value_typ;
$result['value_max'] = $matches2[1]; $result['value_max'] = $matches2[1];
$result['unit'] = $matches2[3] ?? $unit; // If both values have the same unit, we keep it $result['unit'] = $matches2[3] ?? $unit; // If both values have the same unit, we keep it