Skip to content

Commit d13b33f

Browse files
committed
More defensive guards against invalid code points in ant Preferences
Once the Preferences is loaded containing U+0000 in a key, it can't be used anymore since even removing that key (e.g via clear()) will throw IAE. What we can do however is to remove and recreate the Preferences node.
1 parent fb18907 commit d13b33f

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

extide/o.apache.tools.ant.module/src/org/apache/tools/ant/module/AntSettings.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.TreeMap;
3232
import java.util.logging.Level;
3333
import java.util.logging.Logger;
34+
import java.util.prefs.BackingStoreException;
3435
import java.util.prefs.Preferences;
3536
import java.util.regex.Pattern;
3637
import org.apache.tools.ant.module.api.IntrospectedInfo;
@@ -122,7 +123,27 @@ public static synchronized IntrospectedInfo getCustomDefs() {
122123
}
123124

124125
public static synchronized void setCustomDefs(IntrospectedInfo ii) {
125-
IntrospectedInfoSerializer.instance.store(prefs().node(PROP_CUSTOM_DEFS), ii);
126+
Preferences prefs = prefs();
127+
Preferences node = prefs.node(PROP_CUSTOM_DEFS);
128+
try {
129+
IntrospectedInfoSerializer.instance.store(node, ii);
130+
} catch (IllegalArgumentException iae) {
131+
// recreate node in case of corrupted files. Once Preferences are loaded,
132+
// keys which contain code point U+0000 can't be used anymore (clear() will also fail)
133+
if (iae.getMessage().contains("U+0000")) {
134+
LOG.log(Level.WARNING, "recreating {0} preferences node due to code point U+0000", PROP_CUSTOM_DEFS);
135+
try {
136+
node.removeNode();
137+
prefs.flush();
138+
node = prefs.node(PROP_CUSTOM_DEFS);
139+
IntrospectedInfoSerializer.instance.store(node, ii);
140+
} catch (BackingStoreException bse) {
141+
throw new RuntimeException("can't write to preferences", bse);
142+
}
143+
} else {
144+
throw iae;
145+
}
146+
}
126147
customDefs = ii;
127148
}
128149

extide/o.apache.tools.ant.module/src/org/apache/tools/ant/module/api/IntrospectedInfo.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -683,21 +683,21 @@ public IntrospectedInfo load(Preferences node) {
683683
try {
684684
v = node.get(k, null);
685685
} catch (IllegalArgumentException ex) { // e.g invalid code point JDK-8075156
686-
LOG.log(Level.WARNING, "malformed key: {0}, pref path: {1}, msg: {2}",
687-
new Object[] {k, node.absolutePath(), ex.getMessage()});
686+
LOG.log(Level.WARNING, "skipping malformed key; pref path: {0}, msg: {1}",
687+
new Object[] {node.absolutePath(), ex.getMessage()});
688688
continue;
689689
}
690690
assert v != null : k;
691691
String[] ss = k.split("\\.", 2);
692692
if (ss.length != 2) {
693-
LOG.log(Level.WARNING, "malformed key: {0}, pref path: {1}", new Object[] {k, node.absolutePath()});
693+
LOG.log(Level.WARNING, "skipping malformed key: {0}, pref path: {1}", new Object[] {k, node.absolutePath()});
694694
continue;
695695
}
696696
if (ss[0].equals("class")) {
697697
Matcher m = p.matcher(ss[1]);
698698
boolean match = m.matches();
699699
if (!match) {
700-
LOG.log(Level.WARNING, "malformed key: {0}, pref path: {1}", new Object[] {k, node.absolutePath()});
700+
LOG.log(Level.WARNING, "skipping malformed key: {0}, pref path: {1}", new Object[] {k, node.absolutePath()});
701701
continue;
702702
}
703703
String c = m.group(1);

0 commit comments

Comments
 (0)