Seralize incorrectly duplicates element in case of circular reference in array
Nessuno ha ancora preso questa issue.
- Lingua principale
- C
- Stelle
- 40.4k
- Fork
- 8.1k
- Merge medio
- 2g 13h
- PR unite (30g)
- 96
Descrizione
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)
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia eseguendo il riproduttore PHP fornito e confrontando l'output serializzato con la forma prevista della referenza circolare. Analizza il comportamento della serializzazione e deserializzazione degli array di PHP per le referenze circolari; l'issue è completata quando l'array padre viene preservato per riferimento e modificarlo dopo la deserializzazione si propaga alla radice.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- c, php
- Ambito
- backend
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 42/100