Skip to content

Commit 693a223

Browse files
committed
Fixing tests
1 parent 0c1fea3 commit 693a223

File tree

3 files changed

+81
-68
lines changed

3 files changed

+81
-68
lines changed

msgpack.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,13 @@ PHP_MSGPACK_API void php_msgpack_serialize(smart_str *buf, zval *val) /* {{{ */
198198
}
199199
/* }}} */
200200

201-
PHP_MSGPACK_API void php_msgpack_unserialize( zval *return_value, char *str, size_t str_len) /* {{{ */ {
201+
PHP_MSGPACK_API void php_msgpack_unserialize(zval *return_value, char *str, size_t str_len) /* {{{ */ {
202202
int ret;
203203
size_t off = 0;
204204
msgpack_unpack_t mp;
205205
msgpack_unserialize_data_t var_hash;
206206

207-
if (str_len <= 0)
208-
{
207+
if (str_len <= 0) {
209208
RETURN_NULL();
210209
}
211210

msgpack_convert.c

Lines changed: 50 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -126,61 +126,53 @@ int msgpack_convert_array(zval *return_value, zval *tpl, zval *value) /* {{{ */
126126
}
127127

128128
/* string */
129-
if (ht->nNumOfElements != ht->nNextFreeElement) {
130-
htval = HASH_OF(value);
131-
if (!htval) {
132-
MSGPACK_WARNING("[msgpack] (%s) input data is not array", __FUNCTION__);
133-
return FAILURE;
134-
}
135-
136-
zend_hash_internal_pointer_reset_ex(ht, &pos);
137-
zend_hash_internal_pointer_reset_ex(htval, &valpos);
138-
for (;; zend_hash_move_forward_ex(ht, &pos), zend_hash_move_forward_ex(htval, &valpos))
139-
{
140-
key_type = zend_hash_get_current_key_ex(ht, &key, &key_index, &pos);
141-
142-
if (key_type == HASH_KEY_NON_EXISTENT) {
143-
break;
144-
}
145-
146-
if ((data = zend_hash_get_current_data_ex(ht, &pos)) == NULL) {
147-
continue;
148-
}
149-
150-
if (key_type == HASH_KEY_IS_STRING) {
151-
int (*convert_function)(zval *, zval *, zval *) = NULL;
152-
zval *dataval;
153-
154-
switch (Z_TYPE_P(data)) {
155-
case IS_ARRAY:
156-
convert_function = msgpack_convert_array;
157-
break;
158-
case IS_OBJECT:
159-
// case IS_STRING:
160-
convert_function = msgpack_convert_object;
161-
break;
162-
default:
163-
break;
164-
}
129+
if (ht->nNumOfElements != ht->nNextFreeElement) {
130+
htval = HASH_OF(value);
131+
132+
if (!htval) {
133+
MSGPACK_WARNING("[msgpack] (%s) input data is not array", __FUNCTION__);
134+
return FAILURE;
135+
}
136+
137+
zend_hash_internal_pointer_reset_ex(htval, &valpos);
138+
ZEND_HASH_FOREACH_KEY_VAL(ht, key_index, key, data) {
139+
if (key) {
140+
int (*convert_function)(zval *, zval *, zval *) = NULL;
141+
zval *dataval;
142+
143+
switch (Z_TYPE_P(data)) {
144+
case IS_ARRAY:
145+
convert_function = msgpack_convert_array;
146+
break;
147+
case IS_OBJECT:
148+
// case IS_STRING:
149+
convert_function = msgpack_convert_object;
150+
break;
151+
default:
152+
break;
153+
}
165154

166-
if ((dataval = zend_hash_get_current_data_ex(htval, &valpos)) == NULL) {
167-
MSGPACK_WARNING("[msgpack] (%s) can't get data", __FUNCTION__);
168-
return FAILURE;
169-
}
155+
if ((dataval = zend_hash_get_current_data_ex(htval, &valpos)) == NULL) {
156+
MSGPACK_WARNING("[msgpack] (%s) can't get data", __FUNCTION__);
157+
return FAILURE;
158+
}
170159

171-
if (convert_function) {
172-
zval rv;
173-
if (convert_function(&rv, data, dataval) != SUCCESS) {
174-
return FAILURE;
175-
}
176-
add_assoc_zval_ex(return_value, key->val, key->len, dataval);
177-
} else {
178-
add_assoc_zval_ex(return_value, key->val, key->len, dataval);
179-
}
180-
}
181-
}
160+
if (convert_function) {
161+
zval rv;
162+
if (convert_function(&rv, data, dataval) != SUCCESS) {
163+
return FAILURE;
164+
}
165+
Z_TRY_ADDREF_P(dataval);
166+
zend_symtable_update(Z_ARRVAL_P(return_value), key, dataval);
167+
} else {
168+
Z_TRY_ADDREF_P(dataval);
169+
zend_symtable_update(Z_ARRVAL_P(return_value), key, dataval);
170+
}
171+
}
172+
zend_hash_move_forward_ex(htval, &valpos);
173+
} ZEND_HASH_FOREACH_END();
182174

183-
return SUCCESS;
175+
return SUCCESS;
184176
} else {
185177
/* index */
186178
int (*convert_function)(zval *, zval *, zval *) = NULL;
@@ -218,7 +210,7 @@ int msgpack_convert_array(zval *return_value, zval *tpl, zval *value) /* {{{ */
218210
}
219211

220212
htval = HASH_OF(value);
221-
if (zend_hash_num_elements(htval) <= 0) {
213+
if (zend_hash_num_elements(htval) == 0) {
222214
MSGPACK_WARNING("[msgpack] (%s) array length is 0 in unserialized data", __FUNCTION__);
223215
return FAILURE;
224216
}
@@ -237,8 +229,7 @@ int msgpack_convert_array(zval *return_value, zval *tpl, zval *value) /* {{{ */
237229
}
238230

239231
switch (key_type) {
240-
case HASH_KEY_IS_LONG:
241-
{
232+
case HASH_KEY_IS_LONG: {
242233
zval rv;
243234
if (convert_function) {
244235
if (convert_function(&rv, data, arydata) != SUCCESS) {
@@ -249,8 +240,9 @@ int msgpack_convert_array(zval *return_value, zval *tpl, zval *value) /* {{{ */
249240
__FUNCTION__);
250241
return FAILURE;
251242
}
252-
add_next_index_zval(return_value, arydata);
243+
add_next_index_zval(return_value, &rv);
253244
} else {
245+
Z_TRY_ADDREF_P(arydata);
254246
add_next_index_zval(return_value, arydata);
255247
}
256248
break;
@@ -263,7 +255,6 @@ int msgpack_convert_array(zval *return_value, zval *tpl, zval *value) /* {{{ */
263255
return FAILURE;
264256
}
265257
}
266-
267258
return SUCCESS;
268259
}
269260

@@ -423,14 +414,14 @@ int msgpack_convert_object(zval *return_value, zval *tpl, zval *value) /* {{{ */
423414
if (convert_function) {
424415
zval nv;
425416
if (convert_function(&nv, data, aryval) != SUCCESS) {
426-
//zval_ptr_dtor(aryval);
427417
MSGPACK_WARNING("[msgpack] (%s) "
428418
"convert failure in convert_object",
429419
__FUNCTION__);
430420
return FAILURE;
431421
}
432422

433-
zend_update_property(ce, return_value, str_key->val, str_key->len, &nv);
423+
zend_update_property_ex(ce, return_value, str_key, &nv);
424+
zval_ptr_dtor(&nv);
434425
} else {
435426
zend_update_property(ce, return_value, prop_name, prop_len, aryval);
436427
}
@@ -462,19 +453,14 @@ int msgpack_convert_template(zval *return_value, zval *tpl, zval *value) /* {{{
462453
switch (Z_TYPE_P(tpl)) {
463454
case IS_ARRAY:
464455
return msgpack_convert_array(return_value, tpl, value);
465-
break;
466456
case IS_STRING:
467457
case IS_OBJECT:
468458
return msgpack_convert_object(return_value, tpl, value);
469-
break;
470459
default:
471460
MSGPACK_ERROR("[msgpack] (%s) Template type is unsupported",
472461
__FUNCTION__);
473462
return FAILURE;
474463
}
475-
476-
// shouldn't reach
477-
return FAILURE;
478464
}
479465
/* }}} */
480466

msgpack_unpack.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ typedef struct {
1616
} var_entries;
1717

1818
#define MSGPACK_UNSERIALIZE_FINISH_ITEM(_unpack, _count) \
19+
/* msgpack_stack_pop(_unpack->var_hash, _count); */ \
1920
_unpack->stack[_unpack->deps-1]--; \
2021
if (_unpack->stack[_unpack->deps-1] == 0) { \
2122
_unpack->deps--; \
@@ -106,6 +107,31 @@ static zval *msgpack_stack_push(msgpack_unserialize_data_t *var_hashx) /* {{{ */
106107
}
107108
/* }}} */
108109

110+
static inline void msgpack_stack_pop(msgpack_unserialize_data_t *var_hashx, uint32_t count) /* {{{ */ {
111+
uint32_t i;
112+
var_entries *var_hash = var_hashx->first_dtor;
113+
114+
while (var_hash && var_hash->used_slots == VAR_ENTRIES_MAX) {
115+
var_hash = var_hash->next;
116+
}
117+
118+
if (!var_hash || count <= 0) {
119+
return;
120+
}
121+
122+
for (i = count; i > 0; i--) {
123+
var_hash->used_slots--;
124+
if (var_hash->used_slots < 0) {
125+
var_hash->used_slots = 0;
126+
ZVAL_UNDEF(&var_hash->data[var_hash->used_slots]);
127+
break;
128+
} else {
129+
ZVAL_UNDEF(&var_hash->data[var_hash->used_slots]);
130+
}
131+
}
132+
}
133+
/* }}} */
134+
109135
static zend_class_entry* msgpack_unserialize_class(zval **container, zend_string *class_name, zend_bool init_class) /* {{{ */ {
110136
zend_class_entry *ce;
111137
int func_call_status;
@@ -455,7 +481,7 @@ int msgpack_unserialize_map_item(msgpack_unserialize_data *unpack, zval **contai
455481
return 0;
456482
}
457483

458-
ce->unserialize(*container, ce, Z_STRVAL_P(val), Z_STRLEN_P(val) + 1, NULL);
484+
ce->unserialize(*container, ce, (const unsigned char *)Z_STRVAL_P(val), Z_STRLEN_P(val) + 1, NULL);
459485

460486
MSGPACK_UNSERIALIZE_FINISH_MAP_ITEM(unpack, key, val);
461487

@@ -556,6 +582,8 @@ int msgpack_unserialize_map_item(msgpack_unserialize_data *unpack, zval **contai
556582
}
557583
}
558584

585+
/* msgpack_stack_pop(unpack->var_hash, 2); */
586+
559587
deps = unpack->deps - 1;
560588
unpack->stack[deps]--;
561589
if (unpack->stack[deps] == 0) {

0 commit comments

Comments
 (0)