8253459: Formatter treats index, width and precision > Integer.MAX_VALUE incorrectly

Reviewed-by: rriggs, smarks
This commit is contained in:
Ian Graves 2020-11-19 20:20:55 +00:00 committed by Stuart Marks
parent b9db002fef
commit 080c707aab
5 changed files with 175 additions and 8 deletions

View file

@ -692,12 +692,28 @@ import sun.util.locale.provider.ResourceBundleBasedAdapter;
* <p> If the format specifier contains a width or precision with an invalid
* value or which is otherwise unsupported, then a {@link
* IllegalFormatWidthException} or {@link IllegalFormatPrecisionException}
* respectively will be thrown.
* respectively will be thrown. Similarly, values of zero for an argument
* index will result in an {@link IllegalFormatException}.
*
* <p> If a format specifier contains a conversion character that is not
* applicable to the corresponding argument, then an {@link
* IllegalFormatConversionException} will be thrown.
*
* <p> Values of <i>precision</i> must be in the range zero to
* {@link Integer#MAX_VALUE}, inclusive, otherwise
* {@link IllegalFormatPrecisionException} is thrown.</p>
*
* <p> Values of <i>width</i> must be in the range one to
* {@link Integer#MAX_VALUE}, inclusive, otherwise
* {@link IllegalFormatWidthException} will be thrown
* Note that widths can appear to have a negative value, but the negative sign
* is a <i>flag</i>. For example in the format string {@code "%-20s"} the
* <i>width</i> is <i>20</i> and the <i>flag</i> is "-".</p>
*
* <p> Values of <i>index</i> must be in the range one to
* {@link Integer#MAX_VALUE}, inclusive, otherwise
* {@link IllegalFormatException} will be thrown.</p>
*
* <p> All specified exceptions may be thrown by any of the {@code format}
* methods of {@code Formatter} as well as by any {@code format} convenience
* methods such as {@link String#format(String,Object...) String.format} and
@ -2783,8 +2799,11 @@ public final class Formatter implements Closeable, Flushable {
try {
// skip the trailing '$'
index = Integer.parseInt(s, start, end - 1, 10);
if (index <= 0) {
throw new IllegalFormatArgumentIndexException(index);
}
} catch (NumberFormatException x) {
assert(false);
throw new IllegalFormatArgumentIndexException(Integer.MIN_VALUE);
}
} else {
index = 0;
@ -2811,7 +2830,7 @@ public final class Formatter implements Closeable, Flushable {
if (width < 0)
throw new IllegalFormatWidthException(width);
} catch (NumberFormatException x) {
assert(false);
throw new IllegalFormatWidthException(Integer.MIN_VALUE);
}
}
return width;
@ -2826,7 +2845,7 @@ public final class Formatter implements Closeable, Flushable {
if (precision < 0)
throw new IllegalFormatPrecisionException(precision);
} catch (NumberFormatException x) {
assert(false);
throw new IllegalFormatPrecisionException(Integer.MIN_VALUE);
}
}
return precision;