Skip to content

Commit 851c120

Browse files
authored
Update I18nFormatUtil.java
1 parent d89d696 commit 851c120

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

checker-util/src/main/java/org/checkerframework/checker/i18nformatter/util/I18nFormatUtil.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,39 @@ public static void tryFormatSatisfiability(String format) throws IllegalFormatEx
4848
*/
4949
public static I18nConversionCategory[] formatParameterCategories(String format)
5050
throws IllegalFormatException {
51+
5152
tryFormatSatisfiability(format);
52-
I18nConversion[] cs = MessageFormatParser.parse(format);
53+
54+
I18nConversion[] cs;
55+
try {
56+
cs = MessageFormatParser.parse(format);
57+
} catch (Exception e) {
58+
// Defensive programming: fail gracefully on parse errors
59+
throw new IllegalFormatException("Invalid format string: " + format);
60+
}
5361

5462
int maxIndex = -1;
5563
Map<Integer, I18nConversionCategory> conv = new HashMap<>(cs.length);
5664

5765
for (I18nConversion c : cs) {
5866
int index = c.index;
67+
if (index < 0 || index > 1000) { // Arbitrary upper bound to prevent abuse
68+
throw new IllegalFormatException("Format string contains illegal argument index: " + index);
69+
}
70+
5971
Integer indexKey = index;
60-
conv.put(
61-
indexKey,
62-
I18nConversionCategory.intersect(
63-
c.category,
64-
conv.containsKey(indexKey) ? conv.get(indexKey) : I18nConversionCategory.UNUSED));
72+
I18nConversionCategory existing = conv.getOrDefault(indexKey, I18nConversionCategory.UNUSED);
73+
I18nConversionCategory merged = I18nConversionCategory.intersect(c.category, existing);
74+
75+
conv.put(indexKey, merged);
6576
maxIndex = Math.max(maxIndex, index);
6677
}
6778

6879
I18nConversionCategory[] res = new I18nConversionCategory[maxIndex + 1];
6980
for (int i = 0; i <= maxIndex; i++) {
70-
Integer indexKey = i;
71-
res[i] = conv.containsKey(indexKey) ? conv.get(indexKey) : I18nConversionCategory.UNUSED;
81+
res[i] = conv.getOrDefault(i, I18nConversionCategory.UNUSED);
7282
}
83+
7384
return res;
7485
}
7586

0 commit comments

Comments
 (0)