Fix unexpected NumberFormatException in Durations.parse() by replacing with documented thrown ParseException.

This fixes parsing of invalid second value long to throw the correct exception. Most users should already be handling ParseExceptions e.g. for invalid nanos

PiperOrigin-RevId: 686281774
pull/18839/head
Sandy Zhang 4 months ago committed by Copybara-Service
parent 3b2a6081eb
commit ee5aa499af
  1. 7
      java/util/src/main/java/com/google/protobuf/util/Durations.java

@ -237,7 +237,12 @@ public final class Durations {
nanoValue = secondValue.substring(pointPosition + 1);
secondValue = secondValue.substring(0, pointPosition);
}
long seconds = Long.parseLong(secondValue);
long seconds;
try {
seconds = Long.parseLong(secondValue);
} catch (NumberFormatException e) {
throw new ParseException("Invalid duration string: " + value, 0);
}
int nanos = nanoValue.isEmpty() ? 0 : Timestamps.parseNanos(nanoValue);
if (seconds < 0) {
throw new ParseException("Invalid duration string: " + value, 0);

Loading…
Cancel
Save