Skip to content
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

Fix an exception when validating license with year #222

Merged
merged 3 commits into from
Mar 8, 2018

Conversation

thc202
Copy link
Contributor

@thc202 thc202 commented Mar 7, 2018

Fix a StringIndexOutOfBoundsException in LicenseHeaderStep when validating if the license with a year is properly formatted, by checking if the start index of the second part of the license is greater than the length of the first part. This prevents the second part from overlapping the first, which could result in a negative length when obtaining the substring that contains the year.
The exception could happen when the year token is surrounded by the same characters, for example, "Copyright $YEAR DiffPlug", and a license containing "Copyright DiffPlug".


Split into two commits for easier review of the refactoring.

Refactor `LicenseHeaderStepTest` to allow to create license and class
files with custom license contents (defined in the class itself), to
make it easier to test new license contents without adding more files.

The license and class file templates (`LicenseHeaderWithPlaceholder` and
`FileWithLicenseHeaderAndPlaceholder`, respectively) contain a token
which is replaced with the custom license contents when running the
tests.
The year related license is now defined in `LicenseHeaderStepTest` and
the tests are changed to use it. Also, two more cases were added when
testing the year token, to improve the coverage of the condition that
checks if the year is properly defined in the license.

Add a method to `ResourceHarness` that allows to create a test file with
its contents processed by a function, to support the refactoring.
Fix a `StringIndexOutOfBoundsException` in `LicenseHeaderStep` when
validating if the license with a year is properly formatted, by checking
if the start index of the second part of the license is greater than the
length of the first part. This prevents the second part from overlapping
the first, which could result in a negative length when obtaining the
substring that contains the year.
The exception could happen when the year token is surrounded by the same
characters, for example, "Copyright $YEAR DiffPlug", and a license
containing "Copyright DiffPlug".
Update main and plugins CHANGES files.
@nedtwigg
Copy link
Member

nedtwigg commented Mar 8, 2018

Thanks, another good catch! Great commit history too 👍

@nedtwigg nedtwigg merged commit bcf7088 into diffplug:master Mar 8, 2018
@thc202 thc202 deleted the fix-licenseheaderstep-sioobe branch March 8, 2018 11:35
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