Skip to content

fix: don't treat backslashes in comments as escapes - #96

Merged
quintesse merged 1 commit into
codejive:mainfrom
wleeaf:comment-backslashes
Oct 25, 2026
Merged

quintesse merged 1 commit into
codejive:mainfrom
wleeaf:comment-backslashes

Conversation

@wleeaf

@wleeaf wleeaf commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

PropertiesParser processes escape sequences in every token, comments included. In java.util.Properties, a backslash in a comment line has no special meaning, and a comment line can't be continued onto the next line. Two things go wrong here because of that.

A comment containing something like a Windows path makes loading fail, because \u starts a \uXXXX escape:

# see C:\users\me
alpha=1
java.lang.RuntimeException: java.io.IOException: Invalid unicode escape character: 115

A comment that ends with a backslash is treated as a line continuation, so the next line becomes part of the comment and its property is lost:

# temp dir: C:\temp\
alpha=1

loadProperties returns no alpha here, while java.util.Properties loads alpha=1 from both files.

addChar() now skips escape handling while reading a COMMENT token, so a comment runs up to the end of its line as written. This also means getPropertyComment() returns such comments as they appear in the file, instead of with \t and similar sequences decoded.

Tests: TestPropertiesParser.testBackslashesInComment checks the tokens for a comment with \users\ followed by a property, and TestProperties.testCommentEndingWithBackslash checks that the property after such a comment is loaded. Both fail without the change. mvn -B spotless:check package passes on JDK 21 (82 tests).

I found this by loading 20,000 generated .properties inputs with both this library and java.util.Properties and comparing the results. With this change, the inputs where only this parser fails drop from 242 to 13, and 463 more inputs give the same result as the JDK. 11 inputs that matched before only because the comment swallowed the next line now differ. In each of them that next line is a lone \, which this parser reads differently from the JDK (a separate issue).

The parser handled escape sequences in every token, including comments.
In java.util.Properties a backslash has no special meaning in a comment
line, and a comment line can't be continued. Here a comment with a
Windows path made loading fail ("# C:\users" is read as a malformed
\uXXXX escape), and a comment ending with a backslash swallowed the
next line, so the property on it was lost:

    # temp dir: C:\temp\
    alpha=1

Comments are now read as they are, up to the end of the line.
@quintesse

Copy link
Copy Markdown
Collaborator

Great catch, thanks for your help @wleeaf !

@quintesse
quintesse merged commit d24db19 into codejive:main Oct 25, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants