Skip to content

Possible NPE in ExitableInterpreterEngine:InterpreterEngine() found by SAST #8814

@ngr-ilmarh

Description

@ngr-ilmarh

Our SAST shows possible NullPointerException

Interpreter.LOG.info("I: " + ipc + ", R: " + rescuePCs[ipc] + " - " + instr + ">");
or redundant null check in
ipc = rescuePCs == null ? -1 : rescuePCs[ipc];

public class ExitableInterpreterEngine extends InterpreterEngine {
    public ExitableReturn interpret(ThreadContext context, Block block, IRubyObject self,
                                 ExitableInterpreterContext interpreterContext, ExitableInterpreterEngineState state,
                                 RubyModule implClass, String name, IRubyObject[] args, Block blockArg) {

...

        int[] rescuePCs = interpreterContext.getRescueIPCs();

...
        while (ipc < n) {
...

            Operation operation = instr.getOperation();
            if (debug) {
                Interpreter.LOG.info("I: " + ipc + ", R: "  + rescuePCs[ipc] + " - " + instr + ">");
                Interpreter.interpInstrsCount++;
            } else if (profile) {
                Profiler.instrTick(operation);
                Interpreter.interpInstrsCount++;
            }

            try {
...
            } catch (Throwable t) {
                if (debug) extractToMethodToAvoidC2Crash(instr, t);

                ipc = rescuePCs == null ? -1 : rescuePCs[ipc];

                if (debug) {
                    Interpreter.LOG.info("in : " + interpreterContext.getScope() + ", caught Java throwable: " + t + "; excepting instr: " + instr);
                    Interpreter.LOG.info("ipc for rescuer: " + ipc);
                }

                if (ipc == -1) {
                    Helpers.throwException(t);
                } else {
                    exception = t;
                }
            }
        }
...

If rescuePCs can be null, then NPE will be thrown, if rescuePCs can't be null, then null comparison is redundant.

Environment Information

We are analyzing versions 9.4.x (8-12), but this problem is still in master

Expected Behavior

  • No NPE at all. But it seems nobody catch it through the years, so i don't know if rescuePCs can be null at all

Actual Behavior

  • have no tests to show this NPE, can't figure out how to make one. This is simply code analysis, that showed possible NPE

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions