From 90cc710351d4c797d99d3090b80f6f01aef547e2 Mon Sep 17 00:00:00 2001 From: Andrei Zmievski Date: Tue, 14 Aug 2001 20:33:19 +0000 Subject: [PATCH] Be more safe about freeing zvals. --- ext/wddx/wddx.c | 151 ++++++++++++++++++++++++------------------------ 1 file changed, 74 insertions(+), 77 deletions(-) diff --git a/ext/wddx/wddx.c b/ext/wddx/wddx.c index 36328879ee3..66d76a927b0 100644 --- a/ext/wddx/wddx.c +++ b/ext/wddx/wddx.c @@ -86,7 +86,7 @@ typedef struct { static void php_wddx_process_data(void *user_data, const char *s, int len); -/* {{{ wddx_functions[] +/* wddx_functions[] */ function_entry wddx_functions[] = { PHP_FE(wddx_serialize_value, NULL) @@ -97,12 +97,12 @@ function_entry wddx_functions[] = { PHP_FE(wddx_deserialize, NULL) {NULL, NULL, NULL} }; -/* }}} */ +/* */ PHP_MINIT_FUNCTION(wddx); PHP_MINFO_FUNCTION(wddx); -/* {{{ wddx_module_entry +/* wddx_module_entry */ zend_module_entry wddx_module_entry = { "wddx", @@ -114,9 +114,9 @@ zend_module_entry wddx_module_entry = { PHP_MINFO(wddx), STANDARD_MODULE_PROPERTIES }; -/* }}} */ +/* */ -/* {{{ wddx_stack_init +/* wddx_stack_init */ static int wddx_stack_init(wddx_stack *stack) { @@ -130,9 +130,9 @@ static int wddx_stack_init(wddx_stack *stack) return SUCCESS; } } -/* }}} */ +/* */ -/* {{{ wddx_stack_push +/* wddx_stack_push */ static int wddx_stack_push(wddx_stack *stack, void *element, int size) { @@ -147,9 +147,9 @@ static int wddx_stack_push(wddx_stack *stack, void *element, int size) memcpy(stack->elements[stack->top], element, size); return stack->top++; } -/* }}} */ +/* */ -/* {{{ wddx_stack_top +/* wddx_stack_top */ static int wddx_stack_top(wddx_stack *stack, void **element) { @@ -161,9 +161,9 @@ static int wddx_stack_top(wddx_stack *stack, void **element) return FAILURE; } } -/* }}} */ +/* */ -/* {{{ wddx_stack_is_empty +/* wddx_stack_is_empty */ static int wddx_stack_is_empty(wddx_stack *stack) { @@ -173,9 +173,9 @@ static int wddx_stack_is_empty(wddx_stack *stack) return 0; } } -/* }}} */ +/* */ -/* {{{ wddx_stack_destroy +/* wddx_stack_destroy */ static int wddx_stack_destroy(wddx_stack *stack) { @@ -185,8 +185,7 @@ static int wddx_stack_destroy(wddx_stack *stack) for (i = 0; i < stack->top; i++) { if (((st_entry *)stack->elements[i])->data) { - zval_dtor(((st_entry *)stack->elements[i])->data); - efree(((st_entry *)stack->elements[i])->data); + zval_ptr_dtor(&((st_entry *)stack->elements[i])->data); } if (((st_entry *)stack->elements[i])->varname) efree(((st_entry *)stack->elements[i])->varname); @@ -196,9 +195,9 @@ static int wddx_stack_destroy(wddx_stack *stack) } return SUCCESS; } -/* }}} */ +/* */ -/* {{{ release_wddx_packet_rsrc +/* release_wddx_packet_rsrc */ static void release_wddx_packet_rsrc(zend_rsrc_list_entry *rsrc TSRMLS_DC) { @@ -206,11 +205,11 @@ static void release_wddx_packet_rsrc(zend_rsrc_list_entry *rsrc TSRMLS_DC) smart_str_free(str); efree(str); } -/* }}} */ +/* */ #include "ext/session/php_session.h" -/* {{{ PS_SERIALIZER_ENCODE_FUNC +/* PS_SERIALIZER_ENCODE_FUNC */ PS_SERIALIZER_ENCODE_FUNC(wddx) { @@ -238,9 +237,9 @@ PS_SERIALIZER_ENCODE_FUNC(wddx) return SUCCESS; } -/* }}} */ +/* */ -/* {{{ PS_SERIALIZER_DECODE_FUNC +/* PS_SERIALIZER_DECODE_FUNC */ PS_SERIALIZER_DECODE_FUNC(wddx) { @@ -281,9 +280,9 @@ PS_SERIALIZER_DECODE_FUNC(wddx) return ret; } -/* }}} */ +/* */ -/* {{{ PHP_MINIT_FUNCTION +/* PHP_MINIT_FUNCTION */ PHP_MINIT_FUNCTION(wddx) { @@ -294,9 +293,9 @@ PHP_MINIT_FUNCTION(wddx) return SUCCESS; } -/* }}} */ +/* */ -/* {{{ PHP_MINFO_FUNCTION +/* PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION(wddx) { @@ -304,9 +303,9 @@ PHP_MINFO_FUNCTION(wddx) php_info_print_table_row(2, "WDDX Support", "enabled" ); php_info_print_table_end(); } -/* }}} */ +/* */ -/* {{{ php_wddx_packet_start +/* php_wddx_packet_start */ void php_wddx_packet_start(wddx_packet *packet, char *comment, int comment_len) { @@ -321,16 +320,16 @@ void php_wddx_packet_start(wddx_packet *packet, char *comment, int comment_len) php_wddx_add_chunk_static(packet, WDDX_HEADER); php_wddx_add_chunk_static(packet, WDDX_DATA_S); } -/* }}} */ +/* */ -/* {{{ php_wddx_packet_end +/* php_wddx_packet_end */ void php_wddx_packet_end(wddx_packet *packet) { php_wddx_add_chunk_static(packet, WDDX_DATA_E); php_wddx_add_chunk_static(packet, WDDX_PACKET_E); } -/* }}} */ +/* */ #define FLUSH_BUF() \ if (l > 0) { \ @@ -338,7 +337,7 @@ void php_wddx_packet_end(wddx_packet *packet) l = 0; \ } -/* {{{ php_wddx_serialize_string +/* php_wddx_serialize_string */ static void php_wddx_serialize_string(wddx_packet *packet, zval *var) { @@ -389,9 +388,9 @@ static void php_wddx_serialize_string(wddx_packet *packet, zval *var) php_wddx_add_chunk_static(packet, WDDX_STRING_E); } -/* }}} */ +/* */ -/* {{{ php_wddx_serialize_number +/* php_wddx_serialize_number */ static void php_wddx_serialize_number(wddx_packet *packet, zval *var) { @@ -400,15 +399,16 @@ static void php_wddx_serialize_number(wddx_packet *packet, zval *var) tmp = *var; zval_copy_ctor(&tmp); + INIT_PZVAL(&tmp); convert_to_string(&tmp); sprintf(tmp_buf, WDDX_NUMBER, Z_STRVAL(tmp)); zval_dtor(&tmp); php_wddx_add_chunk(packet, tmp_buf); } -/* }}} */ +/* */ -/* {{{ php_wddx_serialize_boolean +/* php_wddx_serialize_boolean */ static void php_wddx_serialize_boolean(wddx_packet *packet, zval *var) { @@ -417,17 +417,17 @@ static void php_wddx_serialize_boolean(wddx_packet *packet, zval *var) sprintf(tmp_buf, WDDX_BOOLEAN, Z_LVAL_P(var) ? "true" : "false"); php_wddx_add_chunk(packet, tmp_buf); } -/* }}} */ +/* */ -/* {{{ php_wddx_serialize_unset +/* php_wddx_serialize_unset */ static void php_wddx_serialize_unset(wddx_packet *packet) { php_wddx_add_chunk_static(packet, WDDX_NULL); } -/* }}} */ +/* */ -/* {{{ php_wddx_serialize_object +/* php_wddx_serialize_object */ static void php_wddx_serialize_object(wddx_packet *packet, zval *obj) { @@ -446,7 +446,7 @@ static void php_wddx_serialize_object(wddx_packet *packet, zval *obj) * We try to call __sleep() method on object. It's supposed to return an * array of property names to be serialized. */ - if (call_user_function_ex(CG(function_table), &obj, fname, &retval, 0, 0, 1, NULL TSRMLS_CC) == SUCCESS) { + if (call_user_function_ex(EG(function_table), &obj, fname, &retval, 0, 0, 1, NULL TSRMLS_CC) == SUCCESS) { if (retval && HASH_OF(retval)) { PHP_CLASS_ATTRIBUTES; @@ -517,9 +517,9 @@ static void php_wddx_serialize_object(wddx_packet *packet, zval *obj) zval_ptr_dtor(&retval); } } -/* }}} */ +/* */ -/* {{{ php_wddx_serialize_array +/* php_wddx_serialize_array */ static void php_wddx_serialize_array(wddx_packet *packet, zval *arr) { @@ -586,9 +586,9 @@ static void php_wddx_serialize_array(wddx_packet *packet, zval *arr) php_wddx_add_chunk_static(packet, WDDX_ARRAY_E); } } -/* }}} */ +/* */ -/* {{{ php_wddx_serialize_var +/* php_wddx_serialize_var */ void php_wddx_serialize_var(wddx_packet *packet, zval *var, char *name, int name_len) { @@ -634,9 +634,9 @@ void php_wddx_serialize_var(wddx_packet *packet, zval *var, char *name, int name php_wddx_add_chunk_static(packet, WDDX_VAR_E); } } -/* }}} */ +/* */ -/* {{{ php_wddx_add_var +/* php_wddx_add_var */ static void php_wddx_add_var(wddx_packet *packet, zval *name_var) { @@ -664,9 +664,9 @@ static void php_wddx_add_var(wddx_packet *packet, zval *name_var) } } } -/* }}} */ +/* */ -/* {{{ php_wddx_push_element +/* php_wddx_push_element */ static void php_wddx_push_element(void *user_data, const char *name, const char **atts) { @@ -772,9 +772,9 @@ static void php_wddx_push_element(void *user_data, const char *name, const char } } } -/* }}} */ +/* */ -/* {{{ php_wddx_pop_element +/* php_wddx_pop_element */ static void php_wddx_pop_element(void *user_data, const char *name) { @@ -855,15 +855,13 @@ static void php_wddx_pop_element(void *user_data, const char *name) } /* Clean up old array entry */ - zval_dtor(ent2->data); - efree(ent2->data); + zval_ptr_dtor(&ent2->data); /* Set stack entry to point to the newly created object */ ent2->data = obj; /* Clean up class name var entry */ - zval_dtor(ent1->data); - efree(ent1->data); + zval_ptr_dtor(&ent1->data); } else zend_hash_update(target_hash, @@ -882,9 +880,9 @@ static void php_wddx_pop_element(void *user_data, const char *name) efree(stack->varname); } } -/* }}} */ +/* */ -/* {{{ php_wddx_process_data +/* php_wddx_process_data */ static void php_wddx_process_data(void *user_data, const char *s, int len) { @@ -939,8 +937,7 @@ static void php_wddx_process_data(void *user_data, const char *s, int len) Z_LVAL_P(ent->data) = 0; else { stack->top--; - zval_dtor(ent->data); - efree(ent->data); + zval_ptr_dtor(&ent->data); if (ent->varname) efree(ent->varname); efree(ent); @@ -952,9 +949,9 @@ static void php_wddx_process_data(void *user_data, const char *s, int len) } } } -/* }}} */ +/* */ -/* {{{ php_wddx_deserialize_ex +/* php_wddx_deserialize_ex */ int php_wddx_deserialize_ex(char *value, int vallen, zval *return_value) { @@ -986,9 +983,9 @@ int php_wddx_deserialize_ex(char *value, int vallen, zval *return_value) return retval; } -/* }}} */ +/* */ -/* {{{ proto string wddx_serialize_value(mixed var [, string comment]) +/* proto string wddx_serialize_value(mixed var [, string comment]) Creates a new packet and serializes the given value */ PHP_FUNCTION(wddx_serialize_value) { @@ -1014,9 +1011,9 @@ PHP_FUNCTION(wddx_serialize_value) smart_str_free(packet); efree(packet); } -/* }}} */ +/* */ -/* {{{ proto string wddx_serialize_vars(mixed var_name [, mixed ...]) +/* proto string wddx_serialize_vars(mixed var_name [, mixed ...]) Creates a new packet and serializes given variables into a struct */ PHP_FUNCTION(wddx_serialize_vars) { @@ -1061,9 +1058,9 @@ PHP_FUNCTION(wddx_serialize_vars) smart_str_free(packet); efree(packet); } -/* }}} */ +/* */ -/* {{{ php_wddx_constructor +/* php_wddx_constructor */ wddx_packet *php_wddx_constructor(void) { @@ -1074,18 +1071,18 @@ wddx_packet *php_wddx_constructor(void) return packet; } -/* }}} */ +/* */ -/* {{{ php_wddx_destructor +/* php_wddx_destructor */ void php_wddx_destructor(wddx_packet *packet) { smart_str_free(packet); efree(packet); } -/* }}} */ +/* */ -/* {{{ proto int wddx_packet_start([string comment]) +/* proto int wddx_packet_start([string comment]) Starts a WDDX packet with optional comment and returns the packet id */ PHP_FUNCTION(wddx_packet_start) { @@ -1108,9 +1105,9 @@ PHP_FUNCTION(wddx_packet_start) ZEND_REGISTER_RESOURCE(return_value, packet, le_wddx); } -/* }}} */ +/* */ -/* {{{ proto string wddx_packet_end(int packet_id) +/* proto string wddx_packet_end(int packet_id) Ends specified WDDX packet and returns the string containing the packet */ PHP_FUNCTION(wddx_packet_end) { @@ -1130,9 +1127,9 @@ PHP_FUNCTION(wddx_packet_end) zend_list_delete(Z_LVAL_P(packet_id)); } -/* }}} */ +/* */ -/* {{{ proto int wddx_add_vars(int packet_id, mixed var_names [, mixed ...]) +/* proto int wddx_add_vars(int packet_id, mixed var_names [, mixed ...]) Serializes given variables and adds them to packet given by packet_id */ PHP_FUNCTION(wddx_add_vars) { @@ -1173,9 +1170,9 @@ PHP_FUNCTION(wddx_add_vars) efree(args); RETURN_TRUE; } -/* }}} */ +/* */ -/* {{{ proto mixed wddx_deserialize(string packet) +/* proto mixed wddx_deserialize(string packet) Deserializes given packet and returns a PHP value */ PHP_FUNCTION(wddx_deserialize) { @@ -1190,7 +1187,7 @@ PHP_FUNCTION(wddx_deserialize) php_wddx_deserialize_ex(packet, packet_len, return_value); } -/* }}} */ +/* */ #endif /* HAVE_LIBEXPAT */