phpserialize icon indicating copy to clipboard operation
phpserialize copied to clipboard

Support for circular references is missing

Open denisvmedia opened this issue 5 years ago • 1 comments

This code fails:

package main

import (
	"github.com/elliotchance/phpserialize"
	"fmt"
)

type A struct {
	Id string
	B *B
}
type B struct {
	Id string
	A *A
}

func main() {
	var in A
	err := phpserialize.Unmarshal(
		[]byte(`O:1:"A":2:{s:2:"id";s:1:"1";s:1:"b";O:1:"B":2:{s:2:"id";s:1:"2";s:1:"a";r:1;}}`),
		&in1,
	)
	fmt.Println(err)
	fmt.Println(in)
}

The error I'm getting: can not consume type: r:1;}} The serialized string I used comes from PHP. Vice versa it doesn't work as well - crashes because of endless recursion.

P.S. PHP code I used:

<?php

class A {
    public $id;
    public $b;
}

class B {
    public $id;
    public $a;
}

$a = new A();
$b = new B();
$a->id = "1";
$a->b = $b;
$b->a = $a;
$b->id = "2";

var_dump(serialize($a));
var_dump(serialize($b));
?>

denisvmedia avatar Feb 01 '19 15:02 denisvmedia

Thanks for reporting this. I won't have to time to fix this, but always welcome pull requests.

elliotchance avatar Feb 03 '19 22:02 elliotchance