xref: /php-src/ext/json/tests/gh15168.phpt (revision f3e87e2a)
1--TEST--
2GH-15168 (stack overflow in json_encode())
3--SKIPIF--
4<?php
5if (ini_get('zend.max_allowed_stack_size') === false) {
6    die('skip No stack limit support');
7}
8if (getenv('SKIP_ASAN')) {
9    die('skip ASAN needs different stack limit setting due to more stack space usage');
10}
11?>
12--INI--
13zend.max_allowed_stack_size=512K
14--FILE--
15<?php
16
17class Node
18{
19    public $next;
20}
21
22$firstNode = new Node();
23$node = $firstNode;
24for ($i = 0; $i < 30000; $i++) {
25    $newNode = new Node();
26    $node->next = $newNode;
27    $node = $newNode;
28}
29
30var_dump(json_encode($firstNode, depth: 500000));
31var_dump(json_last_error());
32var_dump(json_last_error_msg());
33
34while ($next = $firstNode->next) {
35    $firstNode->next = $next->next;
36}
37
38?>
39--EXPECT--
40bool(false)
41int(1)
42string(28) "Maximum stack depth exceeded"
43