Skip to content

[core] Fix off-by-one error in AbstractJjtreeNode.fitTokensToChildren() - #7061

Merged
UncleOwen merged 4 commits into
pmd:mainfrom
UncleOwen:AbstractJjtreeNode-off-by-one
Sep 15, 2026
Merged

UncleOwen merged 4 commits into
pmd:mainfrom
UncleOwen:AbstractJjtreeNode-off-by-one

Conversation

@UncleOwen

@UncleOwen UncleOwen commented Sep 11, 2026 •

Copy link
Copy Markdown
Member

Describe the PR

I've been browsing through sonar, and I think it has found a valid off-by-one bug here. If index == getNumChildren() is true, getChild(index) will throw an ArrayIndexOutOfBoundsException.

This issue never shows, because insertChild is only ever used to insert at the front, never at the back. Still, I think this problem should be fixed. But this means there is no reproduction test case to verify.

This PR closes the bug and adds unit tests for AbstractJjtreeNode.insertChild. I'm not quite happy with the latter. The mocks for compareTo on the tokens only do enough for the code to run, But they don't follow the contract of Comparable. Improvements are welcome.

Related issues

  • None

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • [n/a] Added (in-code) documentation (if needed)

@pmd-actions-helper

pmd-actions-helper Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-09-12 16:18:02+00:00 for 64b2255)

@UncleOwen
UncleOwen merged commit 8c73fec into pmd:main Sep 15, 2026
13 checks passed
@UncleOwen
UncleOwen deleted the AbstractJjtreeNode-off-by-one branch September 15, 2026 07:06
@UncleOwen UncleOwen added this to the 7.28.0 milestone Sep 15, 2026

@Test
void testDummy() {
// Without this, the test will fail?!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception "No Tests have been executed in Test Set" is coming from https://github.com/pmd/build-tools/blob/8e1706f38c8956527396ffb6e57ce836b9bf1281/src/main/java/net/sourceforge/pmd/buildtools/surefire/junit/TestExecutionListener.java#L81

IIRC, I've added this, because we had cases (maybe with kotest, maybe normal JUnit tests), where we had many test methods, but no tests have ever been executed and the build is green, giving a very false sense of safety...

This is activated by the configuration for surefire plugin:

pmd/pom.xml

Lines 350 to 371 in 07cf000

<statelessTestsetInfoReporter implementation="net.sourceforge.pmd.buildtools.surefire.PMDStatelessTestSetInfoConsoleReporter">
<showFailedTests>true</showFailedTests>
<showSuccessfulTests>false</showSuccessfulTests>
<showSkippedTests>true</showSkippedTests>
<usePhrasedFileName>true</usePhrasedFileName>
<usePhrasedClassNameInRunning>true</usePhrasedClassNameInRunning>
<usePhrasedClassNameInTestCaseSummary>true</usePhrasedClassNameInTestCaseSummary>
</statelessTestsetInfoReporter>
<statelessTestsetReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5Xml30StatelessReporter">
<usePhrasedFileName>true</usePhrasedFileName>
<usePhrasedTestSuiteClassName>true</usePhrasedTestSuiteClassName>
<usePhrasedTestCaseClassName>true</usePhrasedTestCaseClassName>
<usePhrasedTestCaseMethodName>true</usePhrasedTestCaseMethodName>
</statelessTestsetReporter>
</configuration>
<dependencies>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-build-tools-config</artifactId>
<version>${pmd.build-tools.version}</version>
</dependency>

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.

3 participants