Skip to content

Commit

Permalink
Fix unhandled event loop exception when binary parser is not in plugi…
Browse files Browse the repository at this point in the history
…n.xml

If a .cproject references a binary parser ID that is not in
the plug-in XML, or in the XML, but marked as private, the
UI cannot display the binary parsers and was raising an
ArrayIndexOutOfBoundsException as below.

This fix rewrites the array handling using collections.


```java
!ENTRY org.eclipse.ui 4 0 2022-11-04 09:44:27.409
!MESSAGE Unhandled event loop exception
!STACK 0
java.lang.ArrayIndexOutOfBoundsException: Index 7 out of bounds for length 7
	at org.eclipse.cdt.ui.newui.BinaryParsTab.updateData(BinaryParsTab.java:253)
	at org.eclipse.cdt.ui.newui.AbstractCPropertyTab.setVisible(AbstractCPropertyTab.java:253)
	at org.eclipse.cdt.ui.newui.BinaryParsTab.setVisible(BinaryParsTab.java:221)
	at org.eclipse.cdt.ui.newui.AbstractCPropertyTab.handleTabEvent(AbstractCPropertyTab.java:630)
	at org.eclipse.cdt.ui.newui.AbstractPage.updateSelectedTab(AbstractPage.java:412)
	at org.eclipse.cdt.ui.newui.AbstractPage$4.widgetSelected(AbstractPage.java:382)
```
  • Loading branch information
jonahgraham committed Nov 4, 2022
1 parent 09e3b5c commit ac979bd
Showing 1 changed file with 31 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@
*******************************************************************************/
package org.eclipse.cdt.ui.newui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.CoreModelUtil;
Expand Down Expand Up @@ -224,42 +231,38 @@ public void setVisible(boolean _visible) {

@Override
public void updateData(ICResourceDescription cfgd) {
String[] ids = null;
Set<String> selectedIds = new LinkedHashSet<>();
if (page.isForPrefs()) { // prefs
if (cfgd != null && cfgd.getConfiguration() != null) {
tps = cfgd.getConfiguration().getTargetPlatformSetting();
if (tps != null)
ids = tps.getBinaryParserIds();
if (tps != null) {
String[] binaryParserIds = tps.getBinaryParserIds();
if (binaryParserIds != null) {
selectedIds.addAll(Arrays.asList(binaryParserIds));
}
}
}
if (ids == null)
ids = new String[0]; // no selection
} else { // project
ICConfigurationDescription[] cfgs = page.getCfgsEditable();
ids = CoreModelUtil.getBinaryParserIds(cfgs);
}
Object[] data = new Object[configMap.size()];
HashMap<String, BinaryParserConfiguration> clone = new HashMap<>(configMap);
// add checked elements
int i;
for (i = 0; i < ids.length; i++) {
data[i] = clone.get(ids[i]);
clone.remove(ids[i]);
}
// add remaining parsers (unchecked)
Iterator<String> it = clone.keySet().iterator();
// i = 0;
while (it.hasNext()) {
String s = it.next();
data[i++] = clone.get(s);
}
tv.setInput(data);
tv.setAllChecked(false);
// set check marks
for (i = 0; i < ids.length; i++) {
if (configMap.containsKey(ids[i])) {
tv.setChecked(configMap.get(ids[i]), true);
String[] binaryParserIds = CoreModelUtil.getBinaryParserIds(cfgs);
if (binaryParserIds != null) {
selectedIds.addAll(Arrays.asList(binaryParserIds));
}
}

List<BinaryParserConfiguration> selectedBinaryParsers = selectedIds.stream().map(configMap::get)
.filter(Objects::nonNull).toList();
List<BinaryParserConfiguration> notSelectedBinaryParsers = configMap.entrySet().stream()
.filter(e -> !selectedIds.contains(e.getKey())).map(Entry::getValue).toList();

// Add the selected ones first so they are at the top of the list
List<BinaryParserConfiguration> allBinaryParsers = new ArrayList<>();
allBinaryParsers.addAll(selectedBinaryParsers);
allBinaryParsers.addAll(notSelectedBinaryParsers);
tv.setInput(allBinaryParsers.toArray());

tv.setAllChecked(false);
selectedBinaryParsers.forEach(e -> tv.setChecked(e, true));
updateButtons();
}

Expand Down

0 comments on commit ac979bd

Please sign in to comment.