#2333: Fix StringIndexOutOfBoundsException in TextComponent.fromLegacy

This commit is contained in:
Gabscap 2018-01-27 15:43:33 +01:00 committed by md_5
parent aef386178a
commit c0356eb72d
2 changed files with 43 additions and 1 deletions

View File

@ -39,7 +39,10 @@ public final class TextComponent extends BaseComponent
char c = message.charAt( i ); char c = message.charAt( i );
if ( c == ChatColor.COLOR_CHAR ) if ( c == ChatColor.COLOR_CHAR )
{ {
i++; if ( ++i >= message.length() )
{
break;
}
c = message.charAt( i ); c = message.charAt( i );
if ( c >= 'A' && c <= 'Z' ) if ( c >= 'A' && c <= 'Z' )
{ {

View File

@ -0,0 +1,39 @@
package net.md_5.bungee.api.chat;
import net.md_5.bungee.api.ChatColor;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TextComponentTest
{
@Test
public void testInvalidColorCodes()
{
StringBuilder allInvalidColorCodes = new StringBuilder();
// collect all invalid color codes (e.g. §z, §g, ...)
for ( char alphChar : "0123456789abcdefghijklmnopqrstuvwxyz".toCharArray() )
{
if ( ChatColor.ALL_CODES.indexOf( alphChar ) == -1 )
{
allInvalidColorCodes.append( ChatColor.COLOR_CHAR );
allInvalidColorCodes.append( alphChar );
}
}
// last char is a single '§'
allInvalidColorCodes.append( ChatColor.COLOR_CHAR );
String invalidColorCodesLegacyText = fromAndToLegacyText( allInvalidColorCodes.toString() );
String emptyLegacyText = fromAndToLegacyText( "" );
// all invalid color codes and the trailing '§' should be ignored
assertEquals( emptyLegacyText, invalidColorCodesLegacyText );
}
private String fromAndToLegacyText(String legacyText)
{
return BaseComponent.toLegacyText( TextComponent.fromLegacyText( legacyText ) );
}
}