Menu

#478 InsufficientStringBufferDeclaration does not consider paths

closed
pmd (543)
5
2012-10-07
2006-03-30
No

The InsufficientStringBufferDeclaration is overly
aggressive. Consider this real world sample fropm XOM :

private static String decompose(int character) {

    if (character < 0x00C0) {
        return String.valueOf((char) character);
    }
    else if (character >= FIRST_HANGUL_SYLLABLE &&

character <= LAST_HANGUL_SYLLABLE) {
return decomposeHangul((char) character);
}

    StringBuffer sb = new StringBuffer(2);
    if (character == 0x00C0) {
        sb.append((char) 0x0041);
        sb.append((char) 0x0300);
    }
    else if (character == 0x00C1) {
        sb.append((char) 0x0041);
        sb.append((char) 0x0301);
    }
    else if (character == 0x00C2) {
        sb.append((char) 0x0041);
        sb.append((char) 0x0302);
    }
    //a few hundred more cases...
    else if (character == 0x2FA1C) {
        sb.append((char) 0x9F3B);
    }
    else if (character == 0x2FA1D) {
        sb.append((char) 0xd869);
        sb.append((char) 0xde00);
    }
    else { // not decomposable
        if (character <= 0xFFFF) {
            sb.append((char) character);
         }
         else {
             sb.append(getHighSurrogate(character));
             sb.append(getLowSurrogate(character));
         }
    }

    return sb.toString();

}
///CLOVER:ON

It reports this as "StringBuffer constructor is
initialized with size 2, but has 11550 characters
appended" even though in reality only two characters
are ever appended.

Discussion

  • Allan Caplan

    Allan Caplan - 2006-03-30

    Logged In: YES
    user_id=1127445

    Elliotte
    You're right, this could also be a problem for a couple
    other String related rules. Let me work with it - I should
    have something new you can test in a few days

     
  • Tom Copeland

    Tom Copeland - 2006-03-31

    Logged In: YES
    user_id=5159

    Passing this one over to you, Allan...

    Tom

     
  • Allan Caplan

    Allan Caplan - 2006-04-10

    Logged In: YES
    user_id=1127445

    Fixed. Was trying to fix all 3 defects together, took a bit
    longer than I had hoped.

    Problem was that I expected all if/else's to be under the
    same if statement, when they're really nested.

     

Log in to post a comment.

MongoDB Logo MongoDB