-
Notifications
You must be signed in to change notification settings - Fork 712
Prevent NullPointerException in ReversedLinesFileReader constructors #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@mernst |
|
@garydgregory I added the test case from the pull request description. It fails on master and passes in this pull request. |
| public void testNullEncoding() throws IOException, URISyntaxException { | ||
| new ReversedLinesFileReader(new File(this.getClass().getResource("/test-file-empty.bin").toURI()), | ||
| (Charset) null); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @mernst,
Thank you for your update to this PR.
The new unit test indeed tests that the ctor does not blow on a null Charset but it does not test that the default Charset kicks in.
Another update would be great.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@garydgregory I agree that would be a useful test, even though it is not directly related to this bug fix.
I've made my best guess at how to test for the default Charset in a system-independent way.
I'm not sure whether this is what you had in mind. If not, could point me at documentation about how Commons IO prefers to test that the default Charset is being used?
Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @mernst,
A better way to test would be to write to the temp file with Charset.defaultCharset() and still read with a null Charset.
From a black-box perspective, writing with a null Charset does not prove anything since you're relying on the fact that another API does the right thing with a null Charset. This would really match the test with expectations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree these tests are useful to improve code coverage. But they are not directly related to the change, and previous changes to this code were permitted without adding these missing tests. Could we create a new pull request or issue to improve the coverage of the test suite, to avoid blocking this pull request?
|
@garydgregory Can this pull request be merged? |
|
I will review tomorrow. |
| @Test | ||
| public void testNullEncoding() throws IOException, URISyntaxException { | ||
| final File file = new File(temporaryFolder, "write.txt"); | ||
| final String text = "Hello /u1234"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is /u1234 supposed to be?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ping. Why is the weird Unicode escape needed here? If it is needed, please add a comment, otherwise, we don't need it right?
| public void testNullEncoding() throws IOException, URISyntaxException { | ||
| new ReversedLinesFileReader(new File(this.getClass().getResource("/test-file-empty.bin").toURI()), | ||
| (Charset) null); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @mernst,
A better way to test would be to write to the temp file with Charset.defaultCharset() and still read with a null Charset.
From a black-box perspective, writing with a null Charset does not prove anything since you're relying on the fact that another API does the right thing with a null Charset. This would really match the test with expectations.
|
@mernst |
|
@garydgregory Thanks for the fix! I appreciate it. Credit is secondary. |
In many parts of the Commons IO API,
nullmay be passed as a Charset or the name of one, and Commons IO uses the platform's default character encoding.I assumed that was the case for ReversedLinesFileReader, and the result was a null pointer exception.
Consider the following minimal example:
Running this program results in:
I assume that the
ReversedLinesFileReaderconstructor is intended to acceptnullas an argument because of this line:which calls
toCharset(Charset)which is a no-op unless its argument isnull. So there would be no purpose to that line except to handle a nullencodingargument to the constructor.The problem comes a few lines later when the possibly-null value
encodingis used:This pull request changes the code so the
encodingfield is non-null even if theencodingformal parameter is null, and usesthis.encodinginstead ofencodingin 4 locations.I think that setting the field
encodingto a non-null value is the right thing because there are three calls tonew Stringthat useencodingas if it is non-null.An alternate fix would be to:
charsetlocal variable, andcharsetlocal variable into uses ofencoding.