diff --git a/cli/smokeping.php b/cli/smokeping.php index 9e8e292..5d0aa4a 100644 --- a/cli/smokeping.php +++ b/cli/smokeping.php @@ -96,7 +96,7 @@ function smokeping_cli_import_smokeping(array $options, $file, $apply, $confirm, $service = smokeping_import_service(); foreach (array('device-template', 'poller', 'site', 'tree') as $name) { - if (isset($options[$name]) && !preg_match('/^[0-9]{1,9}$/', $options[$name])) { + if (isset($options[$name]) && !preg_match('/^[0-9]{1,9}$/D', $options[$name])) { fwrite(STDERR, 'ERROR: --' . $name . ' must be a number' . PHP_EOL . $usage); return 1; diff --git a/lib/ConfigSchema.php b/lib/ConfigSchema.php index f5ac82c..34bbc94 100644 --- a/lib/ConfigSchema.php +++ b/lib/ConfigSchema.php @@ -106,7 +106,7 @@ public static function build(ConfigTree $tree): ConfigDocument { * look like its type stays a string, and the check reports it. */ public static function coerce(string $type, string $text): int|bool|string { - if ($type === 'int' && preg_match('/^-?[0-9]{1,18}$/', $text) === 1) { + if ($type === 'int' && preg_match('/^-?[0-9]{1,18}$/D', $text) === 1) { return (int) $text; } diff --git a/lib/LossColors.php b/lib/LossColors.php index 3ab3bc8..dd61864 100644 --- a/lib/LossColors.php +++ b/lib/LossColors.php @@ -39,7 +39,7 @@ enum LossColors: string { * 2.007003; null for a version that is not in that form. */ public static function ofVersion(string $version): ?self { - if (preg_match('/^(\d+)\.(\d{3})\d{3}$/', $version, $m) !== 1) { + if (preg_match('/^(\d+)\.(\d{3})\d{3}$/D', $version, $m) !== 1) { return null; } @@ -59,7 +59,7 @@ public static function resolve(string $setting, string $detectedVersion): self { * 2.7.3; anything else unchanged. */ public static function displayVersion(string $version): string { - if (preg_match('/^(\d+)\.(\d{3})(\d{3})$/', $version, $m) !== 1) { + if (preg_match('/^(\d+)\.(\d{3})(\d{3})$/D', $version, $m) !== 1) { return $version; } diff --git a/lib/SmokepingInventory.php b/lib/SmokepingInventory.php index 961eabb..dc2d8a5 100644 --- a/lib/SmokepingInventory.php +++ b/lib/SmokepingInventory.php @@ -125,7 +125,7 @@ private static function probeProperties(?SmokepingSection $probes, string $name) } private static function intOrNull(?string $value): ?int { - if ($value === null || preg_match('/^\d+$/', $value) !== 1) { + if ($value === null || preg_match('/^\d+$/D', $value) !== 1) { return null; } diff --git a/lib/SummaryScript.php b/lib/SummaryScript.php index ee0f0a2..db7f78d 100644 --- a/lib/SummaryScript.php +++ b/lib/SummaryScript.php @@ -51,7 +51,7 @@ public function values(string $series): SummaryValues { * @return array{target: int, location: string}|null */ public static function parse(string $series): ?array { - if (preg_match('/^([1-9][0-9]{0,9})~([-_.0-9a-zA-Z]{0,64})$/', $series, $match) !== 1) { + if (preg_match('/^([1-9][0-9]{0,9})~([-_.0-9a-zA-Z]{0,64})$/D', $series, $match) !== 1) { return null; } diff --git a/lib/YamlReader.php b/lib/YamlReader.php index d550104..90d9a15 100644 --- a/lib/YamlReader.php +++ b/lib/YamlReader.php @@ -463,7 +463,7 @@ private function quoted(string $text, int $start, int $no): array { $digits = $e === 'x' ? 2 : ($e === 'u' ? 4 : 0); $hex = $digits > 0 ? substr($text, $i + 1, $digits) : ''; - if ($digits === 0 || strlen($hex) !== $digits || preg_match('/^[0-9A-Fa-f]+$/', $hex) !== 1) { + if ($digits === 0 || strlen($hex) !== $digits || preg_match('/^[0-9A-Fa-f]+$/D', $hex) !== 1) { throw new ConfigException(sprintf('Unknown escape "\\%s" in a double-quoted scalar', $e), $no); } diff --git a/lib/YamlScalar.php b/lib/YamlScalar.php index 56cb52d..bdea87a 100644 --- a/lib/YamlScalar.php +++ b/lib/YamlScalar.php @@ -53,16 +53,16 @@ public static function classify(string $text): array { return [self::BOOL, false]; } - if (preg_match('/^(0|-?[1-9][0-9]{0,17})$/', $text) === 1) { + if (preg_match('/^(0|-?[1-9][0-9]{0,17})$/D', $text) === 1) { return [self::INT, (int) $text]; } if ( - preg_match('/^(y|n|yes|no|on|off)$/i', $text) === 1 - || preg_match('/^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$/', $text) === 1 + preg_match('/^(y|n|yes|no|on|off)$/iD', $text) === 1 + || preg_match('/^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$/D', $text) === 1 || preg_match('/^[-+]?0[xXoObB]/', $text) === 1 - || preg_match('/^[-+]?\.(inf|nan)$/i', $text) === 1 - || preg_match('/^[-+]?[0-9][0-9_]*(:[0-5]?[0-9])+(\.[0-9]*)?$/', $text) === 1 + || preg_match('/^[-+]?\.(inf|nan)$/iD', $text) === 1 + || preg_match('/^[-+]?[0-9][0-9_]*(:[0-5]?[0-9])+(\.[0-9]*)?$/D', $text) === 1 || preg_match('/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}/', $text) === 1 || $text === '=' || $text === '<<' diff --git a/tests/Unit/ConfigTest.php b/tests/Unit/ConfigTest.php index 999431a..df68ecc 100644 --- a/tests/Unit/ConfigTest.php +++ b/tests/Unit/ConfigTest.php @@ -498,3 +498,8 @@ public function bytes(int $length): string { expect(fn () => $service->apply($document, $plan->id(), 1, 1))->toThrow(ConfigException::class, 'do not cover every device'); }); + +it('leaves integer text with a trailing newline as a string for the check to report', function () { + expect(Somethingwithproof\Smokeping\ConfigSchema::coerce('int', '42'))->toBe(42) + ->and(Somethingwithproof\Smokeping\ConfigSchema::coerce('int', "42\n"))->toBe("42\n"); +}); diff --git a/tests/Unit/LossColorsTest.php b/tests/Unit/LossColorsTest.php index a909376..6c25fd0 100644 --- a/tests/Unit/LossColorsTest.php +++ b/tests/Unit/LossColorsTest.php @@ -61,6 +61,7 @@ function lc_dir(array $files): string { ['3.000000', LossColors::Current], ['', null], ['2.8.2', null], + ["2.007003\n", null], ]); it('follows the setting, else the detected version, else 2.8 and later', function () { @@ -72,7 +73,8 @@ function lc_dir(array $files): string { ->and(LossColors::resolve('bogus', ''))->toBe(LossColors::Current) ->and(LossColors::Legacy->other())->toBe(LossColors::Current) ->and(LossColors::displayVersion('2.007003'))->toBe('2.7.3') - ->and(LossColors::displayVersion('odd'))->toBe('odd'); + ->and(LossColors::displayVersion('odd'))->toBe('odd') + ->and(LossColors::displayVersion("2.007003\n"))->toBe("2.007003\n"); }); it('names a graph template per ping count and color table', function () { diff --git a/tests/Unit/SummaryTest.php b/tests/Unit/SummaryTest.php index c0070dd..83b3153 100644 --- a/tests/Unit/SummaryTest.php +++ b/tests/Unit/SummaryTest.php @@ -97,6 +97,8 @@ function summaryObservation(?float $loss, ?float $median, bool $present = true, 'space' => ['12~a b', null], 'shell' => ['12~$(id)', null], 'empty' => ['', null], + 'trailing newline' => ["12~berlin\n", null], + 'newline after separator' => ["12~\n", null], ]); it('builds the key the data source stores', function () { diff --git a/tests/Unit/YamlTest.php b/tests/Unit/YamlTest.php index 396573a..d4bba1f 100644 --- a/tests/Unit/YamlTest.php +++ b/tests/Unit/YamlTest.php @@ -19,6 +19,7 @@ use Somethingwithproof\Smokeping\ConfigException; use Somethingwithproof\Smokeping\YamlReader; +use Somethingwithproof\Smokeping\YamlScalar; use Somethingwithproof\Smokeping\YamlWriter; function smokeping_yaml(string $text): array { @@ -288,3 +289,7 @@ function smokeping_random_map(int $depth, callable $value): array { // Both outcomes must occur, or the corpus is not exercising the reader. expect($ok)->toBeGreaterThan(100)->and($bad)->toBeGreaterThan(100); }); + +it('classifies a scalar with a trailing newline as a plain string, not a number or ambiguous word', function (string $text) { + expect(YamlScalar::classify($text)[0])->toBe(YamlScalar::STRING); +})->with(["12\n", "yes\n", "1.5\n", ".inf\n", "1:30\n"]);