Skip to content

Commit

Permalink
Merge pull request #197 from HypeMC/fix-slice-with-null
Browse files Browse the repository at this point in the history
Allow passing `null` as `$length` to `slice()` methods
  • Loading branch information
rtheunissen authored Jul 29, 2023
2 parents 23ddc08 + 468a68c commit f111b23
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 16 deletions.
4 changes: 4 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
12 changes: 8 additions & 4 deletions src/php/classes/php_deque_ce.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/php/classes/php_map_ce.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/php/classes/php_set_ce.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/php/classes/php_vector_ce.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/php/parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; \
Expand Down

0 comments on commit f111b23

Please sign in to comment.