Skip to content

Commit ff3eea9

Browse files
committed
* Ensure number ranges during parsing
* Cast numbers in logical expressions always to floats
1 parent 4ae8e6a commit ff3eea9

File tree

3 files changed

+53
-40
lines changed

3 files changed

+53
-40
lines changed

src/JsonPathEvaluator.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
use Ropi\JsonPathEvaluator\Parser\Ast\LogicalExpression\FunctionNode;
2929
use Ropi\JsonPathEvaluator\Parser\Ast\LogicalExpression\GreaterThanEqualNode;
3030
use Ropi\JsonPathEvaluator\Parser\Ast\LogicalExpression\GreaterThanNode;
31-
use Ropi\JsonPathEvaluator\Parser\Ast\LogicalExpression\IntegerNode;
3231
use Ropi\JsonPathEvaluator\Parser\Ast\LogicalExpression\LessThanEqualNode;
3332
use Ropi\JsonPathEvaluator\Parser\Ast\LogicalExpression\LessThanNode;
3433
use Ropi\JsonPathEvaluator\Parser\Ast\LogicalExpression\LogicalAndNode;
@@ -711,31 +710,7 @@ protected function evaluateLogicalExpressionNode(
711710
return $functionResult instanceof JsonValue ? $functionResult->getValue() : $functionResult;
712711
}
713712

714-
if ($astNode instanceof IntegerNode) {
715-
if ($astNode->token->value > (string)PHP_INT_MAX || $astNode->token->value < (string)PHP_INT_MIN) {
716-
throw new JsonPathEvaluationException(
717-
'Integer is out of range (possible range from ' . PHP_INT_MIN . ' to ' . PHP_INT_MAX . ')',
718-
$astNode->token->position,
719-
$evaluationContext->expression,
720-
1702863910
721-
);
722-
}
723-
724-
return (int)$astNode->token->value;
725-
}
726-
727713
if ($astNode instanceof FloatNode) {
728-
$float = floatval($astNode->token->value);
729-
730-
if ($float === INF || $float === -INF) {
731-
throw new JsonPathEvaluationException(
732-
'Float is out of range (possible range from ' . PHP_FLOAT_MIN . ' to ' . PHP_FLOAT_MAX . ')',
733-
$astNode->token->position,
734-
$evaluationContext->expression,
735-
1702863907
736-
);
737-
}
738-
739714
return (float)$astNode->token->value;
740715
}
741716

src/Parser/Ast/LogicalExpression/IntegerNode.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/Parser/JsonPathParser.php

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use Ropi\JsonPathEvaluator\Parser\Ast\LogicalExpression\FunctionNode;
2222
use Ropi\JsonPathEvaluator\Parser\Ast\LogicalExpression\GreaterThanEqualNode;
2323
use Ropi\JsonPathEvaluator\Parser\Ast\LogicalExpression\GreaterThanNode;
24-
use Ropi\JsonPathEvaluator\Parser\Ast\LogicalExpression\IntegerNode;
2524
use Ropi\JsonPathEvaluator\Parser\Ast\LogicalExpression\LessThanEqualNode;
2625
use Ropi\JsonPathEvaluator\Parser\Ast\LogicalExpression\LessThanNode;
2726
use Ropi\JsonPathEvaluator\Parser\Ast\LogicalExpression\LogicalAndNode;
@@ -164,6 +163,7 @@ protected function parseBracketSelector(): AbstractSelectorNode
164163
if ($this->getCurrentToken()::class === IntegerToken::class) {
165164
/** @var IntegerToken $integerToken */
166165
$integerToken = $this->consume(IntegerToken::class);
166+
$this->ensureIntegerRange($integerToken);
167167
}
168168

169169
if ($this->getCurrentToken()::class === ColonToken::class) {
@@ -192,6 +192,7 @@ protected function parseArraySliceSelector(?IntegerToken $startToken = null): Ar
192192
$colonToken = $this->consume(ColonToken::class);
193193

194194
if ($this->getCurrentToken()::class === IntegerToken::class) {
195+
/** @var IntegerToken $endToken */
195196
$endToken = $this->consume(IntegerToken::class);
196197
}
197198

@@ -200,16 +201,29 @@ protected function parseArraySliceSelector(?IntegerToken $startToken = null): Ar
200201
}
201202

202203
if ($this->getCurrentToken()::class === IntegerToken::class) {
204+
/** @var IntegerToken $stepToken */
203205
$stepToken = $this->consume(IntegerToken::class);
204206
}
205207

206208
$token = $startToken ?? $endToken ?? $stepToken ?? $colonToken;
207209

210+
if (isset($endToken)) {
211+
$this->ensureIntegerRange($endToken);
212+
} else {
213+
$endToken = null;
214+
}
215+
216+
if (isset($stepToken)) {
217+
$this->ensureIntegerRange($stepToken);
218+
} else {
219+
$stepToken = null;
220+
}
221+
208222
return new ArraySliceSelectorNode(
209223
$token,
210224
$startToken?->value,
211-
isset($endToken) ? $endToken->value : null,
212-
isset($stepToken) ? $stepToken->value : null,
225+
$endToken?->value,
226+
$stepToken?->value,
213227
);
214228
}
215229

@@ -308,11 +322,8 @@ protected function parseLogicalExpressionTerm(): AbstractSelectorNode|AbstractSe
308322
NullToken::class,
309323
);
310324

311-
if ($token::class === IntegerToken::class) {
312-
return new IntegerNode($token);
313-
}
314-
315-
if ($token::class === FloatToken::class) {
325+
if ($token::class === IntegerToken::class || $token::class === FloatToken::class) {
326+
$this->ensureFloatRange($token);
316327
return new FloatNode($token);
317328
}
318329

@@ -363,4 +374,38 @@ protected function parseFunctionArgument(): AbstractSegmentNode|AbstractSelector
363374

364375
return $this->parseLogicalExpression();
365376
}
377+
378+
/**
379+
* @throws SyntaxException
380+
*/
381+
private function ensureIntegerRange(IntegerToken $token): void
382+
{
383+
$float = floatval($token->value);
384+
385+
if ($float > PHP_INT_MAX || $float < PHP_INT_MIN) {
386+
throw new SyntaxException(
387+
'Integer is out of range (possible range from ' . PHP_INT_MIN . ' to ' . PHP_INT_MAX . ')',
388+
$token->position,
389+
$this->getParsingExpression(),
390+
1702863907
391+
);
392+
}
393+
}
394+
395+
/**
396+
* @throws SyntaxException
397+
*/
398+
private function ensureFloatRange(FloatToken|IntegerToken $token): void
399+
{
400+
$float = floatval($token->value);
401+
402+
if ($float === INF || $float === -INF) {
403+
throw new SyntaxException(
404+
'Number is out of range (possible range from ' . PHP_FLOAT_MIN . ' to ' . PHP_FLOAT_MAX . ')',
405+
$token->position,
406+
$this->getParsingExpression(),
407+
1702866702
408+
);
409+
}
410+
}
366411
}

0 commit comments

Comments
 (0)