Skip to content

Commit

Permalink
Improve regex to remove comments from the query to fix incorrect beha…
Browse files Browse the repository at this point in the history
…viour in some cases
  • Loading branch information
donhardman committed Dec 7, 2023
1 parent 9155072 commit 67b5b57
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
17 changes: 13 additions & 4 deletions src/Network/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,22 @@ protected function validateInputFields(array $payload, array $fields): void {
* @throws QueryParseError
*/
protected static function removeComments(string $query): string {
$query = preg_replace(
'/(?<!\')(?<=\s)(--|#)[^\r\n\'\"]*(?=[\r\n]|$)|\/\*.*?\*\//ms',
'',
$query = preg_replace_callback(
'/((\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\')|("--"[^"\r\n]*"|#[^"\r\n]*|\/\*[^!][\s\S]*?\*\/))/',
function ($matches) {
if (strpos($matches[0], '--') === 0
|| strpos($matches[0], '#') === 0
|| strpos($matches[0], '/*') === 0) {
return '';
}

return $matches[0];
},
$query
);

if ($query === null) {
throw new QueryParseError('Error while removing comments from the query using regex');
QueryParseError::throw('Error while removing comments from the query using regex: '. preg_last_error_msg());
}
/** @var string $query */
return trim($query);
Expand Down
6 changes: 4 additions & 2 deletions src/Task/TaskPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ public static function add(string $id, string $body, string $host = 'localhost')
throw new RuntimeException("Task {$id} already exists");
}

static::pool()->set($id, [
static::pool()->set(
$id, [
'id' => substr($id, 0, 24),
'host' => substr($host, 0, 24),
'body' => substr($body, 0, 64),
]);
]
);

return static function () use ($id) {
static::remove($id);
Expand Down

0 comments on commit 67b5b57

Please sign in to comment.