Skip to content

OriginTrackedPropertiesLoader drops the line following a comment that ends with a backslash - #52270

Open
zhoubiao188 wants to merge 1 commit into
spring-projects:4.0.xfrom
zhoubiao188:fix-properties-loader-comment-continuation
Open

zhoubiao188 wants to merge 1 commit into
spring-projects:4.0.xfrom
zhoubiao188:fix-properties-loader-comment-continuation

Conversation

@zhoubiao188

Copy link
Copy Markdown
Contributor

A comment line that ends with a backslash causes the property on the next line to be silently dropped. For example, this file

#\
key=value

loads no properties at all, whereas java.util.Properties loads key=value.

Cause

isNewDocument looks ahead through a comment to work out whether it is a --- document separator. The look-ahead uses CharacterReader#read(), which treats a trailing backslash as a line continuation and carries on reading on the following line. The reader therefore ends up past the end of the comment line and skipComment() discards the rest of the following line, losing the property.

Fix

The remainder of the comment line is now inspected with a new CharacterReader#readRaw() that does not process escape sequences, so the look-ahead can never cross the line boundary. This matches java.util.Properties, where a trailing backslash has no special meaning inside a comment.

Verification

  • Added loadWhenCommentEndsWithBackslashLoadsFollowingProperties, loadWhenCommentWithExclamationPrefixEndsWithBackslashLoadsFollowingProperties and loadWhenDocumentSeparatorEndsWithBackslashLoadsSingleDocument. The first two fail without the change; the third one covers #---\, where b=b was previously swallowed as well.
  • Differential test against java.util.Properties over 100000 generated small properties files: 98 inputs that previously disagreed now match, and no input that previously matched now disagrees.
  • :core:spring-boot:checkFormat, checkstyleMain, checkstyleTest and the org.springframework.boot.env tests pass (43 tests in OriginTrackedPropertiesLoaderTests).

`OriginTrackedPropertiesLoader#isNewDocument` reads ahead through a
comment to work out whether it is a document separator. Those reads went
through `CharacterReader#read()` which treats a trailing backslash as a
line continuation, so a comment such as `#\`, `#a\` or `#---\` continued
reading on the following line. The reader therefore ended up past the end
of the comment line and `skipComment()` dropped the property on that
line.

The remaining characters of the comment are now read without handling
escape sequences so that the look-ahead can never cross the line
boundary. This matches `java.util.Properties`, where a trailing backslash
has no special meaning inside a comment.

Signed-off-by: zhoubiao188 <32508238+zhoubiao188@users.noreply.github.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 17, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants