summaryrefslogtreecommitdiff
path: root/ext/json/json.c
diff options
context:
space:
mode:
authorAntony Dovgal <tony2001@php.net>2006-07-20 08:56:57 +0000
committerAntony Dovgal <tony2001@php.net>2006-07-20 08:56:57 +0000
commit3e308d3a6b6cba69654598836481477816a298f1 (patch)
treeb1620a2faf912070dcabb0e7e24bfa653867fcab /ext/json/json.c
parent0b97d2bd6072fc8939f452459f0b4ae46582a06a (diff)
downloadphp-git-3e308d3a6b6cba69654598836481477816a298f1.tar.gz
MFH: add recursion protection to json_encode() and new tests
Diffstat (limited to 'ext/json/json.c')
-rw-r--r--ext/json/json.c15
1 files changed, 15 insertions, 0 deletions
diff --git a/ext/json/json.c b/ext/json/json.c
index 0df5017e44..99e80fa913 100644
--- a/ext/json/json.c
+++ b/ext/json/json.c
@@ -135,6 +135,11 @@ static void json_encode_array(smart_str *buf, zval **val TSRMLS_DC) {
r = 1;
}
+ if (myht && myht->nApplyCount > 1) {
+ php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "recursion detected");
+ return;
+ }
+
if (r == 0)
{
smart_str_appendc(buf, '[');
@@ -151,6 +156,7 @@ static void json_encode_array(smart_str *buf, zval **val TSRMLS_DC) {
ulong index;
uint key_len;
HashPosition pos;
+ HashTable *tmp_ht;
int need_comma = 0;
zend_hash_internal_pointer_reset_ex(myht, &pos);
@@ -160,6 +166,11 @@ static void json_encode_array(smart_str *buf, zval **val TSRMLS_DC) {
break;
if (zend_hash_get_current_data_ex(myht, (void **) &data, &pos) == SUCCESS) {
+ tmp_ht = HASH_OF(*data);
+ if (tmp_ht) {
+ tmp_ht->nApplyCount++;
+ }
+
if (r == 0) {
if (need_comma) {
smart_str_appendc(buf, ',');
@@ -200,6 +211,10 @@ static void json_encode_array(smart_str *buf, zval **val TSRMLS_DC) {
json_encode_r(buf, *data TSRMLS_CC);
}
}
+
+ if (tmp_ht) {
+ tmp_ht->nApplyCount--;
+ }
}
}
}