Skip to content

Commit 8ebed91

Browse files
committed
Fix some parsed parameter types in SolrQuery
Some methods that should be excepting an int are forcing their argument as string. Methods: 'setStart', 'setRows' and 'setExpandRows', 'setTimeAllowed' and 'setGroupOffset'. Instead of forcing the argument to be correct (as an int), we keep BC by allowing strings, but we throw an exception if argument is neither.
1 parent 22bb79f commit 8ebed91

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

src/php7/php_solr_query.c

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,27 @@ PHP_METHOD(SolrQuery, setStart)
130130
COMPAT_ARG_SIZE_T param_name_len = sizeof("start")-1;
131131
solr_char_t *param_value = NULL;
132132
COMPAT_ARG_SIZE_T param_value_len = 0;
133+
zval *tmp;
133134

134-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &param_value, &param_value_len) == FAILURE) {
135+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &tmp) == FAILURE) {
135136

136137
php_error_docref(NULL, E_WARNING, "Invalid parameters");
137138

138139
RETURN_NULL();
139140
}
140141

142+
if (Z_TYPE_P(tmp) == IS_LONG) {
143+
convert_to_string(tmp);
144+
}
145+
146+
if (Z_TYPE_P(tmp) != IS_STRING) {
147+
solr_throw_exception(solr_ce_SolrIllegalArgumentException, "Argument 1 must be an int", SOLR_ERROR_4000, SOLR_FILE_LINE_FUNC);
148+
RETURN_NULL();
149+
}
150+
151+
param_value = Z_STRVAL_P(tmp);
152+
param_value_len = Z_STRLEN_P(tmp);
153+
141154
if (solr_set_normal_param(getThis(), param_name, param_name_len, param_value, param_value_len) == FAILURE)
142155
{
143156
RETURN_NULL();
@@ -155,14 +168,27 @@ PHP_METHOD(SolrQuery, setRows)
155168
COMPAT_ARG_SIZE_T param_name_len = sizeof("rows")-1;
156169
solr_char_t *param_value = NULL;
157170
COMPAT_ARG_SIZE_T param_value_len = 0;
171+
zval *tmp = NULL;
158172

159-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &param_value, &param_value_len) == FAILURE) {
173+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &tmp) == FAILURE) {
160174

161175
php_error_docref(NULL, E_WARNING, "Invalid parameters");
162176

163177
RETURN_NULL();
164178
}
165179

180+
if (Z_TYPE_P(tmp) == IS_LONG) {
181+
convert_to_string(tmp);
182+
}
183+
184+
if (Z_TYPE_P(tmp) != IS_STRING) {
185+
solr_throw_exception(solr_ce_SolrIllegalArgumentException, "Argument 1 must be an int", SOLR_ERROR_4000, SOLR_FILE_LINE_FUNC);
186+
RETURN_NULL();
187+
}
188+
189+
param_value = Z_STRVAL_P(tmp);
190+
param_value_len = Z_STRLEN_P(tmp);
191+
166192
if (solr_set_normal_param(getThis(), param_name, param_name_len, param_value, param_value_len) == FAILURE)
167193
{
168194
RETURN_NULL();
@@ -326,14 +352,27 @@ PHP_METHOD(SolrQuery, setTimeAllowed)
326352
COMPAT_ARG_SIZE_T param_name_len = sizeof("timeAllowed")-1;
327353
solr_char_t *param_value = NULL;
328354
COMPAT_ARG_SIZE_T param_value_len = 0;
355+
zval *tmp = NULL;
329356

330-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &param_value, &param_value_len) == FAILURE) {
357+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &tmp) == FAILURE) {
331358

332359
php_error_docref(NULL, E_WARNING, "Invalid parameters");
333360

334361
RETURN_NULL();
335362
}
336363

364+
if (Z_TYPE_P(tmp) == IS_LONG) {
365+
convert_to_string(tmp);
366+
}
367+
368+
if (Z_TYPE_P(tmp) != IS_STRING) {
369+
solr_throw_exception(solr_ce_SolrIllegalArgumentException, "Argument 1 must be an int", SOLR_ERROR_4000, SOLR_FILE_LINE_FUNC);
370+
RETURN_NULL();
371+
}
372+
373+
param_value = Z_STRVAL_P(tmp);
374+
param_value_len = Z_STRLEN_P(tmp);
375+
337376
if (solr_set_normal_param(getThis(), param_name, param_name_len, param_value, param_value_len) == FAILURE)
338377
{
339378
php_error_docref(NULL, E_WARNING, "Error setting parameter %s=%s ", param_name, param_value);
@@ -1343,14 +1382,27 @@ PHP_METHOD(SolrQuery, setGroupOffset)
13431382
int param_name_len = sizeof("group.offset")-1;
13441383
solr_char_t *param_value = NULL;
13451384
COMPAT_ARG_SIZE_T param_value_len = 0;
1385+
zval *tmp = NULL;
13461386

1347-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &param_value, &param_value_len) == FAILURE) {
1387+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &tmp) == FAILURE) {
13481388

13491389
php_error_docref(NULL, E_WARNING, "Invalid parameters");
13501390

13511391
RETURN_NULL();
13521392
}
13531393

1394+
if (Z_TYPE_P(tmp) == IS_LONG) {
1395+
convert_to_string(tmp);
1396+
}
1397+
1398+
if (Z_TYPE_P(tmp) != IS_STRING) {
1399+
solr_throw_exception(solr_ce_SolrIllegalArgumentException, "Argument 1 must be an int", SOLR_ERROR_4000, SOLR_FILE_LINE_FUNC);
1400+
RETURN_NULL();
1401+
}
1402+
1403+
param_value = Z_STRVAL_P(tmp);
1404+
param_value_len = Z_STRLEN_P(tmp);
1405+
13541406
if (solr_add_normal_param(getThis(), param_name, param_name_len, param_value, param_value_len) == FAILURE)
13551407
{
13561408
php_error_docref(NULL, E_WARNING, "Unable to add param value %s to %s ", param_value, param_name);
@@ -1917,14 +1969,27 @@ PHP_METHOD(SolrQuery, setExpandRows)
19171969
COMPAT_ARG_SIZE_T param_name_len = sizeof("expand.rows")-1;
19181970
solr_char_t *param_value = NULL;
19191971
COMPAT_ARG_SIZE_T param_value_len = 0;
1972+
zval *tmp = NULL;
19201973

1921-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &param_value, &param_value_len) == FAILURE) {
1974+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &tmp) == FAILURE) {
19221975

19231976
php_error_docref(NULL, E_WARNING, "Invalid parameters");
19241977

19251978
RETURN_NULL();
19261979
}
19271980

1981+
if (Z_TYPE_P(tmp) == IS_LONG) {
1982+
convert_to_string(tmp);
1983+
}
1984+
1985+
if (Z_TYPE_P(tmp) != IS_STRING) {
1986+
solr_throw_exception(solr_ce_SolrIllegalArgumentException, "Argument 1 must be an int", SOLR_ERROR_4000, SOLR_FILE_LINE_FUNC);
1987+
RETURN_NULL();
1988+
}
1989+
1990+
param_value = Z_STRVAL_P(tmp);
1991+
param_value_len = Z_STRLEN_P(tmp);
1992+
19281993
if (solr_set_normal_param(getThis(), param_name, param_name_len, param_value, param_value_len) == FAILURE)
19291994
{
19301995
RETURN_NULL();

tests/203.solrquery_strict_types.phpt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ $query->setTimeAllowed('300');
2121
$query->setGroupOffset('1');
2222
$query->setExpandRows('1');
2323

24-
echo $query;
24+
echo $query . "\n";
25+
26+
try { $query->setStart(true); } catch (SolrIllegalArgumentException $e) { echo $e->getMessage(); }
2527
--EXPECT--
2628
q=lucene&start=1&rows=2&timeAllowed=300&group.offset=1&expand.rows=1
2729
q=lucene&start=1anystring&rows=2&timeAllowed=300&group.offset=1&expand.rows=1
30+
Argument 1 must be an int
2831

0 commit comments

Comments
 (0)