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

[java] InsufficientStringBufferDeclaration doesn't handle non-constant conditionals #4888

Open
jsotuyod opened this issue Mar 25, 2024 · 0 comments
Labels
a:false-negative PMD doesn't flag a problematic piece of code

Comments

@jsotuyod
Copy link
Member

Affects PMD Version:
7.0.0

Rule:

InsufficientStringBufferDeclaration

Description:

When reducing the expression to compute the size, the rule fails to process non-constant conditions. But in doing so, misses that some branches may effectively allocate too small a buffer.

Ideally, these expressions would be reduced to retrieve the minimum possible value to analyze, as the rule checks against the minimum number of appended chars expected.

Code Sample demonstrating the issue:

public class ConditionalExpression {
    public void bar(int arg) {
        StringBuffer sb = new StringBuffer(arg > 1 ? 500 : 2);  // Should report a warning at this line, 2 is too small
        sb.append('a');
        sb.append('a');
        sb.append('a');
    }
}

Expected outcome:

PMD should report a violation at line 3, but doesn't. This is a false-negative.

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]
Any

@jsotuyod jsotuyod added the a:false-negative PMD doesn't flag a problematic piece of code label Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-negative PMD doesn't flag a problematic piece of code
Projects
None yet
Development

No branches or pull requests

1 participant