Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incr_parse() Hangs on Certain Inputs #67

Merged
merged 5 commits into from May 12, 2022

Conversation

DabeDotCom
Copy link
Contributor

Notably, inputs that end in a number or [tfn]:

perl -MJSON::PP -e 'print JSON::PP->new->incr_parse(q|{"key": 1|)'

Whereas JSON::XS returns silently:

perl -MJSON::XS -e 'print JSON::XS->new->incr_parse(q|{"key": 1|)'

My fix is pretty trivial:

--- a/lib/JSON/PP.pm
+++ b/lib/JSON/PP.pm
@@ -1699,2 +1699,3 @@ INCR_PARSE:
         } elsif ( $mode == INCR_M_TFN ) {
+            last INCR_PARSE if $p >= $len && $self->{incr_nest};
             while ( $len > $p ) {
@@ -1710,2 +1711,3 @@ INCR_PARSE:
         } elsif ( $mode == INCR_M_NUM ) {
+            last INCR_PARSE if $p >= $len && $self->{incr_nest};
             while ( $len > $p ) {

But it's the tests that I spent the most time on (which is why I've left the PR unsquashed...)

Because the current behavior gets stuck in an infinite loop, I wanted to properly timeout, which meant reaching for Test::Fork:

use Test::Fork;
sub run_test {
    my ($num_tests, $input, $sub) = @_;
    my $pid = fork_ok($num_tests => sub {
        setpgrp 0, 0;
        $sub->($input);
    });

    local $SIG{ALRM} = sub { warn "\e[31mnot ok - '$input' hung; killing $pid...\e[m\n"; kill -9, $pid };
    alarm 10;
    waitpid $pid, 0;
    alarm 0;
}

If you have a better way, I'm all ears! :-D

@charsbar
Copy link
Collaborator

charsbar commented May 3, 2022

Thanks for the PR. Could you 1) fix the test and Makefile.PL not to depend on Test::Fork because JSON::PP is a core module and I don't want to add non-core dependency here, and could you also 2) remove JSON::XS from the loop because that should be for JSON.pm?

@DabeDotCom
Copy link
Contributor Author

Thanks for the PR. Could you 1) fix the test and Makefile.PL not to depend on Test::Fork because JSON::PP is a core module and I don't want to add non-core dependency here, and could you also 2) remove JSON::XS from the loop because that should be for JSON.pm?

"Done."

PS: You're right of course... It's not actually a test of JSON::XS; I just wanted to prove (to myself, at least) that JSON::PP and JSON::XS were producing the same results — and yeah, I see how that's better suited to JSON. 👍

Cheers! :-D

@charsbar charsbar merged commit 742a8e0 into makamaka:master May 12, 2022
@charsbar
Copy link
Collaborator

Sorry for the delay. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants