Skip to content

Commit 3139c77

Browse files
committed
Fixed GH-5789
1 parent 9d0a1f6 commit 3139c77

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

ext-src/swoole_table.cc

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ END_EXTERN_C()
2424

2525
using namespace swoole;
2626

27-
static inline void php_swoole_table_row2array(Table *table, TableRow *row, zval *return_value) {
27+
static inline void php_swoole_table_row2array(const Table *table, TableRow *row, zval *return_value) {
2828
array_init(return_value);
2929

30-
for (auto i = table->column_list->begin(); i != table->column_list->end(); i++) {
31-
TableColumn *col = *i;
30+
for (const auto col : *table->column_list) {
3231
if (col->type == TableColumn::TYPE_STRING) {
3332
TableStringLength len = 0;
3433
char *str = nullptr;
@@ -48,9 +47,11 @@ static inline void php_swoole_table_row2array(Table *table, TableRow *row, zval
4847
}
4948
}
5049

51-
static inline void php_swoole_table_get_field_value(
52-
Table *table, TableRow *row, zval *return_value, char *field, uint16_t field_len) {
53-
TableColumn *col = table->get_column(std::string(field, field_len));
50+
static inline void php_swoole_table_get_field_value(Table *table,
51+
TableRow *row,
52+
zval *return_value,
53+
const zend_string *field) {
54+
TableColumn *col = table->get_column(std::string(ZSTR_VAL(field), ZSTR_LEN(field)));
5455
if (!col) {
5556
ZVAL_FALSE(return_value);
5657
return;
@@ -82,22 +83,22 @@ struct TableObject {
8283
};
8384

8485
static inline TableObject *php_swoole_table_fetch_object(zend_object *obj) {
85-
return (TableObject *) ((char *) obj - swoole_table_handlers.offset);
86+
return reinterpret_cast<TableObject *>(reinterpret_cast<char *>(obj) - swoole_table_handlers.offset);
8687
}
8788

88-
static inline Table *php_swoole_table_get_ptr(zval *zobject) {
89+
static inline Table *php_swoole_table_get_ptr(const zval *zobject) {
8990
return php_swoole_table_fetch_object(Z_OBJ_P(zobject))->ptr;
9091
}
9192

92-
static inline Table *php_swoole_table_get_and_check_ptr(zval *zobject) {
93+
static inline Table *php_swoole_table_get_and_check_ptr(const zval *zobject) {
9394
Table *table = php_swoole_table_get_ptr(zobject);
9495
if (UNEXPECTED(!table)) {
9596
swoole_fatal_error(SW_ERROR_WRONG_OPERATION, "must call constructor first");
9697
}
9798
return table;
9899
}
99100

100-
static inline Table *php_swoole_table_get_and_check_ptr2(zval *zobject) {
101+
static inline Table *php_swoole_table_get_and_check_ptr2(const zval *zobject) {
101102
Table *table = php_swoole_table_get_and_check_ptr(zobject);
102103
if (!table->ready()) {
103104
php_swoole_fatal_error(E_ERROR, "table is not created or has been destroyed");
@@ -114,7 +115,7 @@ static inline void php_swoole_table_free_object(zend_object *object) {
114115
}
115116

116117
static inline zend_object *php_swoole_table_create_object(zend_class_entry *ce) {
117-
TableObject *table = (TableObject *) zend_object_alloc(sizeof(TableObject), ce);
118+
auto *table = static_cast<TableObject *>(zend_object_alloc(sizeof(TableObject), ce));
118119
zend_object_std_init(&table->std, ce);
119120
object_properties_init(&table->std, ce);
120121
table->std.handlers = &swoole_table_handlers;
@@ -197,7 +198,7 @@ void php_swoole_table_minit(int module_number) {
197198
PHP_METHOD(swoole_table, __construct) {
198199
Table *table = php_swoole_table_get_ptr(ZEND_THIS);
199200
if (table) {
200-
zend_throw_error(NULL, "Constructor of %s can only be called once", SW_Z_OBJCE_NAME_VAL_P(ZEND_THIS));
201+
zend_throw_error(nullptr, "Constructor of %s can only be called once", SW_Z_OBJCE_NAME_VAL_P(ZEND_THIS));
201202
RETURN_FALSE;
202203
}
203204

@@ -242,7 +243,7 @@ PHP_METHOD(swoole_table, column) {
242243
php_swoole_fatal_error(E_WARNING, "unable to add column after table has been created");
243244
RETURN_FALSE;
244245
}
245-
RETURN_BOOL(table->add_column(std::string(name, len), (enum TableColumn::Type) type, size));
246+
RETURN_BOOL(table->add_column(std::string(name, len), static_cast<TableColumn::Type>(type), size));
246247
}
247248

248249
static PHP_METHOD(swoole_table, create) {
@@ -298,8 +299,7 @@ static PHP_METHOD(swoole_table, set) {
298299
HashTable *ht = Z_ARRVAL_P(array);
299300

300301
if (out_flags & SW_TABLE_FLAG_NEW_ROW) {
301-
for (auto i = table->column_list->begin(); i != table->column_list->end(); i++) {
302-
TableColumn *col = *i;
302+
for (const auto col : *table->column_list) {
303303
zval *zv = zend_hash_str_find(ht, col->name.c_str(), col->name.length());
304304
if (zv == nullptr || ZVAL_IS_NULL(zv)) {
305305
col->clear(row);
@@ -472,21 +472,20 @@ static PHP_METHOD(swoole_table, get) {
472472
Table *table = php_swoole_table_get_and_check_ptr2(ZEND_THIS);
473473
char *key;
474474
size_t keylen;
475-
char *field = nullptr;
476-
size_t field_len = 0;
475+
zend_string *field = nullptr;
477476

478477
ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 1, 2)
479478
Z_PARAM_STRING(key, keylen)
480479
Z_PARAM_OPTIONAL
481-
Z_PARAM_STRING(field, field_len)
480+
Z_PARAM_STR_OR_NULL(field)
482481
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
483482

484483
TableRow *_rowlock = nullptr;
485484
TableRow *row = table->get(key, keylen, &_rowlock);
486485
if (!row) {
487486
RETVAL_FALSE;
488-
} else if (field && field_len > 0) {
489-
php_swoole_table_get_field_value(table, row, return_value, field, (uint16_t) field_len);
487+
} else if (field) {
488+
php_swoole_table_get_field_value(table, row, return_value, field);
490489
} else {
491490
php_swoole_table_row2array(table, row, return_value);
492491
}

tests/swoole_table/gh-5789.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
swoole_table: bug_5789
3+
--SKIPIF--
4+
<?php require __DIR__ . '/../include/skipif.inc'; ?>
5+
--FILE--
6+
<?php
7+
require __DIR__ . '/../include/bootstrap.php';
8+
9+
use Swoole\Table;
10+
11+
$table = new Table(128, 1);
12+
$table->column('test', Table::TYPE_INT);
13+
$table->create();
14+
15+
$value = random_int(1, PHP_INT_MAX);
16+
$table->set('firstrow', ['test' => $value]);
17+
18+
Assert::eq($table->get('firstrow', 'test'), $value);
19+
Assert::eq($table->get('firstrow', null), ['test' => $value]);
20+
Assert::same($table->get('not-exists', null), false);
21+
Assert::same($table->get('not-exists', 'test'), false);
22+
?>
23+
--EXPECT--

0 commit comments

Comments
 (0)