Skip to content

Possible NPE in Spring MVC LogFormatUtils #27782

Closed
@gonzalad

Description

@gonzalad

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);
	}

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)in: webIssues in web modules (web, webmvc, webflux, websocket)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions