Seralize incorrectly duplicates element in case of circular reference in array
还没有人认领这个 Issue。
- 主要语言
- C
- 星标
- 40.4k
- 派生
- 8.1k
- 平均合并
- 2 天 13 小时
- 30 天内合并 PR
- 96
描述
Description
The following code:
<?php
$a = ['id'=>1,'parent'=>null,'child'=>null];
$b = ['id'=>2,'parent'=>null,'child'=>null];
$a['child'] =&$b;
$b['parent'] = &$a;
echo serialize($a);
Resulted in this output:
a:3:{s:2:"id";i:1;s:6:"parent";N;s:5:"child";a:3:{s:2:"id";i:2;s:6:"parent";a:3:{s:2:"id";i:1;s:6:"parent";N;s:5:"child";R:4;}s:5:"child";N;}}
The problem is that in the 'child' array with id 2, 'parent' duplicates the root array with id 1 instead of directly referencing it, something along the lines of :
a:3:{s:2:"id";i:1;s:6:"parent";N;s:5:"child";a:3:{s:2:"id";i:2;s:6:"parent";R:1;s:5:"child";N;}}
In any case, the parent array shouldn't be duplicated, as this can cause traversal issues or modifications that won't properly propagate if code tries to modify the de-serialized array (for instance modifying element 2's parent will not propagate to the root as it should), for instance :
If I modify the original array :
$a['id'] = 3;
echo($a['id'].','.$a['child']['parent']['id']);
echoes (properly) :
3,3
However, if I unserialize the aformentioned string, and apply the same operation, it echoes :
3,1
The reference is lost, as is apparent from the serialized form.
(Of course this is a trivial example but corresponds to something I've encountered working with graph-type arrays)
PHP Version
PHP 8.1.21 (FPM,CLI)
Operating System
Debian 10 x64 (on WSL2 on Windows 10 x64) ; Debian 11 x64 (native)
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先运行提供的 PHP 复现程序,并将序列化输出与预期的循环引用形式进行比较。跟踪 PHP 对循环引用进行数组序列化和反序列化时的行为;当父数组以引用方式保留,并且在反序列化后修改它会传播到根时,该 issue 就完成了。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- c, php
- 领域
- backend
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 42/100