Best practice of initializing zval array hash?

php.internals

Yasuo Ohgaki

9 years ago
Hi all, I'm wandering what is the best practice of zval array hash initialization. Following code leaks 56 bytes on ZVAL_NEW_ARR() chash = php_mb_convert_encoding_recursive(HASH_OF(entry), _to_encoding, _from_encodings); if (chash) { ZVAL_NEW_ARR(&entry_tmp); // _array_init() is called. Should leak by next line. Z_ARRVAL(entry_tmp) = chash; } I thought there would be API that replaces or initialize ZVAL array hash to existing hash, but I couldn't find one. So I fixed above code like chash = php_mb_convert_encoding_recursive(HASH_OF(entry), _to_encoding, _from_encodings); if (chash) { Z_TYPE_INFO(entry_tmp) = IS_ARRAY_EX; Z_ARRVAL(entry_tmp) = chash; } I used IS_ARRAY_EX like ZVAL_NEW_ARR(). However, this code seemed a little strange to me, so I grepped source tree and found no other .c code uses IS_ARRAY_EX. What's the best practice for this? It seems API does not expect to replace zval array hash by existing hash. Should I pass entry_tmp to php_mb_covert_encoding_recursive() instead? i.e. ZVAL_NEW_ARR(&entry_tmp); php_mb_convert_encoding_recursive(HASH_OF(&entry_tmp), HASH_OF(entry), _to_encoding, _from_encodings); Thank you.
-- Yasuo Ohgaki yohgaki@ohgaki.net

Nikita Popov

9 years ago
On Sat, Oct 15, 2016 at 8:23 AM, Yasuo Ohgaki <yohgaki@ohgaki.net> wrote:
> Hi all, > > I'm wandering what is the best practice of zval array hash initialization. > > Following code leaks 56 bytes on ZVAL_NEW_ARR() > > chash = php_mb_convert_encoding_recursive(HASH_OF(entry), > _to_encoding, _from_encodings); > if (chash) { > ZVAL_NEW_ARR(&entry_tmp); // _array_init() is called. Should leak by > next line. > Z_ARRVAL(entry_tmp) = chash; > } > > I thought there would be API that replaces or initialize ZVAL array > hash to existing hash, but I couldn't find one. So I fixed above code > like > > chash = php_mb_convert_encoding_recursive(HASH_OF(entry), > _to_encoding, _from_encodings); > if (chash) { > Z_TYPE_INFO(entry_tmp) = IS_ARRAY_EX; > Z_ARRVAL(entry_tmp) = chash; > } > > I used IS_ARRAY_EX like ZVAL_NEW_ARR(). However, this code seemed a > little strange to me, so I grepped source tree and found no other .c > code uses IS_ARRAY_EX. > > What's the best practice for this? > > It seems API does not expect to replace zval array hash by existing > hash. Should I pass entry_tmp to php_mb_covert_encoding_recursive() > instead? i.e. > > ZVAL_NEW_ARR(&entry_tmp); > php_mb_convert_encoding_recursive(HASH_OF(&entry_tmp), HASH_OF(entry), > _to_encoding, _from_encodings); > > Thank you. >
Are you looking for ZVAL_ARR()? Nikita

Yasuo Ohgaki

9 years ago
Hi Nikita, On Sat, Oct 15, 2016 at 6:22 PM, Nikita Popov <nikita.ppv@gmail.com> wrote:
> Are you looking for ZVAL_ARR()?
I don't know why I didn't think of it! Thank you.
-- Yasuo Ohgaki yohgaki@ohgaki.net