Skip to content

Commit 0a206c4

Browse files
committed
Allow null as offset in select() with strict types
When PHP strict types mode is enabled (`declare(strict_types=1);`) the `offset` parameter passed as `null` to select() method does not pass validation. It looks quite natural to accept `null` here, because when the parameter is omitted the default value (zero) is assumed. A user may want to set the next parameter (`iterator`) and left `offset` to be default, so (s)he'll use `null`. The similar change was made for the `limit` in the scope of #58. After this commit `offset` and `limit` parameters behave in the same way. The functionality is already covered by the test suite (DMLTest::test_14_select_limit_defaults()), but the problem was not catched until now, because strict types mode is not enabled in tests. It'll be enabled in a following commit and the problem will be actually tested. Fixes #154.
1 parent ff8dfa4 commit 0a206c4

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/tarantool.c

+13-3
Original file line numberDiff line numberDiff line change
@@ -1308,12 +1308,12 @@ PHP_METHOD(Tarantool, ping) {
13081308

13091309
PHP_METHOD(Tarantool, select) {
13101310
zval *space = NULL, *index = NULL, *key = NULL;
1311-
zval *zlimit = NULL, *ziterator = NULL;
1311+
zval *zlimit = NULL, *zoffset = NULL, *ziterator = NULL;
13121312
long limit = LONG_MAX - 1, offset = 0, iterator = 0;
13131313
zval key_new = {0};
13141314

1315-
TARANTOOL_FUNCTION_BEGIN(obj, id, "z|zzzlz", &space, &key, &index,
1316-
&zlimit, &offset, &ziterator);
1315+
TARANTOOL_FUNCTION_BEGIN(obj, id, "z|zzzzz", &space, &key, &index,
1316+
&zlimit, &zoffset, &ziterator);
13171317
TARANTOOL_CONNECT_ON_DEMAND(obj);
13181318

13191319
if (zlimit != NULL &&
@@ -1326,6 +1326,16 @@ PHP_METHOD(Tarantool, select) {
13261326
limit = Z_LVAL_P(zlimit);
13271327
}
13281328

1329+
if (zoffset != NULL &&
1330+
Z_TYPE_P(zoffset) != IS_NULL &&
1331+
Z_TYPE_P(zoffset) != IS_LONG) {
1332+
THROW_EXC("wrong type of 'offset' - expected long/null, "
1333+
"got '%s'", zend_zval_type_name(zoffset));
1334+
RETURN_FALSE;
1335+
} else if (zoffset != NULL && Z_TYPE_P(zoffset) == IS_LONG) {
1336+
offset = Z_LVAL_P(zoffset);
1337+
}
1338+
13291339
/* Obtain space number */
13301340
long space_no = get_spaceno_by_name(obj, space);
13311341
if (space_no == FAILURE) RETURN_FALSE;

0 commit comments

Comments
 (0)