php / php/php-src

Seralize incorrectly duplicates element in case of circular reference in array

オープン
#11,743 コメント 7 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Bug Category: Serialization Status: Needs Triage
主要言語
C
スター
40.4k
フォーク
8.2k
平均マージ
2日 13時間
マージ済み PR(30日)
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)

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、提供されている PHP 再現コードを実行し、シリアライズされた出力を期待される循環参照の形式と比較します。循環参照に対する PHP の配列のシリアライズおよびアンシリアライズの動作を追跡します。アンシリアライズ後に親配列が参照として保持され、その配列を変更するとルートにも反映されれば、issue は完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
c, php
領域
backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
42/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。