Skip to content

fix: drop a backslash at the end of the input - #97

Merged
quintesse merged 1 commit into
codejive:mainfrom
wleeaf:backslash-at-eof
Oct 25, 2026
Merged

quintesse merged 1 commit into
codejive:mainfrom
wleeaf:backslash-at-eof

Conversation

@wleeaf

@wleeaf wleeaf commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

When the input ends with a backslash (for example, the last line of a file without a final newline ends in a line continuation), addChar() reads the end of the stream as the escaped character. It stores (char) -1, which is '�':

Properties p = Properties.loadProperties(new StringReader("foo=bar\\"));
p.getProperty("foo"); // "bar�"

A key ending there gets the same extra character ("foo\\" loads the key "foo�"). java.util.Properties drops such a backslash and loads "bar" and "foo".

addChar() now returns without appending anything when the character after a backslash is the end of the input, and unescape() ignores a trailing lone backslash. The backslash stays in the raw text, so store() still writes the file back exactly as it was read.

Test: TestPropertiesParser.testBackslashAtEof checks that foo=bar\ at the end of the input gives the value text bar with the raw text bar\. It fails without the change. mvn -B spotless:check package passes on JDK 21 (81 tests).

I found this by loading 20,000 generated .properties inputs with both this library and java.util.Properties and comparing the results. 114 inputs that differed now match the JDK, and none that matched before differ now.

When the input ended with a backslash, the parser read the end of the
stream (-1) as the escaped character and stored it as '\uffff'. So
"foo=bar\" at the end of a file without a final newline loaded as
"bar\uffff", and a key ending there got the same extra character.
java.util.Properties drops such a backslash and loads "bar".

The trailing backslash now stays in the raw text, so the file is
written back unchanged, but it is left out of the key or value.
@quintesse

Copy link
Copy Markdown
Collaborator

Oh wow, good catch! 👍

@quintesse
quintesse merged commit 89d153c 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