Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c9df77d

Browse files
committed
ra_call_distributor returns position or -1 in case of error
1 parent 6c2c6fa commit c9df77d

1 file changed

Lines changed: 10 additions & 16 deletions

File tree

redis_array_impl.c

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,10 @@ ra_extract_key(RedisArray *ra, const char *key, int key_len, int *out_len TSRMLS
431431
}
432432

433433
/* call userland key distributor function */
434-
zend_bool
435-
ra_call_distributor(RedisArray *ra, const char *key, int key_len, int *pos TSRMLS_DC)
434+
int
435+
ra_call_distributor(RedisArray *ra, const char *key, int key_len TSRMLS_DC)
436436
{
437-
zend_bool ret = 0;
437+
int ret;
438438
zval z_ret, z_argv;
439439

440440
/* check that we can call the extractor function */
@@ -444,18 +444,15 @@ ra_call_distributor(RedisArray *ra, const char *key, int key_len, int *pos TSRML
444444
if (!zend_is_callable_ex(&ra->z_dist, NULL, 0, NULL, NULL, NULL)) {
445445
#endif
446446
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not call distributor function");
447-
return 0;
447+
return -1;
448448
}
449449

450450
ZVAL_NULL(&z_ret);
451451
/* call extraction function */
452452
ZVAL_STRINGL(&z_argv, key, key_len);
453453
call_user_function(EG(function_table), NULL, &ra->z_dist, &z_ret, 1, &z_argv);
454454

455-
if (Z_TYPE(z_ret) == IS_LONG) {
456-
*pos = Z_LVAL(z_ret);
457-
ret = 1;
458-
}
455+
ret = (Z_TYPE(z_ret) == IS_LONG) ? Z_LVAL(z_ret) : -1;
459456

460457
zval_dtor(&z_argv);
461458
zval_dtor(&z_ret);
@@ -473,13 +470,12 @@ ra_find_node(RedisArray *ra, const char *key, int key_len, int *out_pos TSRMLS_D
473470
if(!out) return NULL;
474471

475472
if (Z_TYPE(ra->z_dist) != IS_NULL) {
476-
if (!ra_call_distributor(ra, key, key_len, &pos TSRMLS_CC)) {
473+
pos = ra_call_distributor(ra, key, key_len TSRMLS_CC);
474+
if (pos < 0 || pos >= ra->count) {
477475
efree(out);
478476
return NULL;
479477
}
480-
efree(out);
481478
} else {
482-
uint64_t h64;
483479
unsigned long ret = 0xffffffff;
484480
size_t i;
485481

@@ -488,14 +484,12 @@ ra_find_node(RedisArray *ra, const char *key, int key_len, int *out_pos TSRMLS_D
488484
CRC32(ret, (unsigned char)out[i]);
489485
}
490486
hash = (ret ^ 0xffffffff);
491-
efree(out);
492487

493488
/* get position on ring */
494-
h64 = hash;
495-
h64 *= ra->count;
496-
h64 /= 0xffffffff;
497-
pos = (int)h64;
489+
pos = (int)(hash * ra->count / 0xffffffff);
498490
}
491+
efree(out);
492+
499493
if(out_pos) *out_pos = pos;
500494

501495
return &ra->redis[pos];

0 commit comments

Comments
 (0)