From 9d5802cb9f0897e98c018420f4d2db11547ec43e Mon Sep 17 00:00:00 2001 From: epriestley Date: Wed, 17 Feb 2021 12:42:09 -0800 Subject: [PATCH] Provide some "preg_*" wrappers which raise exceptions on failure Summary: Ref T13608. Ref T13100. Ref T13586. Properly checking "preg_match()" and similar calls for failure and raising useful exceptions is complicated and error-prone. Provide wrapper functions with an API that's more consistent with the rest of the codebase: matches are returned; and errors raise detailed exceptions. Test Plan: See next change. Maniphest Tasks: T13608, T13586, T13100 Differential Revision: https://secure.phabricator.com/D21561 --- src/__phutil_library_map__.php | 5 ++ src/exception/PhutilRegexException.php | 3 + src/utils/utils.php | 86 ++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 src/exception/PhutilRegexException.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 0bd7549a..8c46addd 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -849,6 +849,7 @@ phutil_register_library_map(array( 'PhutilRawEnglishLocale' => 'internationalization/locales/PhutilRawEnglishLocale.php', 'PhutilReadableSerializer' => 'readableserializer/PhutilReadableSerializer.php', 'PhutilReadableSerializerTestCase' => 'readableserializer/__tests__/PhutilReadableSerializerTestCase.php', + 'PhutilRegexException' => 'exception/PhutilRegexException.php', 'PhutilRope' => 'utils/PhutilRope.php', 'PhutilRopeTestCase' => 'utils/__tests__/PhutilRopeTestCase.php', 'PhutilServiceProfiler' => 'serviceprofiler/PhutilServiceProfiler.php', @@ -1009,6 +1010,9 @@ phutil_register_library_map(array( 'phutil_partition' => 'utils/utils.php', 'phutil_passthru' => 'future/exec/execx.php', 'phutil_person' => 'internationalization/pht.php', + 'phutil_preg_match' => 'utils/utils.php', + 'phutil_preg_match_all' => 'utils/utils.php', + 'phutil_raise_preg_exception' => 'utils/utils.php', 'phutil_register_library' => 'init/lib/core.php', 'phutil_register_library_map' => 'init/lib/core.php', 'phutil_set_system_locale' => 'utils/utf8.php', @@ -1925,6 +1929,7 @@ phutil_register_library_map(array( 'PhutilRawEnglishLocale' => 'PhutilLocale', 'PhutilReadableSerializer' => 'Phobject', 'PhutilReadableSerializerTestCase' => 'PhutilTestCase', + 'PhutilRegexException' => 'Exception', 'PhutilRope' => 'Phobject', 'PhutilRopeTestCase' => 'PhutilTestCase', 'PhutilServiceProfiler' => 'Phobject', diff --git a/src/exception/PhutilRegexException.php b/src/exception/PhutilRegexException.php new file mode 100644 index 00000000..90b1b3bf --- /dev/null +++ b/src/exception/PhutilRegexException.php @@ -0,0 +1,3 @@ +getErrorsAsString(); + + $trap->destroy(); + + $pattern = $argv[0]; + $pattern_display = sprintf( + '"%s"', + addcslashes($pattern, '\\\"')); + + $message = array(); + $message[] = pht( + 'Call to %s(%s, ...) failed.', + $function, + $pattern_display); + + if (strlen($error_message)) { + $message[] = pht( + 'Regular expression engine emitted message: %s', + $error_message); + } + + $message = implode("\n\n", $message); + + throw new PhutilRegexException($message); +}