2121use Ropi \JsonPathEvaluator \Parser \Ast \LogicalExpression \FunctionNode ;
2222use Ropi \JsonPathEvaluator \Parser \Ast \LogicalExpression \GreaterThanEqualNode ;
2323use Ropi \JsonPathEvaluator \Parser \Ast \LogicalExpression \GreaterThanNode ;
24- use Ropi \JsonPathEvaluator \Parser \Ast \LogicalExpression \IntegerNode ;
2524use Ropi \JsonPathEvaluator \Parser \Ast \LogicalExpression \LessThanEqualNode ;
2625use Ropi \JsonPathEvaluator \Parser \Ast \LogicalExpression \LessThanNode ;
2726use 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