fix leaks
[m6w6/ext-http] / php_http_querystring.c
index 67df721b9015729fbbb030d903614ff0f92eef88..a77118c406d89ab5e177f21d95a54e056e28b786 100644 (file)
@@ -29,7 +29,7 @@ static inline void php_http_querystring_set(zval *instance, zval *params, int fl
        array_init(&qa);
 
        if (flags & QS_MERGE) {
-               zval *old = zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0);
+               zval old_tmp, *old = zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0, &old_tmp);
 
                ZVAL_DEREF(old);
                if (Z_TYPE_P(old) == IS_ARRAY) {
@@ -39,11 +39,12 @@ static inline void php_http_querystring_set(zval *instance, zval *params, int fl
 
        php_http_querystring_update(&qa, params, NULL);
        zend_update_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), &qa);
+       zval_ptr_dtor(&qa);
 }
 
 static inline void php_http_querystring_str(zval *instance, zval *return_value)
 {
-       zval *qa = zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0);
+       zval qa_tmp, *qa = zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0, &qa_tmp);
 
        ZVAL_DEREF(qa);
        if (Z_TYPE_P(qa) == IS_ARRAY) {
@@ -53,18 +54,20 @@ static inline void php_http_querystring_str(zval *instance, zval *return_value)
        }
 }
 
-static inline void php_http_querystring_get(zval *instance, int type, char *name, uint name_len, zval *defval, zend_bool del, zval *return_value TSRMLS_DC)
+static inline void php_http_querystring_get(zval *instance, int type, char *name, uint name_len, zval *defval, zend_bool del, zval *return_value)
 {
-       zval *arrval, *qarray = zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0 TSRMLS_CC);
+       zval *arrval, qarray_tmp, *qarray = zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0, &qarray_tmp);
 
        ZVAL_DEREF(qarray);
        if ((Z_TYPE_P(qarray) == IS_ARRAY) && (arrval = zend_symtable_str_find(Z_ARRVAL_P(qarray), name, name_len))) {
                if (type && type != Z_TYPE_P(arrval)) {
-                       SEPARATE_ZVAL(arrval);
-                       convert_to_explicit_type(arrval, type);
-                       RETVAL_ZVAL(arrval, 1, 1);
+                       zval tmp;
+
+                       ZVAL_DUP(&tmp, arrval);
+                       convert_to_explicit_type(&tmp, type);
+                       RETVAL_ZVAL(&tmp, 0, 0);
                } else {
-                       RETVAL_ZVAL_FAST(arrval);
+                       RETVAL_ZVAL(arrval, 1, 0);
                }
 
                if (del) {
@@ -76,7 +79,7 @@ static inline void php_http_querystring_get(zval *instance, int type, char *name
                        zval_ptr_dtor(&delarr);
                }
        } else if(defval) {
-               RETURN_ZVAL_FAST(defval);
+               RETURN_ZVAL(defval, 1, 0);
        }
 }
 
@@ -185,7 +188,6 @@ ZEND_RESULT_CODE php_http_querystring_parse(HashTable *ht, const char *str, size
                } while (*asi_str);
 
                opts.param = php_http_params_separator_init(&arr);
-
                zval_ptr_dtor(&arr);
        }
 
@@ -222,7 +224,7 @@ ZEND_RESULT_CODE php_http_querystring_update(zval *qarray, zval *params, zval *o
 
                /* squeeze the hash out of the zval */
                if (Z_TYPE_P(params) == IS_OBJECT && instanceof_function(Z_OBJCE_P(params), php_http_querystring_class_entry)) {
-                       zval *qa = zend_read_property(php_http_querystring_class_entry, params, ZEND_STRL("queryArray"), 0);
+                       zval qa_tmp, *qa = zend_read_property(php_http_querystring_class_entry, params, ZEND_STRL("queryArray"), 0, &qa_tmp);
 
                        ZVAL_DEREF(qa);
                        convert_to_array(qa);
@@ -239,7 +241,7 @@ ZEND_RESULT_CODE php_http_querystring_update(zval *qarray, zval *params, zval *o
                        ht = Z_ARRVAL(zv);
                }
 
-               ZEND_HASH_FOREACH_KEY_VAL(ht, key.h, key.key, params_entry)
+               ZEND_HASH_FOREACH_KEY_VAL_IND(ht, key.h, key.key, params_entry)
                {
                        /* only public properties */
                        if (!key.key || *key.key->val) {
@@ -257,12 +259,12 @@ ZEND_RESULT_CODE php_http_querystring_update(zval *qarray, zval *params, zval *o
                                        /*
                                         * update
                                         */
-                                       zval equal, *entry = NULL;
+                                       zval equal, tmp, *entry = &tmp;
 
+                                       ZVAL_UNDEF(&tmp);
                                        /* recursive */
                                        if (Z_TYPE_P(params_entry) == IS_ARRAY || Z_TYPE_P(params_entry) == IS_OBJECT) {
-                                               entry = qarray_entry;
-                                               SEPARATE_ZVAL(entry);
+                                               ZVAL_DUP(entry, qarray_entry);
                                                convert_to_array(entry);
                                                php_http_querystring_update(entry, params_entry, NULL);
                                        } else if ((FAILURE == is_equal_function(&equal, qarray_entry, params_entry)) || Z_TYPE(equal) != IS_TRUE) {
@@ -338,52 +340,39 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_getGlobalInstance, 0, 0, 0)
 ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, getGlobalInstance)
 {
-       zval *instance;
+       zval *instance, *_GET;
        zend_string *zs;
 
        php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
 
        zs = zend_string_init(ZEND_STRL("instance"), 0);
-       instance = zend_std_get_static_property(php_http_querystring_class_entry, zs, 0, NULL);
+       instance = zend_std_get_static_property(php_http_querystring_class_entry, zs, 0);
        zend_string_release(zs);
 
-       if (Z_TYPE_P(instance) != IS_OBJECT) {
-               zval *_GET = NULL;
-               zend_string *zs = zend_string_init("_GET", lenof("_GET"), 0);
-
-               zend_is_auto_global(zs);
+       if (Z_TYPE_P(instance) == IS_OBJECT) {
+               RETVAL_ZVAL(instance, 1, 0);
+       } else if ((_GET = php_http_env_get_superglobal(ZEND_STRL("_GET")))) {
+               ZVAL_OBJ(return_value, php_http_querystring_object_new(php_http_querystring_class_entry));
 
-               if ((_GET = zend_hash_find(&EG(symbol_table).ht, zs))
-               &&      (Z_TYPE_P(_GET) == IS_ARRAY)
-               ) {
-                       zval new_instance;
-
-                       ZVAL_OBJ(&new_instance, php_http_querystring_object_new(php_http_querystring_class_entry));
-
-                       ZVAL_MAKE_REF(_GET);
-                       zend_update_property(php_http_querystring_class_entry, &new_instance, ZEND_STRL("queryArray"), _GET);
-
-                       zend_update_static_property(php_http_querystring_class_entry, ZEND_STRL("instance"), instance);
-                       instance = &new_instance;
-               } else {
-                       php_http_throw(unexpected_val, "Could not acquire reference to superglobal GET array", NULL);
-               }
+               ZVAL_MAKE_REF(_GET);
+               zend_update_property(php_http_querystring_class_entry, return_value, ZEND_STRL("queryArray"), _GET);
 
-               zend_string_release(zs);
+               zend_update_static_property(php_http_querystring_class_entry, ZEND_STRL("instance"), return_value);
+       } else {
+               php_http_throw(unexpected_val, "Could not acquire reference to superglobal GET array", NULL);
        }
 
-       RETVAL_ZVAL_FAST(instance);
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_getIterator, 0, 0, 0)
 ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, getIterator)
 {
-       zval *qa;
+       zval qa_tmp, *qa;
 
        php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
 
-       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0);
+       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0, &qa_tmp);
 
        object_init_ex(return_value, spl_ce_RecursiveArrayIterator);
        zend_call_method_with_1_params(return_value, spl_ce_RecursiveArrayIterator, NULL, "__construct", NULL, qa);
@@ -403,14 +392,14 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_toArray, 0, 0, 0)
 ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, toArray)
 {
-       zval *zqa;
+       zval zqa_tmp, *zqa;
 
        if (SUCCESS != zend_parse_parameters_none()) {
                return;
        }
 
-       zqa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0);
-       RETURN_ZVAL_FAST(zqa);
+       zqa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0, &zqa_tmp);
+       RETURN_ZVAL(zqa, 1, 0);
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_get, 0, 0, 0)
@@ -480,13 +469,15 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_mod, 0, 0, 0)
 ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, mod)
 {
-       zval *params, *instance = getThis();
+       zval qa_tmp, *params, *instance = getThis();
        zend_error_handling zeh;
 
        php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "z", &params), invalid_arg, return);
        
        zend_replace_error_handling(EH_THROW, php_http_exception_bad_querystring_class_entry, &zeh);
        ZVAL_OBJ(return_value, Z_OBJ_HT_P(instance)->clone_obj(instance));
+       /* make sure we do not inherit the reference to _GET */
+       SEPARATE_ZVAL(zend_read_property(Z_OBJCE_P(return_value), return_value, ZEND_STRL("queryArray"), 0, &qa_tmp));
        php_http_querystring_set(return_value, params, QS_MERGE);
        zend_restore_error_handling(&zeh);
 }
@@ -523,12 +514,12 @@ PHP_METHOD(HttpQueryString, xlate)
 {
        char *ie, *oe;
        size_t ie_len, oe_len;
-       zval na, *qa;
+       zval na, qa_tmp, *qa;
 
        php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &ie, &ie_len, &oe, &oe_len), invalid_arg, return);
 
        array_init(&na);
-       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0);
+       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0, &qa_tmp);
        ZVAL_DEREF(qa);
        convert_to_array(qa);
 
@@ -538,7 +529,7 @@ PHP_METHOD(HttpQueryString, xlate)
        );
 
        php_http_querystring_set(getThis(), &na, 0);
-       RETVAL_ZVAL_FAST(getThis());
+       RETVAL_ZVAL(getThis(), 1, 0);
        
        zval_ptr_dtor(&na);
 }
@@ -578,18 +569,18 @@ ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, offsetGet)
 {
        zend_string *offset;
-       zval *value, *qa;
+       zval *value, qa_tmp, *qa;
 
        if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S", &offset)) {
                return;
        }
        
-       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0);
+       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0, &qa_tmp);
        ZVAL_DEREF(qa);
 
        if (Z_TYPE_P(qa) == IS_ARRAY) {
                if ((value = zend_symtable_find(Z_ARRVAL_P(qa), offset))) {
-                       RETVAL_ZVAL_FAST(value);
+                       RETVAL_ZVAL(value, 1, 0);
                }
        }
 }
@@ -601,16 +592,21 @@ ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, offsetSet)
 {
        zend_string *offset;
-       zval *value, param;
+       zval *value, param, znull;
        
        if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Sz", &offset, &value)) {
                return;
        }
 
-       array_init(&param);
+       array_init_size(&param, 1);
+       /* unset first */
+       ZVAL_NULL(&znull);
+       zend_symtable_update(Z_ARRVAL(param), offset, &znull);
+       php_http_querystring_set(getThis(), &param, QS_MERGE);
+       /* then update, else QS_MERGE would merge sub-arrrays */
        Z_TRY_ADDREF_P(value);
        zend_symtable_update(Z_ARRVAL(param), offset, value);
-       php_http_querystring_set(getThis(), &param, 0);
+       php_http_querystring_set(getThis(), &param, QS_MERGE);
        zval_ptr_dtor(&param);
 }
 
@@ -620,13 +616,13 @@ ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, offsetExists)
 {
        zend_string *offset;
-       zval *value, *qa;
+       zval *value, qa_tmp, *qa;
 
        if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S", &offset)) {
                return;
        }
        
-       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0);
+       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0, &qa_tmp);
        ZVAL_DEREF(qa);
 
        if (Z_TYPE_P(qa) == IS_ARRAY) {