Skip to content

_encode mishandles $@ generating error_string #3

@tlhackque

Description

@tlhackque

I don't have a reproducer for generating bad JSON, but code review revealed a bug in _encode.

As I haven't created a reproducer in your module, I don't plan to generate a PR. But you probably should produce a regression test and patch this.

In _encode, you attempt to remove the calling location from $@ with:

$self->{error_string} =~ s/\s+at\s+\S+\s+line\s+\d+\.?\s*//;

This doesn't take account of the file position that is sometimes included by die and croak.

Here's an example outside of JSON::API (type <CR> twice):

 perl -e'$x=<>.<>;eval{die "foo"}; print "$@\n"; $@ =~ s/\s+at\s+\S+\s+line\s+\d+\.?\s*//; print $@,"\n"'
foo at -e line 1, <> line 2.

foo, <> line 2.

Note that , <> line can be any file handle, and line can also be `chunk'.

You probably intended:

$self->{error_string} =~ s/\s+at\s+\S+\s+line\s+\d+(?:,\s+\S+\s+(?:line|chunk)?\s+\d+)?\..*\z//s; 

 perl -e'$x=<>.<STDIN>;eval{die "foo"}; print "$@\n"; $@ =~ s/\s+at\s+\S+\s+line\s+\d+(?:,\s+\S+\s+(?:line|chunk)\s+\d+)?\..*\z//s; print $@,"\n"'
foo at -e line 1, <STDIN> line 2.

foo

Note that JSON uses Carp::croak() rather than die(). E.g. you need to handle multiple lines:

perl -MCarp -e'$x=<>.<STDIN>;eval{croak "foo"}; print "$@\n"; $@ =~ s/\s+at\s+\S+\s+line\s+\d+(?:,\s+\S+\s+(?:line|chunk)\s+\d+)?\..*\z//s; print $@,"\n"'
foo at -e line 1, <STDIN> line 1.
        eval {...} called at -e line 1

foo

Carp was "recently" patched to include the file position, so you may need to update to see this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions