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

Reject null return value from MethodReplacer for primitive return type #32412

Conversation

LeMikaelF
Copy link
Contributor

@LeMikaelF LeMikaelF commented Mar 11, 2024

If a MethodReplacer replaces a method with a primitive return type, and if the replacer returns null, CGLIB silently converts the null to a zero-value primitive (false or 0), instead of throwing an exception (see CodeEmitter::zero_or_null in CGLIB).

This does not align with the rest of the Spring Framework. For example, the AOP module checks for a null in advices for methods with primitive return types before it reaches the CGLIB proxy (CglibAopProxy::processReturnType) and throws an AopInvocationException. Similarly, the AOP module maintains functional parity between the JDK Proxy and CGLIB proxies by wrapping an undeclared checked exception in an UndeclaredThrowableException. Moreover, silently converting a null to a non-null value can lead to hard-to-find bugs.

MvcUriComponentsBuilder.ControllerMethodInvocationInterceptor also has this quirk, but the return values of the proxies it returns aren't meant to be used, so I didn't touch that one.

This PR follows the pattern used by CglibAopProxy and throws an exception if null is returned in the advice for a method with a primitive return type.

@pivotal-cla
Copy link

@LeMikaelF Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 11, 2024
@pivotal-cla
Copy link

@LeMikaelF Thank you for signing the Contributor License Agreement!

@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 11, 2024
@sbrannen sbrannen added this to the 6.2.0-M1 milestone Mar 11, 2024
@sbrannen sbrannen self-assigned this Mar 11, 2024
@sbrannen sbrannen changed the title Throw NPE instead of silently converting null to primitive in method replacer. Throw exception instead of silently converting null to primitive in method replacer Mar 11, 2024
@sbrannen sbrannen changed the title Throw exception instead of silently converting null to primitive in method replacer Reject null return value from MethodReplacer for primitive return type Mar 11, 2024
sbrannen pushed a commit that referenced this pull request Mar 11, 2024
This commit throws an exception instead of silently converting a null
return value from a MethodReplacer to a primitive 0/false value.

See gh-32412
@sbrannen sbrannen closed this in 04e69bd Mar 11, 2024
@sbrannen
Copy link
Member

Hi @LeMikaelF,

Congratulations on submitting your first PR for the Spring Framework! 👍

This has been merged into main in 3e48031 and revised in 04e69bd.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants