Fix #980
(cherry picked from commit b59c33d8e40af336507d850fa12575344a8ba540)
Unit test
(cherry picked from commit 1b2f780ddaef1036fbe6753ee536e85117c6849f)
Merge pull request #982 from sjinks/issue-980-1.2.2

[1.2.2] Segfault in unserialize()