JSON-PP
JSON-PP copied to clipboard
Reentrance issue — `$convert_blessed` etc. are global variables and may be clobbered
The settings variables are global variables (although not visible in a global scope); they may be clobbered by a reentrant call to JSON::PP.
Here is a minimal test case demonstrating the problem:
use JSON;
sub MyClass::new { bless {}, shift }
sub MyClass::TO_JSON { encode_json([]) }
my $jsonist = JSON->new->convert_blessed;
print "So far, so good:\n";
print $jsonist->encode([MyClass->new]);
print "But the next line will crash:\n";
print $jsonist->encode([MyClass->new, MyClass->new]);
In the last line, the “JSONificiation” of a first MyClass->new
instance goes through the TO_JSON
method, which changes the global variables, which prevents the “JSONification” of the second object.
All the settings variables should be refactored as instance properties of the object returned by JSON->new
.
I just hit this issue as well. It has been open for only a year in a core Perl module - is there any likelihood of it being fixed? Otherwise I'll open an issue in the Perl github.