Skip to content

Gson can throw StackOverFlowError which will most likely not be caught #2161

@fan87

Description

@fan87

Gson version

2.9.0

Description

Gson could throw StackOverFlowError with "[" * 8000, but most people will only catch Exception, not Error.

I don't think it's a real "security" vuln since most servers would catch every Throwable thrown by the code that handles the request instead of just killing the entire server but just in case.

Expected behavior

Throws a JSON syntax exception

Actual behavior

Throws an Error, which most people won't be catching

Reproduction steps

        Gson gson = new Gson();
        StringBuilder out = new StringBuilder();
        for (int i = 0; i < 8000; i++) {
            out.append("[");
        }

        try {
            gson.fromJson(out.toString(), JsonArray.class);
        } catch (Exception ignored) {
            System.out.println("Invalid Json Body!");
            // Most people will only be catching Exception, not Error
        }

Exception stack trace

(What a normal StackOverFlowError stacktrace looks like)

        (repeated until it throws stackoverflowerror)
	at com.google.gson.internal.bind.TypeAdapters$28.read(TypeAdapters.java:718)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions