php / php/php-src

Seralize incorrectly duplicates element in case of circular reference in array

Đang mở
#11,743 7 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Bug Category: Serialization Status: Needs Triage
Ngôn ngữ chính
C
Star
40.4k
Fork
8.2k
Merge trung bình
2 ngày 13 giờ
Pull request đã merge (30 ngày)
96

Mô tả

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)

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách chạy trình tái hiện PHP được cung cấp và so sánh đầu ra đã serialize với dạng tham chiếu vòng được mong đợi. Theo dõi hành vi serialize và unserialize mảng của PHP đối với các tham chiếu vòng; issue được hoàn tất khi mảng cha được giữ bằng tham chiếu và việc sửa đổi nó sau khi unserialize được phản ánh lên gốc.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
c, php
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
42/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.