Skip to content

fix: handle null constructor in NewFilledInstanceInstruction and varargs with too few args - #456

Open
chenjunwenhao wants to merge 1 commit into
alibaba:mainfrom
chenjunwenhao:fix/new-filled-instance-npe-and-varargs-crash
Open

fix: handle null constructor in NewFilledInstanceInstruction and varargs with too few args#456
chenjunwenhao wants to merge 1 commit into
alibaba:mainfrom
chenjunwenhao:fix/new-filled-instance-npe-and-varargs-crash

Conversation

@chenjunwenhao

Copy link
Copy Markdown
Contributor

Summary

Fix two runtime crashes that expose raw Java exceptions instead of meaningful QLExpress error messages.

Bug 1: NullPointerException in NewFilledInstanceInstruction

When using the classified-JSON syntax ({'@class': 'ClassName', ...}) to instantiate a class that has no no-arg constructor, loadConstructor() returns null but the code proceeds to call constructor.newInstance(), resulting in a raw NPE.

Fix: Added a null check matching the pattern already used in NewInstanceInstruction, reporting NO_SUITABLE_CONSTRUCTOR with a clear message.

Reproduction:

{'@class': 'com.example.ClassWithNoDefaultCtor', 'field': 'value'}

Bug 2: ArrayIndexOutOfBoundsException in MemberResolver.adapt2VarArgTypes()

When calling a varargs Java method with fewer arguments than the number of required (non-vararg) parameters, System.arraycopy copies more elements than the destination array can hold, causing an ArrayIndexOutOfBoundsException.

Fix: Added a pre-check in both resolveConstructor() and resolveMethod() to skip varargs candidates where argTypes.length < parameterTypes.length - 1, allowing the resolver to gracefully report "method not found" instead of crashing.

Reproduction:

// Java: static int sum(int required, int... rest)
VarArgsHelper.sum()  // 0 args for a method requiring at least 1

Additional: Preserve exception cause chains

The catch (Exception e) blocks in both NewFilledInstanceInstruction.newInstance() and NewInstanceInstruction.newObject() did not pass the caught exception to ErrorReporter.report(), losing the original stack trace. Now e is passed as the catchObj parameter, consistent with how InvocationTargetException is already handled in the same methods.

Test plan

  • testNewFilledInstanceWithNoDefaultConstructor — verifies NO_SUITABLE_CONSTRUCTOR error instead of NPE
  • testVarArgsMethodWithTooFewArgs — verifies graceful error for too-few-args varargs call, plus validates normal varargs calls (0 args, exact minimum, multiple varargs items) still work correctly

…rgs with too few args

Two bug fixes:

1. NewFilledInstanceInstruction.newInstance() threw NullPointerException
   when loadConstructor() returned null (e.g., class without no-arg
   constructor). Added null check matching NewInstanceInstruction pattern,
   reporting NO_SUITABLE_CONSTRUCTOR instead.

2. MemberResolver.adapt2VarArgTypes() threw ArrayIndexOutOfBoundsException
   when calling varargs methods with fewer arguments than required
   parameters. Added pre-check in both resolveConstructor() and
   resolveMethod() to skip varargs candidates that cannot satisfy
   the minimum required parameters.

Also preserves exception cause chains in NewFilledInstanceInstruction
and NewInstanceInstruction catch blocks by passing the caught exception
to ErrorReporter.report().
@DQinYuan DQinYuan self-assigned this Aug 10, 2026
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.

2 participants