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

Possible NPE in Spring MVC LogFormatUtils #27782

Closed
gonzalad opened this issue Dec 8, 2021 · 0 comments
Closed

Possible NPE in Spring MVC LogFormatUtils #27782

gonzalad opened this issue Dec 8, 2021 · 0 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) in: web Issues in web modules (web, webmvc, webflux, websocket) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Milestone

Comments

@gonzalad
Copy link

gonzalad commented Dec 8, 2021

Affects: 5.2.15

When using Spring Boot (2.3.12) and when I activate debug: true in application.yml, I can get a NullPointerException thrown by LogFormatUtils.formatValue(LogFormatUtils.java:62).

Sample stacktrace:

java.lang.NullPointerException: null
    at org.springframework.core.log.LogFormatUtils.formatValue(LogFormatUtils.java:62)
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.lambda$writeWithMessageConverters$0(AbstractMessageConverterMethodProcessor.java:274)
    at org.springframework.core.log.LogFormatUtils.traceDebug(LogFormatUtils.java:86)
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:273)
    at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:219)
    at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:123)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)

Looking at the code of LogFormatUtils, the error can happen if value.toString() returns null.
Which is the case in my situation (value is an instance if InMemoryResource in my sample)

	public static String formatValue(@Nullable Object value, boolean limitLength) {
		if (value == null) {
			return "";
		}
		String str;
		if (value instanceof CharSequence) {
			str = "\"" + value + "\"";
		}
		else {
			try {
				str = value.toString();
			}
			catch (Throwable ex) {
				str = ex.toString();
			}
		}
		return (limitLength && str.length() > 100 ? str.substring(0, 100) + " (truncated)..." : str);
	}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 8, 2021
@jhoeller jhoeller added in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 8, 2021
@jhoeller jhoeller added this to the 5.3.14 milestone Dec 8, 2021
@jhoeller jhoeller self-assigned this Dec 8, 2021
@jhoeller jhoeller added the for: backport-to-5.2.x Marks an issue as a candidate for backport to 5.2.x label Dec 8, 2021
@spring-projects-issues spring-projects-issues added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.2.x Marks an issue as a candidate for backport to 5.2.x labels Dec 8, 2021
@jhoeller jhoeller added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Dec 8, 2021
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) in: web Issues in web modules (web, webmvc, webflux, websocket) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants