From 468a68cff453f7b254574a49d9bdda9ff10d7869 Mon Sep 17 00:00:00 2001 From: HypeMC Date: Sat, 29 Jul 2023 04:50:19 +0200 Subject: [PATCH] Allow passing null as length to slice functions --- src/common.h | 4 ++++ src/php/classes/php_deque_ce.c | 12 ++++++++---- src/php/classes/php_map_ce.c | 12 ++++++++---- src/php/classes/php_set_ce.c | 12 ++++++++---- src/php/classes/php_vector_ce.c | 12 ++++++++---- src/php/parameters.h | 5 +++++ 6 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/common.h b/src/common.h index 509a6bb..832adec 100644 --- a/src/common.h +++ b/src/common.h @@ -216,6 +216,10 @@ int name##_unserialize( \ zend_ce_type_error, \ "Index must be of type integer, %s given", zend_get_type_by_const(Z_TYPE_P(z))) +#define INTEGER_LENGTH_REQUIRED(z) ds_throw_exception( \ + zend_ce_type_error, \ + "Length must be of type integer, %s given", zend_get_type_by_const(Z_TYPE_P(z))) + #define ITERATION_BY_REF_NOT_SUPPORTED() ds_throw_exception( \ zend_ce_error, \ "Iterating by reference is not supported") diff --git a/src/php/classes/php_deque_ce.c b/src/php/classes/php_deque_ce.c index f57b86c..08751e2 100644 --- a/src/php/classes/php_deque_ce.c +++ b/src/php/classes/php_deque_ce.c @@ -85,11 +85,15 @@ METHOD(slice) { ds_deque_t *deque = THIS_DS_DEQUE(); - if (ZEND_NUM_ARGS() > 1) { - PARSE_LONG_AND_LONG(index, length); - RETURN_DS_DEQUE(ds_deque_slice(deque, index, length)); + PARSE_LONG_AND_OPTIONAL_ZVAL(index, length); + + if (ZEND_NUM_ARGS() > 1 && Z_TYPE_P(length) != IS_NULL) { + if (Z_TYPE_P(length) != IS_LONG) { + INTEGER_LENGTH_REQUIRED(length); + } else { + RETURN_DS_DEQUE(ds_deque_slice(deque, index, Z_LVAL_P(length))); + } } else { - PARSE_LONG(index); RETURN_DS_DEQUE(ds_deque_slice(deque, index, deque->size)); } } diff --git a/src/php/classes/php_map_ce.c b/src/php/classes/php_map_ce.c index 0262d4c..a1e53d8 100644 --- a/src/php/classes/php_map_ce.c +++ b/src/php/classes/php_map_ce.c @@ -246,11 +246,15 @@ METHOD(slice) { ds_map_t *map = THIS_DS_MAP(); - if (ZEND_NUM_ARGS() > 1) { - PARSE_LONG_AND_LONG(index, length); - RETURN_DS_MAP(ds_map_slice(map, index, length)); + PARSE_LONG_AND_OPTIONAL_ZVAL(index, length); + + if (ZEND_NUM_ARGS() > 1 && Z_TYPE_P(length) != IS_NULL) { + if (Z_TYPE_P(length) != IS_LONG) { + INTEGER_LENGTH_REQUIRED(length); + } else { + RETURN_DS_MAP(ds_map_slice(map, index, Z_LVAL_P(length))); + } } else { - PARSE_LONG(index); RETURN_DS_MAP(ds_map_slice(map, index, DS_MAP_SIZE(map))); } } diff --git a/src/php/classes/php_set_ce.c b/src/php/classes/php_set_ce.c index 7087a1f..4c13440 100644 --- a/src/php/classes/php_set_ce.c +++ b/src/php/classes/php_set_ce.c @@ -178,11 +178,15 @@ METHOD(slice) { ds_set_t *set = THIS_DS_SET(); - if (ZEND_NUM_ARGS() > 1) { - PARSE_LONG_AND_LONG(index, length); - RETURN_DS_SET(ds_set_slice(set, index, length)); + PARSE_LONG_AND_OPTIONAL_ZVAL(index, length); + + if (ZEND_NUM_ARGS() > 1 && Z_TYPE_P(length) != IS_NULL) { + if (Z_TYPE_P(length) != IS_LONG) { + INTEGER_LENGTH_REQUIRED(length); + } else { + RETURN_DS_SET(ds_set_slice(set, index, Z_LVAL_P(length))); + } } else { - PARSE_LONG(index); RETURN_DS_SET(ds_set_slice(set, index, DS_SET_SIZE(set))); } } diff --git a/src/php/classes/php_vector_ce.c b/src/php/classes/php_vector_ce.c index 1c394d0..8b7394d 100644 --- a/src/php/classes/php_vector_ce.c +++ b/src/php/classes/php_vector_ce.c @@ -204,11 +204,15 @@ METHOD(slice) { ds_vector_t *vector = THIS_DS_VECTOR(); - if (ZEND_NUM_ARGS() > 1) { - PARSE_LONG_AND_LONG(index, length); - RETURN_DS_VECTOR(ds_vector_slice(vector, index, length)); + PARSE_LONG_AND_OPTIONAL_ZVAL(index, length); + + if (ZEND_NUM_ARGS() > 1 && Z_TYPE_P(length) != IS_NULL) { + if (Z_TYPE_P(length) != IS_LONG) { + INTEGER_LENGTH_REQUIRED(length); + } else { + RETURN_DS_VECTOR(ds_vector_slice(vector, index, Z_LVAL_P(length))); + } } else { - PARSE_LONG(index); RETURN_DS_VECTOR(ds_vector_slice(vector, index, vector->size)); } } diff --git a/src/php/parameters.h b/src/php/parameters.h index a013e56..edbbf1e 100644 --- a/src/php/parameters.h +++ b/src/php/parameters.h @@ -44,6 +44,11 @@ zend_long l = 0; \ zval *z = NULL; \ PARSE_2("lz", &l, &z) +#define PARSE_LONG_AND_OPTIONAL_ZVAL(l, z) \ +zend_long l = 0; \ +zval *z = NULL; \ +PARSE_2("l|z", &l, &z) + #define PARSE_ZVAL_AND_LONG(z, l) \ zval *z = NULL; \ zend_long l = 0; \