0

I have this string saved in database, using Doctrine's array type.

$test = unserialize('a:10:{s:4:"data";s:12:"registration";s:5:"order";s:4:"desc";s:5:"mySex";i:2;s:3:"sex";s:3:"all";s:6:"ageMin";i:36;s:6:"ageMax";i:46;s:9:"interests";O:43:"Doctrine\Common\Collections\ArrayCollection":1:{s:53:"Doctrine\Common\Collections\ArrayCollectionelements";a:1:{i:0;O:41:"Singles\Bundle\UserBundle\Entity\Interest":4:{s:45:"Singles\Bundle\UserBundle\Entity\Interestid";N;s:50:"Singles\Bundle\UserBundle\Entity\Interestprofile";N;s:46:"Singles\Bundle\UserBundle\Entity\Interestsex";i:1;s:51:"Singles\Bundle\UserBundle\Entity\Interestactivity";i:0;}}}s:14:"onlyWithPhotos";b:1;s:8:"counties";N;s:9:"districts";N;}');

It always throws an exception with message:

Notice: unserialize(): Error at offset 261 of 615 bytes

That is exactly the semicolon at end of this string:

... Doctrine\Common\Collections\ArrayCollectionelements";

What could cause the deserialization to fail? Whenever I exclude the array collection, deserialization works. The serialization is done using doctrine, I don't do it manually.

tomazahlin
  • 2,137
  • 1
  • 24
  • 29

1 Answers1

1

Output of serialize is binary string, which can contain null bytes – eg \x00. Deserialization fails because of you are passing string trimmed of that null bytes, so string size of FQCN doesn't match. Eg:

->  Size is set to 53:
-> …ArrayCollection":1:{s:53:"Doctrine\Common\C…
->  But it is 51:
-> …ArrayCollection":1:{s:51:"Doctrine\Common\C…
jkucharovic
  • 4,214
  • 1
  • 31
  • 46
  • Thanks! This was the solution! I have used a function here - https://stackoverflow.com/questions/3148712/regex-code-to-fix-corrupt-serialized-php-data to fix the length of elements, after that unserialize worked. – tomazahlin Jul 11 '17 at 08:54
  • 1
    @tomazahlin if you want to avoid this issue, you can use `base64_encode` after serialization, and before unserialize `base64_decode`. – jkucharovic Jul 11 '17 at 09:11