Skip to content

Commit

Permalink
Fix ArrayIndexOutOfBoundsException in CommentPanel. (#11786)
Browse files Browse the repository at this point in the history
This a follow-up to my change that narrowed the scope of the lock in this code. We're now seeing node.getLastChild() throw an index out of bounds, even though it's directly after a size check. This is likely due to a race.

Instead of reading the children off the actual history tree, this changes the code to instead get this info directly from the event, which is more correct as well. It should fix the issue.
  • Loading branch information
asvitkine committed Jul 17, 2023
1 parent c4f9314 commit 469ad90
Showing 1 changed file with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
@Slf4j
class CommentPanel extends JPanel {
private static final long serialVersionUID = -9122162393288045888L;

private static final Pattern COMMENT_PATTERN = Pattern.compile("^COMMENT: (.*)");

private JTextPane text;
private JScrollPane scrollPane;
private JTextField nextMessage;
Expand Down Expand Up @@ -134,16 +137,17 @@ public void treeStructureChanged(final TreeModelEvent e) {
private void readHistoryTreeEvent(final TreeModelEvent e) {
SwingAction.invokeNowOrLater(
() -> {
final Document doc = text.getDocument();
final HistoryNode node = (HistoryNode) e.getTreePath().getLastPathComponent();
final TreeNode child =
node == null ? null : (node.getChildCount() > 0 ? node.getLastChild() : null);
final String title =
child != null
? (child instanceof Event ? ((Event) child).getDescription() : child.toString())
: (node != null ? node.getTitle() : "");
final Pattern p = Pattern.compile("^COMMENT: (.*)");
final Matcher m = p.matcher(title);
final Object[] children = e.getChildren();
final Object child =
(children != null && children.length > 0) ? children[children.length - 1] : null;
final String title;
if (child != null) {
title = (child instanceof Event) ? ((Event) child).getDescription() : child.toString();
} else {
final HistoryNode node = (HistoryNode) e.getTreePath().getLastPathComponent();
title = (node != null) ? node.getTitle() : "";
}
final Matcher m = COMMENT_PATTERN.matcher(title);
if (m.matches()) {
final GamePlayer gamePlayer;
final String player;
Expand All @@ -156,6 +160,7 @@ private void readHistoryTreeEvent(final TreeModelEvent e) {
final Icon icon = iconMap.get(gamePlayer);
try {
// insert into ui document
final Document doc = text.getDocument();
final String prefix = " " + player + "(" + round + ") : ";
text.insertIcon(icon);
doc.insertString(doc.getLength(), prefix, bold);
Expand Down

0 comments on commit 469ad90

Please sign in to comment.