Skip to content

NullPointerException thrown when MessageSourceSupport#formatMessage is invoked with a null Locale #34751

Closed as not planned
@GiovanniChien

Description

@GiovanniChien

If you use org.springframework.context.MessageSource#getMessage(java.lang.String, java.lang.Object[], java.lang.String, java.util.Locale) and locale is null. When the org.springframework.context.support.MessageSourceSupport#formatMessage method is executed, the ConcurrentHashMap is used as the cache and the locale is null. As a result, the null pointer is generated when use null as the key.

protected String formatMessage(String msg, @Nullable Object[] args, Locale locale) {
	if (!isAlwaysUseMessageFormat() && ObjectUtils.isEmpty(args)) {
		return msg;
	}
	Map<Locale, MessageFormat> messageFormatsPerLocale = this.messageFormatsPerMessage
			.computeIfAbsent(msg, key -> new ConcurrentHashMap<>());

        // NullPointerException thrown when locale is null
	MessageFormat messageFormat = messageFormatsPerLocale.computeIfAbsent(locale, key -> {
		try {
			return createMessageFormat(msg, locale);
		}
		catch (IllegalArgumentException ex) {
			// Invalid message format - probably not intended for formatting,
			// rather using a message structure with no arguments involved...
			if (isAlwaysUseMessageFormat()) {
				throw ex;
			}
			// Silently proceed with raw message if format not enforced...
			return INVALID_MESSAGE_FORMAT;
		}
	});
	if (messageFormat == INVALID_MESSAGE_FORMAT) {
		return msg;
	}
	synchronized (messageFormat) {
		return messageFormat.format(resolveArguments(args, locale));
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)status: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions