yiisoft / json

JSON encoding and decoding
https://www.yiiframework.com/
BSD 3-Clause "New" or "Revised" License
26 stars 8 forks source link

JSON helper recursive bug #1

Closed wirwolf closed 4 years ago

wirwolf commented 8 years ago
{
message: "Maximum function nesting level of '100' reached, aborting!",
code: 1,
line: 115,
file: "/home/wir_wolf/Web/www/testHost/new/vendor/yiisoft/yii2/helpers/BaseJson.php",
trace: [
{
function: "processData",
type: "::",
class: "yii\helpers\BaseJson",
file: "/home/wir_wolf/Web/www/testHost/new/vendor/yiisoft/yii2/helpers/BaseJson.php",
line: 141,
params: {
data: "",
expressions: "",
expPrefix: ""
},
args: {
data: "",
expressions: "",
expPrefix: ""
}
},
{
function: "processData",
type: "::",
class: "yii\helpers\BaseJson",
file: "/home/wir_wolf/Web/www/testHost/new/vendor/yiisoft/yii2/helpers/BaseJson.php",
line: 141,
params: {
data: "",
expressions: "",
expPrefix: ""
},
args: {
data: "",
expressions: "",
expPrefix: ""
}
},
{
function: "processData",
type: "::",
class: "yii\helpers\BaseJson",
file: "/home/wir_wolf/Web/www/testHost/new/vendor/yiisoft/yii2/helpers/BaseJson.php",
line: 141,
params: {
data: "",
expressions: "",
expPrefix: ""
},
args: {
data: "",
expressions: "",
expPrefix: ""
}
},
samdark commented 8 years ago

How to reproduce it?

wirwolf commented 8 years ago
        $tmp = \Yii::$app->response;
        $tmp->format = Response::FORMAT_JSON;
        $arr = [
            '1' =>
            [
                '2' =>
                [

                ],
                '3' =>
                [

                ]
            ]
        ];
        $arr['1']['2'] = &$arr['1'];
        $tmp->data = $arr;
        $tmp->send();

this bug in FORMAT_JSON and FORMAT_XML

samdark commented 8 years ago

How would expect such array to be formatted in JSON or XML?

samdark commented 8 years ago

It is recursive and both formats could not represent it.

wirwolf commented 8 years ago

how to convert error exeption trace to json?

samdark commented 8 years ago

Which error exception? Usually trace doesn't contain any recursion.

ileicovici commented 8 years ago

Hi. I also have this bug occurs config

'log' => [
    'traceLevel' => 0,
    'flushInterval' => 1,
    'targets' => [
        [
            'class' => 'yii\log\FileTarget',
            'levels' => ['error', 'warning'],
            'logVars' => [null],
            'exportInterval' => 1,
            'logFile' => '@app/runtime/logs/error.log',
        ],
    ],
],

for example usage

public function actionIndex()
{
    Yii::error("My error message");
}

in log file

2015-11-10 07:50:28 [127.0.0.1][-][-][error][application] My error message
2015-11-10 07:50:28 [127.0.0.1][-][-][error][application] My error message

the same occurs for Exception

for example usage

public function actionIndex()
{
    throw new HttpException(404, 'Error message');
}

in log file

2015-11-10 08:06:06 [127.0.0.1][-][-][error][yii\web\HttpException:404] exception 'yii\web\HttpException' with message 'Error message' in /var/www/components/controllers/SiteController.php:54
Stack trace:
#0 [internal function]: app\controllers\SiteController->actionIndex()
yiisoft/yii-core#1 /var/www/components/vendor/yiisoft/yii2/base/InlineAction.php(55): call_user_func_array(Array, Array)
yiisoft/yii-core#2 /var/www/components/vendor/yiisoft/yii2/base/Controller.php(151): yii\base\InlineAction->runWithParams(Array)
yiisoft/yii-core#3 /var/www/components/vendor/yiisoft/yii2/base/Module.php(455): yii\base\Controller->runAction('index', Array)
yiisoft/yii-core#4 /var/www/components/vendor/yiisoft/yii2/web/Application.php(84): yii\base\Module->runAction('site/index', Array)
yiisoft/yii-core#5 /var/www/components/vendor/yiisoft/yii2/base/Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
yiisoft/yii-core#6 /var/www/components/web/index.php(12): yii\base\Application->run()
yiisoft/yii-core#7 {main}
2015-11-10 08:06:07 [127.0.0.1][-][-][error][yii\web\HttpException:404] exception 'yii\web\HttpException' with message 'Error message' in /var/www/components/controllers/SiteController.php:54
Stack trace:
#0 [internal function]: app\controllers\SiteController->actionIndex()
yiisoft/yii-core#1 /var/www/components/vendor/yiisoft/yii2/base/InlineAction.php(55): call_user_func_array(Array, Array)
yiisoft/yii-core#2 /var/www/components/vendor/yiisoft/yii2/base/Controller.php(151): yii\base\InlineAction->runWithParams(Array)
yiisoft/yii-core#3 /var/www/components/vendor/yiisoft/yii2/base/Module.php(455): yii\base\Controller->runAction('index', Array)
yiisoft/yii-core#4 /var/www/components/vendor/yiisoft/yii2/web/Application.php(84): yii\base\Module->runAction('site/index', Array)
yiisoft/yii-core#5 /var/www/components/vendor/yiisoft/yii2/base/Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
yiisoft/yii-core#6 /var/www/components/web/index.php(12): yii\base\Application->run()
yiisoft/yii-core#7 {main}

debugging

vendor/yiisoft/yii2/log/Logger.php

public function flush($final = false)
{
    $messages = $this->messages;
    var_dump($messages); exit;
    // https://github.com/yiisoft/yii2/issues/5619
    // new messages could be logged while the existing ones are being handled by targets
    $this->messages = [];
    if ($this->dispatcher instanceof Dispatcher) {
        $this->dispatcher->dispatch($messages, $final);
    }
}

return http://joxi.ru/VrwqpZVcZBL82X

or in vendor/yiisoft/yii2/log/Dispatcher.php

public function dispatch($messages, $final)
{
    var_dump($messages); exit;
    $targetErrors = [];
    foreach ($this->targets as $target) {
        if ($target->enabled) {
            try {
                $target->collect($messages, $final);
            } catch (\Exception $e) {
                $target->enabled = false;
                $targetErrors[] = [
                    'Unable to send log via ' . get_class($target) . ': ' . ErrorHandler::convertExceptionToString($e),
                    Logger::LEVEL_WARNING,
                    __METHOD__,
                    microtime(true),
                    [],
                ];
            }
        }
    }
    if (!empty($targetErrors)) {
        $this->dispatch($targetErrors, true);
    }
}

return http://joxi.ru/Y2LX8v6fR34P26

cbhp commented 8 years ago

There's an option JSON_PARTIAL_OUTPUT_ON_ERROR for json_encode() which sets NULL instead of recursive references, see http://php.net/manual/en/json.constants.php

samdark commented 8 years ago

Since 5.5.0. We support 5.4.

cbhp commented 8 years ago

PHP 5.4 is outdated since 2015-09-14 and won't even get any security updates. Minimum requirement should be set to 5.6: http://php.net/supported-versions.php

samdark commented 8 years ago

In the next major version — certainly.

samdark commented 4 years ago

It's now solved as we're requiring 7.4.