@@ -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